2017-11-25 59 views
0

嗨我試圖找到下面的圖像中的所有圓圈,並確定缺陷。OpenCV C++:如何找到圖像中的所有圓圈

這是我的代碼:

static void findCircles2(const Mat& image) 
 
{ 
 
\t vector<Vec3f> circles; 
 
\t 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()); 
 
\t 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; 
 
\t \t gray = gray0 >= (2)*255/N; 
 
\t \t gray = gray0 >= (6)*255/N; 
 
\t \t namedWindow("Hough Circle Transform Demo 1", CV_WINDOW_AUTOSIZE); 
 
\t \t imshow("Hough Circle Transform Demo 1", gray); 
 
\t \t waitKey(0); 
 

 
\t \t HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 1, gray.rows/8, 200, 100, 0, 0); 
 
\t \t cout<<"size of circles: "<<circles.size()<<endl; 
 
\t \t for(size_t i = 0; i < circles.size(); i++) 
 
\t \t { 
 
\t \t \t Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); 
 
\t \t \t int radius = cvRound(circles[i][2]); 
 
\t \t \t circle(gray, center, 3, Scalar(0,255,0), -1, 8, 0); 
 
\t \t \t circle(gray, center, radius, Scalar(0,0,255), 3, 8, 0); 
 
\t \t } 
 

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

 
\t \t waitKey(0); 
 

 
    } 
 
}

圖片:

enter image description here

然而代碼無法找到任何東西,我角落找尋與閾值,但它發揮沒有幫助。請指教。

開發平臺:VS2010,opencv的版本:2.4.10

回答

1

因爲圈子這麼小,而不是standard,所以你水溼只是做了二值圖像HoughCircles

另一種方法是findContours,然後通過值contourAreaminEnclosingCircle的值之間的比率來過濾輪廓。


enter image description here