7.3. Hough圆变换

7.3.1. 目标

在本章中,
-我们将学习使用Hough变换在图像中查找圆。-我们将看到这些功能: cv2.Houghcirles()

7.3.2. 理论

圆在数学上表示为 \((x-x_{{center}})^2 + (y - y_{{center}})^2 = r^2\) 在哪里? \((x_{{center}},y_{{center}})\) 是圆的中心,并且 \(r\) 是圆的半径。从方程中可以看出我们有3个参数,所以我们需要一个三维累加器来进行hough变换,这将是非常无效的。所以OpenCV使用了更复杂的方法, 霍夫梯度法 利用边缘的梯度信息。

我们在这里使用的函数是 cv2.Houghcirles() . 它有大量的论据,这些论据在文献中得到了很好的解释。所以我们直接查密码。

>>> %matplotlib inline
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> import cv2 as cv
>>> img = cv.imread('/cvdata/opencv-logo-white.png',0)
>>> img = cv.medianBlur(img,5)
>>> cimg = cv.cvtColor(img,cv.COLOR_GRAY2BGR)
>>> circles = cv.HoughCircles(img,cv.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
>>>     cv.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
>>>     # draw the center of the circle
>>>     cv.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
>>>
>>> # cv.imshow('detected circles',cimg)
>>> # cv.waitKey(0)
>>> # cv.destroyAllWindows()
>>> plt.imshow(cimg)
<matplotlib.image.AxesImage at 0x7effd74262e8>
../_images/sec03-houghcircles_3_1.png

结果如下:

7.3.3. 额外资源

7.3.4. 练习