Wednesday, October 29, 2008

[OpenCV]: find circle with Hough transform

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char** argv) {
IplImage* image = cvLoadImage(
"circle.jpg",
CV_LOAD_IMAGE_GRAYSCALE
);
IplImage* src = cvLoadImage("circle.jpg");
CvMemStorage* storage = cvCreateMemStorage(0);
cvSmooth(image, image, CV_GAUSSIAN, 5, 5 );
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
4,
image->width/10
);

for( int i = 0; i < results->total; i++ )
{
float* p = (float*) cvGetSeqElem( results, i );
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
cvCircle(
src,
pt,
cvRound( p[2] ),
CV_RGB(0xff,0,0)
);
}
cvNamedWindow( "HoughCircles", 1 );
cvShowImage( "HoughCircles", src);
cvWaitKey(0);
}


codes from 'learning opencv' book examples

No comments:

Post a Comment