c++ - OpenCV C++ : How to find all the circles in an image

标签 c++ opencv

您好,我正在尝试找到下图中的所有圆圈并识别缺陷。

这是我的代码:

static void findCircles2(const Mat& image)
{
	vector<Vec3f> circles;
	int thresh1 = 5;
    Mat pyr, timg, gray0(image.size(), CV_8U), gray;
    pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
    pyrUp(pyr, timg, image.size());
	for( int c = 0; c < 3; c++ )
    {
        int ch[] = {c, 0};
        mixChannels(&timg, 1, &gray0, 1, ch, 1);
        Canny(gray0, gray, 0, thresh1, 5);
        //dilate(gray, gray, Mat(), Point(-1,-1));
        gray = gray0 >= (1)*255/N;
		gray = gray0 >= (2)*255/N;
		gray = gray0 >= (6)*255/N;
		namedWindow( "Hough Circle Transform Demo 1", CV_WINDOW_AUTOSIZE );
		imshow( "Hough Circle Transform Demo 1", gray );
		waitKey(0);

		HoughCircles( gray, circles, CV_HOUGH_GRADIENT, 1, gray.rows/8, 200, 100, 0, 0 );
		cout<<"size of circles: "<<circles.size()<<endl;
		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( gray, center, 3, Scalar(0,255,0), -1, 8, 0 );
			circle( gray, center, radius, Scalar(0,0,255), 3, 8, 0 );
		}

  /// Show your results
		namedWindow( "Hough Circle Transform Demo 2", CV_WINDOW_AUTOSIZE );
		imshow( "Hough Circle Transform Demo 2", gray );

		 waitKey(0);

    }
}

图片:

enter image description here

但是代码无法找到任何东西,我试过阈值但没有帮助。请指教。

开发平台:VS2010,Opencv版本:2.4.10

最佳答案

因为圆圈太小而不是standard , 所以你不能只做 HoughCircles在二进制图像上。

另一种方法是 findContours ,然后按 contourArea 的值之间的比率过滤轮廓和 minEnclosingCircle 的值.


enter image description here

关于c++ - OpenCV C++ : How to find all the circles in an image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486245/

相关文章:

c++ - 从共享库调用带有 STL 参数的函数

python-3.x - 无法访问内置网络摄像头 Python

python - OpenCV-使用多个IP摄像机进行运动捕捉

python opencv去除图像中的噪声

android - 在 Android 中使用 OpenCV 提取按颜色检测的区域

c++ - 如何 boost 我的项目?

java - OpenGL 正确附加纹理

c++ - Cygwin pkg-config/protobuf 文件路径问题

c++ - 使用 while 循环对三角形进行编号

python - 检测图像中的正方形(绘画)并使用 python 在它们周围绘制轮廓