OpenCV核心模块核心提炼(六) —— 圆形检测:Hough圆变换

霍夫圆变换的工作方式与前一个教程中解释的霍夫线变换大致类似。

在线检测情况下,线由两个参数定义(r,\ theta)。在圆圈情况下,我们需要三个参数来定义圆:

C:(x_ {center},y_ {center},r)

在哪里(x_ {center},y_ {center})定义中心位置(格力点)并且[R是半径,这允许我们完全定义一个圆,如下所示:

用Hough变换检测圆的结果

为了提高效率,OpenCV实现了一种比标准Hough变换稍微复杂的检测方法:霍夫梯度法。有关详细信息,请查看学习OpenCV或您最喜欢的计算机视觉参考书目

代码

  1. 这个程序做什么用的?
    • 加载图像并模糊图像以减少噪点
    • 霍夫圆变换应用于模糊图像。
    • 在窗口中显示检测到的圆圈。
  2. 我们将解释的示例代码可以从这里下载。可以在此处找到稍微更漂亮的版本(显示Hough标准和带轨迹栏的概率以更改阈值)。
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;

/** @function main */
int main(int argc, char** argv)
{
  Mat src, src_gray;

  /// Read the image
  src = imread( argv[1], 1 );

  if( !src.data )
    { return -1; }

  /// Convert it to gray
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// Reduce the noise so we avoid false circle detection
  GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );

  vector<Vec3f> circles;

  /// Apply the Hough Transform to find the circles
  HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );

  /// Draw the circles detected
  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 center
      circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
      // circle outline
      circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
   }

  /// Show your results
  namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
  imshow( "Hough Circle Transform Demo", src );

  waitKey(0);
  return 0;
}

说明

  1. 加载图像

    src = imread( argv[1], 1 );
    
    if( !src.data )
      { return -1; }

     

  2. 将其转换为灰度:

    cvtColor ( src , src_gray , CV_BGR2GRAY  );
    
  3. 应用高斯模糊以减少噪音并避免假圆检测:

    高斯模糊( src_gray , src_gray , 尺寸(9 9 ), 2 2  );
    
  4. 继续应用霍夫圆变换:

    vector<Vec3f> circles;
    
    HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, 200, 100, 0, 0 );

    与参数:

    • src_gray:输入图像(灰度)
    • 圆圈:存储3个值的集合的向量:x_ {c},y_ {c},r对于每个检测到的圆圈。
    • CV_HOUGH_GRADIENT:定义检测方法。目前这是OpenCV中唯一可用的
    • dp = 1:分辨率的反比
    • min_dist = src_gray.rows / 8:检测到的中心之间的最小距离
    • param_1 = 200:内部Canny边缘检测器的上限阈值
    • param_2 = 100 *:中心检测的阈值。
    • min_radius = 0:要检测的最小无线电。如果未知,则默认为零。
    • max_radius = 0:要检测的最大半径。如果未知,则默认为零
  5. 绘制检测到的圆圈:

    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 center
       circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
       // circle outline
       circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
     }

    你可以看到我们将用红色绘制圆圈,用小绿点绘制中心

  6. 显示检测到的圆圈:

    namedWindow ( “Hough Circle Transform Demo” , CV_WINDOW_AUTOSIZE  ); 
    imshow ( “Hough Circle Transform Demo” , src  );
    
  7. 等待用户退出程序

    waitKey (0 );
    

结果

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

用Hough变换检测圆的结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值