Saph Saph - 3 years ago 346
C++ Question

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( ! )
{ 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 );



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!

Answer Source

I started feeling suspicious about the environment, so I got a friend who had OpenCV all set up to try out my code and he could run it with no problem...

So I reinstalled everything, but this time I chose Microsoft Visual Studio 2010 SP1 and OpenCV 2.4.3, and it worked correctly.

If someone is having the same problem, I recommend downgrading to VS2010. Also, this video installation guide was really helpful when I was setting the environment!

Thank you :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download