简体   繁体   English

OpenCV C ++:如何查找图像中的所有圆

[英]OpenCV C++: How to find all the circles in an image

Hi I am trying to find all the circles in the following image and Identify the defect. 嗨,我试图在下图中找到所有的圆圈,并找出缺陷。

this is my code: 这是我的代码:

 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); } } 

Picture: 图片:

在此处输入图片说明

however the code is unable to find anything, I played arround with the thresholds but it doesnt help. 但是代码无法找到任何东西,我在阈值周围玩了一下,但没有帮助。 please advise. 请指教。

Development platform: VS2010, Opencv version: 2.4.10 开发平台:VS2010,Opencv版本:2.4.10

Because the circles are so small and not that standard , so you cann't just do HoughCircles on the binary image. 因为圆圈太小而不是那个standard ,所以您不能只对二进制图像执行HoughCircles

An alternative method is to findContours , then filter the contours by ratio between the value of contourArea and the value of minEnclosingCircle . 另一种方法是,以findContours ,然后通过的值之比过滤轮廓contourArea和的值minEnclosingCircle


在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM