您好,我正在尝试找到下图中的所有圆圈并识别缺陷。
这是我的代码:
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);
}
}
图片:
但是代码无法找到任何东西,我试过阈值但没有帮助。请指教。
开发平台:VS2010,Opencv版本:2.4.10
最佳答案
因为圆圈太小而不是standard
, 所以你不能只做 HoughCircles
在二进制图像上。
另一种方法是 findContours
,然后按 contourArea
的值之间的比率过滤轮廓和 minEnclosingCircle
的值.
关于c++ - OpenCV C++ : How to find all the circles in an image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47486245/