霍夫圆变换的函数为:
利用 Hough 变换在灰度图像中找圆
CvSeq* cvHoughCircles( CvArr* image, void* circle_storage, int method, double dp, double min_dist, double param1=100, double param2=100, int min_radius=0, int max_radius=0 );
. 每个圆由三个浮点数表示:圆心坐标(x,y)和半径.
Resolution of the accumulator used to detect centers of the circles. For example, if it is 1, the accumulator will have the same resolution as the input image, if it is 2 - accumulator will have twice smaller width and height, etc.
Minimum distance between centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
The first method-specific parameter. In case of CV_HOUGH_GRADIENT it is the higher threshold of the two passed to Canny edge detector (the lower one will be twice smaller).
The second method-specific parameter. In case of CV_HOUGH_GRADIENT it is accumulator threshold at the center detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
Minimal radius of the circles to search for.
Maximal radius of the circles to search for. By default the maximal radius is set to max(image_width, image_height).
The function cvHoughCircles finds circles in grayscale image using some modification of Hough transform.
Example. Detecting circles with Hough transform.
实现例题:#include "stdafx.h" #include "stdafx.h" #include "cv.h" #include "highgui.h" #include <math.h> int _tmain(int argc, _TCHAR* argv[]) { IplImage* image0=cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE); IplImage* image= cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE); //IplImage* image=NULL;// //image=cvCreateImage(cvGetSize(image0),IPL_DEPTH_8U,3); CvMemStorage* storage=cvCreateMemStorage(0); cvSmooth(image0,image,CV_GAUSSIAN,5,5); CvSeq* results=cvHoughCircles(image,storage,CV_HOUGH_GRADIENT,2,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(image,pt,cvRound(p[2]),CV_RGB(0xff,0xff,0xff)); } cvNamedWindow("source",0); cvShowImage("source",image0); cvNamedWindow("cvHoughCircles",0); cvShowImage("cvHoughCircles",image); cvWaitKey(0); return 0; }
参考资料:‘
1.学习OpenCV,于仕祺,刘瑞祯,清华大学出版社,pp.179-183
2.http://www.opencv.org.cn/index.php/Cv%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86#HoughLines
PS:修改一下:
#include "stdafx.h" #include "stdafx.h" #include "cv.h" #include "highgui.h" #include <math.h> int _tmain(int argc, _TCHAR* argv[]) { IplImage* image0=cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE); //IplImage* image= cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE); IplImage *image1=cvLoadImage("circle.bmp",1); //IplImage* image=NULL;// //image=cvCreateImage(cvGetSize(image0),IPL_DEPTH_8U,3); CvMemStorage* storage=cvCreateMemStorage(0); //cvSmooth(image0,image,CV_GAUSSIAN,5,5); CvSeq* results=cvHoughCircles(image0,storage,CV_HOUGH_GRADIENT,2,image0->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(image1,pt,cvRound(p[2]),CV_RGB(255,0,0)); } cvNamedWindow("source",0); cvShowImage("source",image0); cvNamedWindow("cvHoughCircles",0); cvShowImage("cvHoughCircles",image1); cvWaitKey(0); return 0; }
看起来清楚些,可是还是检测的不好。