OpenCV-Python -- Hough Circle Transform

学习目标

  • 我们使用霍夫变换寻找图像中的圆。
  • 学习函数:cv2.HoughCircles().

理论

圆的表达式通常为: ( x − x c e n t e r ) 2 + ( y − y c e n t e r ) 2 = r 2 (x - x_{center})^2 + (y - y_{center})^2 =r^2 (xxcenter)2+(yycenter)2=r2,其中 ( x c e n t e r , y c e n t e r ) (x_{center},y_{center}) (xcenterycenter)是圆的中心, r r r是圆的半径。从该方程中,我们可以看到3个参数,所以我们需要3D累加器,这可能非常低效。而OpenCV使用更加棘手的方法,霍夫梯度方法(Hough Gradient Method),使用边界的梯度信息。

函数为: cv2.HoughCircles(). 示例代码如下,

    img = cv2.imread('opencv_logo.png', 0)
    img = cv2.medianBlur(img, 5)
    cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

    circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20,
                               param1=50, param2=30, minRadius=0, maxRadius=0)

    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        # draw the outer circle
        cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
        # draw the center of the circle
        cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)

    cv2.imshow('detected circles', cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

结果如下:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值