Ask Your Question
-1

How to find circles in an image

asked 2017-11-25 06:47:35 -0600

AdhilIqbal gravatar image

updated 2019-12-04 10:13:04 -0600

supra56 gravatar 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

image description

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][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);

    }
}
edit retag flag offensive close merge delete

Comments

I don't see duplicate.

supra56 gravatar imagesupra56 ( 2017-11-25 08:20:28 -0600 )edit

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

berak gravatar imageberak ( 2017-11-25 08:22:35 -0600 )edit

@supra56, you don't see them, because they are already deleted

berak gravatar imageberak ( 2017-11-25 08:28:55 -0600 )edit

@berak. I thought you were, talking to me.

supra56 gravatar imagesupra56 ( 2017-11-25 08:31:54 -0600 )edit

@supra56, apologies for the confusion, then

berak gravatar imageberak ( 2017-11-25 08:54:49 -0600 )edit

@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 gravatar imagesjhalayka ( 2017-11-25 09:35:24 -0600 )edit

@sjhalayka , now, cmon ...

berak gravatar imageberak ( 2017-11-25 09:41:31 -0600 )edit

@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 gravatar imageAdhilIqbal ( 2017-11-25 09:52:20 -0600 )edit

Sorry for calling you a pig. You’re welcome.

sjhalayka gravatar imagesjhalayka ( 2017-11-25 10:14:42 -0600 )edit

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

berak gravatar imageberak ( 2017-11-25 11:39:25 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2017-11-25 20:41:44 -0600

sjhalayka gravatar image

updated 2017-11-25 22:22:49 -0600

@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.

image description

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.

edit flag offensive delete link more

Comments

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

AdhilIqbal gravatar imageAdhilIqbal ( 2017-11-25 22:56:53 -0600 )edit

thanks I figured it out

AdhilIqbal gravatar imageAdhilIqbal ( 2017-11-26 06:29:19 -0600 )edit

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++;
                }
            }
        }
sjhalayka gravatar imagesjhalayka ( 2017-11-26 12:11:46 -0600 )edit

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.

sjhalayka gravatar imagesjhalayka ( 2017-11-27 09:40:11 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,588 times

Last updated: Dec 04 '19