当前位置:主页 > 软件编程 > C代码 >

C++ opencv霍夫圆检测使用案例详解

时间:2022-05-15 07:46:13 | 栏目:C代码 | 点击:

本程序是一个最简单的霍夫圆检测函数的使用案例,刚刚学会的用法,发一下,可以参考,参数啥的可根据图片调节。

#pragma once
#include<quickopencv.h>
#include<vector>
#include <stdio.h>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<opencv2/opencv.hpp>
 
using namespace cv;
using namespace std;
  
class QuickDemo {
public:
	void ColorSpace_Demo(Mat &image);
 
 
};
 
void QuickDemo::ColorSpace_Demo(Mat &image)
 
{
	Mat gray, hsv, img3;
	cvtColor(image, gray, COLOR_BGR2GRAY);
	cvtColor(image, hsv, COLOR_BGR2HSV);
	//H取值0-180表示颜色 S取值0-255表示饱和度 V0-255表示亮度
	namedWindow("huidu", WINDOW_FREERATIO);
	
	imshow("huidu", gray);
 
GaussianBlur(gray, gray, Size(9, 9), 0, 0);//高斯模糊
	medianBlur(gray, gray, 3);//中值滤波
	
 
vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 1, 1, 100,100, 0,00 );
 
Canny(gray, img3, 50, 100);//边缘检测
 
for (size_t i = 0; i < circles.size(); i++)
		  {
		     Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
		      int radius = cvRound(circles[i][2]);
		       // circle center
			       circle(gray, center, 3, Scalar(60, 30, 60), -1, 5, 0);
		       // circle outline
			       circle(gray, center, radius, Scalar(0, 60, 60), 3, 3, 0);
				   
		   }
namedWindow("Hough Circle Transform Demo", WINDOW_FREERATIO);
	imshow("Hough Circle Transform Demo",gray);
}

主函数调用

int main(int, char **argv)
{
	Mat src = imread("D:/YUAN.jpg");//只能读八位或者小数图像,通道顺序B G R 取值空间为0-255
 
	if (src.empty()) 
	{ 
		cout<<"cant read";
	
	return -1;
	}
	
	QuickDemo qd;
	qd.ColorSpace_Demo(src);
 
 
	waitKey(0);
	
	destroyAllWindows;
	
 
	return 0;
}

运行效果如下

您可能感兴趣的文章:

相关文章