• opencv c++ 霍夫圆检测


    1、原理

            a)对某点(x_{0},y_{0}),以其为圆心的圆为无数(一圈圈的圆),将其从x-y平面坐标系上转换到r-θ极坐标系上后,则变成了以r、θ为自变量,x_{0},y_{0}为固定值,x、y为因变量的式子:

            (x-x_{0})^{2} + (y - y_{0})^{2} = r^{2}

           b)其余点作同样操作,可以得到,当半径r为某值r_{0}时,使得三个圆同时交于1点,从而获取这些点构成的圆的圆心,半径(x_{0},y_{0},r)

            

             圆的参数方程:

            x = x_{0}+ r *cos{\Theta}

            y= y_{0}+ r *sin{\Theta}

            注:在实际实现时,会设定一个固定的半径r来进行检测(因为r的范围太大了)。

    2、API

    1. void cv::HoughCircles ( InputArray image,
    2. OutputArray circles,
    3. int method,
    4. double dp,
    5. double minDist,
    6. double param1 = 100,
    7. double param2 = 100,
    8. int minRadius = 0,
    9. int maxRadius = 0
    10. )

    image ——输入的灰度图像
    circles——输出,数据类型为vector (x,y,radius) or (x,y,radius,votes) .
    method ——检测方法
    dp ——累加器分辨率与图像分辨率的反比. 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. For HOUGH_GRADIENT_ALT the recommended value is dp=1.5, unless some small very circles need to be detected.
    minDist ——两个被检测圆的圆心的最小距离,即在这个距离范围内,不会出现第二个被检测出的圆。 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 ——Canny边缘检测的高阈值。
    param2 ——累计阈值,当相交于同一点的圆的个数大于它时,才记录这个被识别到的圆。
    minRadius——圆的最小半径
    maxRadius ——圆的最大半径。If <= 0, uses the maximum image dimension. If < 0, HOUGH_GRADIENT returns centers without finding the radius. HOUGH_GRADIENT_ALT always computes circle radiuses.

     

    3、代码:

            说明:HoughCircles    会基于canny自动二值化图,因此输入灰度图即可,但由于该算法对图像噪点敏感,必须在调用前对灰度图进行降噪处理

    1. void QuickDemo::hough_circle(Mat& image)
    2. {
    3. //霍夫圆检测会基于canny自动二值化图,因此输入灰度图即可,但在传入之前,需要对图像进行降噪。
    4. Mat gray, binary;
    5. cvtColor(image, gray, COLOR_BGR2GRAY);
    6. GaussianBlur(gray, gray, Size(9, 9), 2, 2);
    7. namedWindow("hough gray", WINDOW_FREERATIO);
    8. imshow("hough gray", gray);
    9. vector circles;
    10. double mindist = 2;
    11. double min_r = 10;
    12. double max_r = 200;
    13. HoughCircles(gray, circles, HOUGH_GRADIENT, 1.5, mindist, 100, 100, min_r, max_r);
    14. for (size_t i = 0; i < circles.size(); ++i) {
    15. circle(image, Point(circles[i][0], circles[i][1]), circles[i][2], Scalar(0, 255, 0), 3, 8);
    16. circle(image, Point(circles[i][0], circles[i][1]), 10, Scalar(250, 0, 0), -1, 8);
    17. }
    18. namedWindow("hough circle", WINDOW_FREERATIO);
    19. imshow("hough circle", image);
    20. }

     

  • 相关阅读:
    Linux 下安装MySQL 5.7与 8.0详情
    Docker容器之间的通信
    重新认识mysql
    【云原生之Docker实战】使用Docker部署Linux系统监控平台Netdata
    聚观早报|苹果明年iPhone基带继续由高通提供
    通用任务批次程序模板
    双十二购买护眼台灯亮度多少合适?灯光亮度多少对眼睛比较好呢
    微调 Florence-2 - 微软的尖端视觉语言模型
    阿里云弹性手机购买与配置
    MongoDB3.x创建用户与用户角色
  • 原文地址:https://blog.csdn.net/lucust/article/details/128191293