Ask Your Question

# How to find circles in an image

Hi in the below image how do you find and mark all the circles and identify the defect, my vs is vs2010 and opencv 2.4.10 this is my code so far, but it doesnt detect anything

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]), cvRound(circles[i]));
int radius = cvRound(circles[i]);
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);

}


}

edit retag close merge delete

## Comments

@AdhilIqbal, please do NOT post duplicate questions about this, thank you.

@AdhilIqbal Why do you expect help when you didn't even say thank you for the last time you got help? Are you a capitalist pig?

@sjhalayka sorry you had to resort to foul language, but thank you for pointing out that I hadn't said thank you. sometimes i myself forget the trouble you go through to answer these questions

@AdhilIqbal , btw, IF you want to use anything hough related, try to drop the Canny (it's doing it's own internally) .

## 1 answer

Sort by » oldest newest most voted @AdhilIqbal -- You need to alter your image before you can run the Hough circle transformation on it -- do a Canny() edge detection on it, and then fill in (floodFill()) all but the largest region to get a black and white (binary) image. That stops false positives (circles that appear out of the noise) from occurring, as well as finds all 15 of the circles (plus one elliptical defect that was identified as a 16th circle). In other words, it works perfectly, and it's about as simple as it gets.

Below is the altered image that I am describing. It hurts the eyes, doesn't it? Well it's quincunxesque eye candy to the Hough circle transformation. You really need to think like the computer. My Hough circle transformation function call looks like:

// Parameter values are very much obtained by trial and error
HoughCircles(frame, // input
circles, // output
CV_HOUGH_GRADIENT, // method
1, // dp
5, // minimum distance
5, // param1
5, // param2
0, // minimum radius
25); // maximum radius


You know how big the radii of the 15 circles are. Take the mean radius. Any white region with a radius much smaller or larger than this mean radius is likely a defect.

more

## Comments

how did you setup the floodfill command? thanks by the way

Glad to hear that it's working out.

My floodFill() code looks like the following, where flt_canny is a floating point version of the Canny output:

        int section_count = 0;
int fill_colour = 1;

for(int j = 0; j < flt_canny.rows; j++)
{
for(int i = 0; i < flt_canny.cols; i++)
{
float colour = flt_canny.at<float>(j, i);

if(colour == 0)
{
floodFill(flt_canny, Point(i, j), Scalar(fill_colour));

section_count++;
fill_colour++;
}
}
}


Check out:

https://github.com/sjhalayka/obstacle...

... where the input .avi file is at:

https://github.com/sjhalayka/obstacle...

The code finds all regions (sections) in the Canny output, colours them in floating point mode, and then uses their size data to colour each segment.

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2017-11-25 06:47:35 -0500

Seen: 428 times

Last updated: Nov 25 '17