找圆

霍夫圆变换的函数为:

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.

实现例题:
  1. #include "stdafx.h"  
  2. #include "stdafx.h"  
  3. #include "cv.h"  
  4. #include "highgui.h"  
  5. #include <math.h>  
  6. int _tmain(int argc, _TCHAR* argv[])  
  7. {  
  8.     IplImage* image0=cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);  
  9.     IplImage* image= cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);  
  10.     //IplImage* image=NULL;//  
  11.     //image=cvCreateImage(cvGetSize(image0),IPL_DEPTH_8U,3);   
  12.     CvMemStorage* storage=cvCreateMemStorage(0);  
  13.     cvSmooth(image0,image,CV_GAUSSIAN,5,5);  
  14.     CvSeq* results=cvHoughCircles(image,storage,CV_HOUGH_GRADIENT,2,image->width /10);  
  15.     for(int i=0;i<results->total ;i++)  
  16.     {  
  17.         float* p=(float*) cvGetSeqElem(results,i);  
  18.         CvPoint pt=cvPoint(cvRound(p[0]),cvRound(p[1]));  
  19.         cvCircle(image,pt,cvRound(p[2]),CV_RGB(0xff,0xff,0xff));  
  20.     }  
  21.     cvNamedWindow("source",0);  
  22.     cvShowImage("source",image0);  
  23.     cvNamedWindow("cvHoughCircles",0);  
  24.     cvShowImage("cvHoughCircles",image);  
  25.     cvWaitKey(0);  
  26.     return 0;  
  27. }  

运算结果不是太理想:

参考资料:‘

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:修改一下:

  1. #include "stdafx.h"    
  2. #include "stdafx.h"    
  3. #include "cv.h"    
  4. #include "highgui.h"    
  5. #include <math.h>    
  6. int _tmain(int argc, _TCHAR* argv[])    
  7. {    
  8.     IplImage* image0=cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);    
  9.     //IplImage* image= cvLoadImage("circle.bmp",CV_LOAD_IMAGE_GRAYSCALE);    
  10.     IplImage *image1=cvLoadImage("circle.bmp",1);  
  11.     //IplImage* image=NULL;//    
  12.     //image=cvCreateImage(cvGetSize(image0),IPL_DEPTH_8U,3);     
  13.     CvMemStorage* storage=cvCreateMemStorage(0);    
  14.     //cvSmooth(image0,image,CV_GAUSSIAN,5,5);    
  15.     CvSeq* results=cvHoughCircles(image0,storage,CV_HOUGH_GRADIENT,2,image0->width /10);    
  16.     for(int i=0;i<results->total ;i++)    
  17.     {    
  18.         float* p=(float*) cvGetSeqElem(results,i);    
  19.         CvPoint pt=cvPoint(cvRound(p[0]),cvRound(p[1]));    
  20.         cvCircle(image1,pt,cvRound(p[2]),CV_RGB(255,0,0));    
  21.     }    
  22.     cvNamedWindow("source",0);    
  23.     cvShowImage("source",image0);    
  24.     cvNamedWindow("cvHoughCircles",0);    
  25.     cvShowImage("cvHoughCircles",image1);    
  26.     cvWaitKey(0);    
  27.     return 0;    
  28. }    

结果:


看起来清楚些,可是还是检测的不好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值