Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV - HoughCircles causing program to crash

I want to detect circles in an image using OpenCV and C++. I COULD do that by referring to the official documentation and adjusting the parameters of the piece of code written by the OpenCV Team.

So, the code I'm working with is as follows: (parameters already adjusted)

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include <iostream>
    #include <stdio.h>

    using namespace cv;

    int main(int, char** argv)
    {
       Mat src, src_gray;

       /// Read the image
       src = imread( argv[1], 1 );

       if( !src.data )
         { return -1; }

       /// Convert it to gray
        cvtColor( src, src_gray, CV_BGR2GRAY );

       /// Reduce the noise so we avoid false circle detection
        GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );

        vector<Vec3f> circles;

       /// Apply the Hough Transform to find the circles
        HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 6.0, 5, 110, 70, 3, 20 );

       /// Draw the circles detected
        for( size_t i = 0; i < circles.size(); i++ )
        {
             Point center(cvRound(circles[i][0]), cvRound(circles[i][2]));
             int radius = cvRound(circles[i][3]);
             // circle center
             circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
             // circle outline
             circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
        }

       /// Show your results
        namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
        imshow( "Hough Circle Transform Demo", src );

        waitKey(0);

        src.release();
        src_gray.release();

        return 0;
    }

And the image whose circles I want to detect is the following: Test image

These are actually the contour of two blobs that I obtained using cvBlobsLib and redrew as a new image.

That algorithm is able to identify the centers of each circle, but, when I hit any key to close the program, it crashes... :( And I have to forcefully close it.

I need to adapt that algorithm to run in a camera, so I cannot proceed with the implementation while it crashes like that.

So, does anyone know what could be causing this problem? I'm doing the development on Visual Studio 2012 and OpenCV version 2.4.2.

If someone could give me a suggestion of what it could be or maybe try running the algorithm, I would be very grateful!

like image 989
Saph Avatar asked Aug 15 '26 20:08

Saph


1 Answers

I have four advices for you.

First: To see whether a Mat is empty or not, you use

if( src.empty() ) // instead of !src.data.

The chances are src.data has random (stale) value for an empty Mat.

Second: correct the indices like this:

Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);

(actually you don't need cvRound, but whatever).

Third: It is worth to check whether imread understood that you want to load the image in color mode, by checking its number of channels:

src.channels()==3
//or
src.type()==CV_8UC3; // that is what you are counting for, really.

Otherwise a line like CV_BGR2GRAY on a gray image could cause weird behaviour.

Fourth: you don't need to release Mat's. That's the reason they created Mat class in the first place, so that they automatically take care of releasing.

like image 114
Barney Szabolcs Avatar answered Aug 18 '26 09:08

Barney Szabolcs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!