I am trying to detect 22 balls on the snooker table. I have an image to test on but programme os detecting 2 balls, and random circles elsewhere. My code is below with the circle detection algorithm. Does anyone know which parameters should be adjusted to get the detection i would need? thanks
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(int argc, char** argv)
{ int edge_thresh = 1;
IplImage* img = cvLoadImage("C:\\Users\\Nathan\\Desktop\\SnookerPic.png", 1);;
IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);
IplImage *edge = cvCreateImage( cvSize(img->width,img->height), 8, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor(img, gray, CV_BGR2GRAY);
cvThreshold(gray,gray, CV_GAUSSIAN, 9, 9);
cvSmooth(gray, gray, CV_GAUSSIAN, 11, 11);
cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 5);
CvSeq* circles = cvHoughCircles(edge, storage,
CV_HOUGH_GRADIENT, 2, 20, 200, 50);
int i;
for (i = 0; i < circles->total; i++)
{
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])),
3, CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])),
cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
}
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", img );
return 0;
}
I suspect you are having trouble with the parameters being either too restrictive or loose. You need to play with the parameters until you get the number of circles you want. Also, the Gaussian 11×11 blur may be a bit aggressive depending on the image. For my image, it was doing more harm than good, but my image is kind of idealized…
I modified the OpenCV example you are using to include the track bars allowing you to play with the Canny parameters. This should really help you get a feel for how it works. Also, pay attention to the minDist parameter. For my image, the circle centers were about 32 pixels away. You’ll need to adjust this to your circle sizes. So, here is the sample:
I used this snooker image, and this is the output I get.
(P.S. consider using the C++ interface it’s far superior to the C interface IMHO 🙂