#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;
}
Friday, December 21, 2007
OpenCV: detect circles with Hough transform
Subscribe to:
Post Comments (Atom)
good example
ReplyDeleteCan 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.
ReplyDelete@aditya Hough only works with gray-scale images. But this should eliminate issues with colour differences.
ReplyDeleteGreat example, thanks very much! :)
Thnks.
ReplyDeleteHi
ReplyDeleteI 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?
CvReleaseImage(img);
ReplyDeleteCvReleaseImage(gray);
This comment has been removed by the author.
ReplyDeletecan I apply this to a soccer match to detect soccer ball
ReplyDeletehi
ReplyDeleteImplement the global processing using Do Hough Transform
fathallah82@yahoo.com
ReplyDelete