## Friday, December 21, 2007

### OpenCV: detect circles with Hough transform

#include <cv.h>#include <highgui.h>#include <math.h>int main(int argc, char** argv){    IplImage* img = cvLoadImage("circle.jpg", 1);;    IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);    CvMemStorage* storage = cvCreateMemStorage(0);    cvCvtColor(img, gray, CV_BGR2GRAY);    cvSmooth(gray, gray, CV_GAUSSIAN, 9, 9);     CvSeq* circles = cvHoughCircles(gray, storage,         CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100);    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 );    cvWaitKey(0);        return 0;}

1. Anonymous8:38 AM

good example

2. Can you provide any pointers on how do I proceed with the problem of detecting circles in which there is a color gradient. My problem is to detect a ball in different lighting conditions.

3. @aditya Hough only works with gray-scale images. But this should eliminate issues with colour differences.

Great example, thanks very much! :)

4. Anonymous3:18 AM

Thnks.

5. Anonymous1:34 AM

Hi
I guess this program has some bugs. I wrote it in a way to detects circles on frames of camera , Suddenly Memory gets full and the program breaks down , what should I do?

6. Anonymous8:29 AM

CvReleaseImage(img);
CvReleaseImage(gray);

7. This comment has been removed by the author.

8. can I apply this to a soccer match to detect soccer ball

9. hi

Implement the global processing using Do Hough Transform

10. fathallah82@yahoo.com