您的位置:首页 > 编程语言

利用霍夫梯度法进行圆检测的原理概要及OpenCV代码实现

2016-06-29 17:07 856 查看
霍夫圆变换的基本原理与霍夫线变换原理类似,对直线检测而言,一条直线可由极坐标参数r和θ确定,对于圆来说,则需要三个参数来确定一个圆(为什么是三个,圆心坐标需要两上参数,还要加上一个半径)。标准霍夫圆变换还是将直角坐标转换到描述圆的三维度空间中,然后使用这三个维度进行累加度量(投票),根据投票的结果判断是否为圆。要理解这段话,请先看我的博文:利用霍夫变换做直线检测的原理及OpenCV代码实现

但是转换到三维度空间,很明显,计算量是几何级增大,所以我们不可能用这个来实现圆检测法,所以出现了霍夫梯度法来检测圆的算法。

霍夫梯度法的思路:第一步根据每个点的模向量来找到圆心,这样三维的累加平面就转化为二维累加平面;第二步根据所有候选中心的边缘非零像素对其的支持程度来确定半径。

如果想了解霍夫梯度法的算法原理及其优点和缺点,可以参考博文找圆算法((HoughCircles)总结与优化

OpenCV中提供了函数HoughCircles来实现霍夫梯度法,其函数原型如下:

void HoughCircles( InputArray image, OutputArray circles,int method, double dp, double minDist,double param1 = 100, double param2 = 100,int minRadius = 0, int maxRadius = 0 );

官方解释如下:

image – 8-bit, single-channel, grayscale input image. 
circles – Output vector of found circles. Each vector is encoded as a 3-element floating-point vector (x, y, radius) . 
method – Detection method to use. Currently, the only implemented method is CV_HOUGH_GRADIENT , which is basically 21HT , described in [Yuen90]. 
dp – Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height. 
minDist – Minimum distance between the 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. 
param1 – First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny() edge detector (the lower one is twice smaller). 
param2 – Second method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding
to the larger accumulator values, will be returned first. 
minRadius – Minimum circle radius. 
maxRadius – Maximum circle radius. 

我翻译如下:

image-源图像

circles-检测到的圆的参数就存在这个动态数组中,数组的每个成员包含三个参数,也就是这是一个二维数组

method-检测方法选择参数,目前只支持 CV_HOUGH_GRADIENT这种方法,即霍夫梯度法

dp-累加器图像分辨率的反比。要理解这个,必须举例子,比如若dp==1,那么累加器的分辨率与原图像一样,如果dp==2,那么累加器的分辨率只有原图像的一半。可见,这个值越大,累加器的分辨率反而越低,所以这是个反比值。

minDist-检测到圆的圆心之间的最小距离,显然,这个值越小,伪圆可能越多,而这个值越大,则有越多的圆被漏检。

param1-函数HoughCircles是用到了Canny作边缘检测的,而Canny算法中的滞后阈值法要求设定高低两个阈值,这个参数就是设定这个高阈值的,而低阈值为这个高阈值的二分之一。

param2 -圆心阈值参数,我前面已经说了(或者大家可以参考利用霍夫变换做直线检测的原理及OpenCV代码实现),圆心是通过投票得出的,那么多少票才算是圆心呢?这个值就是确定这个问题的。

minRadius -检测到的圆的最小半径

maxRadius-检测到的圆的最大半径

接下来,上代码(代码中用到的图像的下载链接http://pan.baidu.com/s/1bo6hEft

相关内容已迁移到帖子 http://opencv66.net/thread-36-1-1.html

相关内容已迁移到帖子 http://opencv66.net/thread-36-1-1.html

相关内容已迁移到帖子 http://opencv66.net/thread-36-1-1.html

代码运行结果如下图所示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息