Detecting Circles in opencv
Hi guys,
I am using the following code to detect circles
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main()
{
Mat src, gray;
src = imread( "/homepath/pic1.png", 1 );resize(src,src,Size(640,480));
cvtColor( src, gray, COLOR_BGR2GRAY );
// Reduce the noise so we avoid false circle detection
GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
vector<Vec3f> circles;
// Apply the Hough Transform to find the circles
HoughCircles( gray, circles, HOUGH_GRADIENT, 1, 30 );
// Draw the circles detected
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( src, center, 3, Scalar(0,255,0), -1, 8, 0 );// circle center
circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );// circle outline
cout << "center : " << center << "\nradius : " << radius << endl;
}
// Show your results
namedWindow( "Hough Circle Transform Demo", WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo", src );
waitKey(0);
return 0;
}
However the output look like this:
What am I doing wrong?
If you know the minimum and maximum radius for the circles you want to detect, then maybe you shall use the last 2 parameters of the function. Anyway, you shall play with the parameters (param1 and param2), too, for adapting the application to your purpose
Why don't you like the result? I think it's really good as your circles approximate your ellipses. HoughCircles looks for circles, so which result did you expect if your input only contains ellipses?
@FooBar Don't be so sure, it contains small green circles...
@thdrksdfthmn The green circles are the center of the circles. The provided code calls HoughCircles and then draws the circle centers in green and the circle outlines in red.