OpenCV-Python教程:30.霍夫圆变换

理论

圆的数学方程是


(xcenter, ycenter)是圆的中心点,r是半径。在这个方程里我们可以看到三个参数,所以我们需要一个3维寄存器来做霍夫变换。而这样效率就很低。所以OpenCV使用了个技巧,霍夫梯度法,使用边的梯度信息。

我们用的函数是cv2.HoughCircles()。有很多参数。

import cv2
import numpy as np

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()


OpenCV-Python教程:30.霍夫圆变换_第1张图片

结果

你可能感兴趣的:(OpenCV-Python教程:30.霍夫圆变换)