霍夫圆变换的函数为:
HoughCircles
利用 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 );
-
image
- 输入 8-比特、单通道灰度图像. circle_storage
- 检测到的圆存储仓. 可以是内存存储仓(此种情况下,一个线段序列在存储仓中被创建,并且由函数返回)或者是包含圆参数的特殊类型的具有单行/单列的CV_32FC3型矩阵(CvMat*).矩阵头为函数所修改,使得它的 cols/rows 将包含一组检测到的圆。如果 circle_storage是矩阵,而实际圆的数目超过矩阵尺寸,那么最大可能数目的圆被返回
. 每个圆由三个浮点数表示:圆心坐标(x,y)和半径.
-
method
- Hough 变换方式,目前只支持CV_HOUGH_GRADIENT, which is basically 21HT, described in [Yuen03]. dp
- 累加器图像的分辨率。这个参数允许创建一个比输入图像分辨率低的累加器。(这样做是因为有理由认为图像中存在的圆会自然降低到与图像宽高相同数量的范畴)。如果dp设置为1,则分辨率是相同的;如果设置为更大的值(比如2),累加器的分辨率受此影响会变小(此情况下为一半)。dp的值不能比1小。
Resolutionof the accumulator used to detect centers of the circles. For example,if it is 1, the accumulator will have the same resolution as the inputimage, if it is 2 - accumulator will have twice smaller width andheight, etc.
-
min_dist
- 该参数是让算法能明显区分的两个不同圆之间的最小距离。
Minimumdistance between centers of the detected circles. If the parameter istoo small, multiple neighbor circles may be falsely detected inaddition to a true one. If it is too large, some circles may be missed.
-
param1
- 用于Canny的边缘阀值上限,下限被置为上限的一半。
Thefirst method-specific parameter. In case of CV_HOUGH_GRADIENT it is thehigher threshold of the two passed to Canny edge detector (the lowerone will be twice smaller).
-
param2
- 累加器的阀值。
Thesecond method-specific parameter. In case of CV_HOUGH_GRADIENT it isaccumulator threshold at the center detection stage. The smaller it is,the more false circles may be detected. Circles, corresponding to thelarger accumulator values, will be returned first.
-
min_radius
- 最小圆半径。
Minimal radius of the circles to search for.
-
max_radius
- 最大圆半径。
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;
- }
结果:
看起来清楚些,可是还是检测的不好。