OpenCV 实现霍夫圆变换(52)

79 篇文章 0 订阅
78 篇文章 0 订阅
本文介绍了如何在OpenCV中使用HoughCircles函数检测图像中的圆圈,包括理论背景、边缘检测、参数设置以及代码实现,展示了如何通过Hough梯度方法减少噪点并精确检测圆的位置和半径。
摘要由CSDN通过智能技术生成

返回:OpenCV系列文章目录(持续更新中......)

上一篇:OpenCV实现霍夫变换(51)
下一篇:OpenCV 实现重新映射(50)

目标

在本教程中,您将学习如何:

  • 使用 OpenCV 函数 HoughCircles()检测图像中的圆圈。

理论

Hough 圆变换

  • Hough Circle 变换的工作方式与上一教程中介绍的 Hough Line 变换大致相似。
  • 在线路检测情况下,一条线路由两个参数 (r,Q)定义。在圆的情况下,我们需要三个参数来定义一个圆:

    其中 (xcenter,ycenter)定义中心位置(绿点,),r是半径,这让我们可以完全定义一个圆,如下图所示:

  • 为了提高效率,OpenCV 实现了一种比标准 Hough 变换稍微棘手的检测方法:Hough 梯度方法,它由两个主要阶段组成。第一阶段涉及边缘检测和查找可能的圆心,第二阶段为每个候选中心找到最佳半径。有关更多详细信息,请查看《学习 OpenCV》一书或您最喜欢的计算机视觉参考书目
  • 这个程序是做什么的?

  • 加载图像并对其进行模糊处理以减少噪点
  • 将 Hough Circle 变换应用于模糊图像。
  • 在窗口中显示检测到的圆圈。

C++代码

我们将要解释的示例代码可以从这里下载。可以在此处找到一个稍微花哨的版本(显示用于更改阈值的跟踪栏)。

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
 
using namespace cv;
using namespace std;
 
int main(int argc, char** argv)
{
 const char* filename = argc >=2 ? argv[1] : "smarties.png";
 
 // Loads an image
 Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
 
 // Check if image is loaded fine
 if(src.empty()){
 printf(" Error opening image\n");
 printf(" Program Arguments: [image_name -- default %s] \n", filename);
 return EXIT_FAILURE;
 }
 
 Mat gray;
 cvtColor(src, gray, COLOR_BGR2GRAY);
 
 medianBlur(gray, gray, 5);
 
 vector<Vec3f> circles;
 HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
 gray.rows/16, // change this value to detect circles with different distances to each other
 100, 30, 1, 30 // change the last two parameters
 // (min_radius & max_radius) to detect larger circles
 );
 
 for( size_t i = 0; i < circles.size(); i++ )
 {
 Vec3i c = circles[i];
 Point center = Point(c[0], c[1]);
 // circle center
 circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
 // circle outline
 int radius = c[2];
 circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
 }
 
 imshow("detected circles", src);
 waitKey();
 
 return EXIT_SUCCESS;
}

解释

我们使用的图像可以在这里找到

加载图像:

 const char* filename = argc >=2 ? argv[1] : "smarties.png";
 
 // Loads an image
 Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
 
 // Check if image is loaded fine
 if(src.empty()){
 printf(" Error opening image\n");
 printf(" Program Arguments: [image_name -- default %s] \n", filename);
 return EXIT_FAILURE;
 }

将其转换为灰度:

 Mat gray;
 cvtColor(src, gray, COLOR_BGR2GRAY);

应用中值模糊以减少噪点并避免误圆检测:

 medianBlur(gray, gray, 5);

继续应用 Hough Circle 变换:

 vector<Vec3f> circles;
 HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
 gray.rows/16, // change this value to detect circles with different distances to each other
 100, 30, 1, 30 // change the last two parameters
 // (min_radius & max_radius) to detect larger circles
 );
  • 带有参数:
    • 灰色:输入图像(灰度)。
    • circles:存储 3 个值集的向量:xc1,yc1 对于每个检测到的圆。
    • HOUGH_GRADIENT:定义检测方法。目前,这是 OpenCV 中唯一可用的。
    • dp = 1:分辨率的倒比。
    • min_dist = gray.rows/16:检测到的中心之间的最小距离。
    • param_1 = 200:内部 Canny 边缘检测器的上限阈值。
    • param_2 = 100*:中心检测的阈值。
    • min_radius = 0:要检测的最小半径。如果未知,则将零作为默认值。
    • max_radius = 0:要检测的最大半径。如果未知,则将零作为默认值。

绘制检测到的圆圈:

 for( size_t i = 0; i < circles.size(); i++ )
 {
 Vec3i c = circles[i];
 Point center = Point(c[0], c[1]);
 // circle center
 circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
 // circle outline
 int radius = c[2];
 circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);
 }

你可以看到,我们将用红色画圆圈,用一个小绿点画中心

显示检测到的圆圈并等待用户退出程序:

 imshow("detected circles", src);
 waitKey();

结果

使用测试图像运行上述代码的结果如下所示:


参考文献:

1、《Hough Circle Transform》------Ana Huamán

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚梦者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值