zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Opencv原理代码 实现 函数 利用 进行 检测
2023-09-11 14:15:39 时间

霍夫圆变换的基本原理与霍夫线变换原理类似。利用霍夫变换检测直线的相关知识可以参考我的另一篇博文,链接 https://blog.csdn.net/wenhao_ir/article/details/51774444

对直线检测而言,一条直线可由极坐标参数r和θ确定,对于圆来说,则需要三个参数来确定一个圆(为什么是三个,圆心坐标需要两个参数,还要加上一个半径)。标准霍夫圆变换就依据这个思路将直角坐标转换到描述圆的三维度空间中,然后使用这三个维度进行累加度量(投票),根据投票的结果判断是否为圆。

由于是三个参数,所以相当于转换到了三维空间,这样带来的问题就是计算量是几何级增大的,所以我们不用这个思路来实现圆检测法。为了克服这个问题,便有了霍夫梯度法来检测圆的算法。

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

如果想了解霍夫梯度法的算法原理及其优点和缺点,可以参考下面这两篇文章:

http://t.zoukankan.com/jsxyhelu-p-7192711.html

https://blog.csdn.net/DIPDWC/article/details/117513601

由于上面的第一篇文章并不是CSDN的,容易出现网页丢失的情况,所以博主把网页保存到了本地,大家如果打不开了,可以下载下来浏览,百度网盘下载链接如下:https://pan.baidu.com/s/1qSXnIkLGq3kSsePebqnWUw?pwd=t0w6 

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-源图像,要求是8 位、单通道、灰度输入图像。

circles-检测到的圆的三个参数就存在这个动态数组中,数组的每个成员包含三个参数,分别为圆心的x坐标、圆心的y坐标和圆的半径。

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

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

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

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

param2 -圆心阈值参数。圆心是通过投票得出的,那么多少票才算是圆心呢?这个值就是确定这个问题的。

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

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

接下来上示例代码:

代码中用到的图像的下载链接:https://pan.baidu.com/s/1bo6hEft

//博主微信/QQ 2487872782
//有问题可以联系博主交流
//有图像处理开发需求请联系博主
//图像处理技术交流QQ群 271891601

//OpenCV版本:3.0
//VS版本:2013

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>


using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	cv::Mat srcImage = imread("F:/material/images/P0043-circle.jpg");
	if (!srcImage.data)
		return -1;
	cv::imshow("srcImage", srcImage);
	// 转换为灰度图像
	cv::Mat src_gray;
	cvtColor(srcImage, src_gray, CV_BGR2GRAY);
	// 高斯平滑滤波
	GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
	vector<Vec3f> circles;
	// 霍夫圆检测
	HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT,
		1, src_gray.rows / 8, 200, 100, 0, 0);
	// 将得到的结果绘图
	for (size_t i = 0; i < circles.size(); i++)
	{
		Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
		int radius = cvRound(circles[i][2]);
		// 绘制圆中心
		circle(srcImage, center, 3, Scalar(0, 255, 0), -1, 8, 0);
		// 绘制圆轮廓
		circle(srcImage, center, radius, Scalar(120, 120, 120), 3, 8, 0);
	}
	cv::imshow("HoughResult", srcImage);
	cv::waitKey(0);
	return 0;
}

运行结果如下图所示

注意:利用OpenCV的函数HoughCircles()实现霍夫梯度法圆检测时参数dp的值对于最终结果影响是挺大的,详情见博文 https://blog.csdn.net/wenhao_ir/article/details/125140993