Content from OpenCV-Python Tutorials Translate and arrange by yourself

target ：

Using Hough transform to find circle in image
Using functions cv2.HoughCircles()

principle ：

The expression for a circle is (x−xcenter)2+(y−ycenter)2=r2, The determination of a ring needs three parameters . Then the accumulator of Hough transform must be three-dimensional , But this kind of calculation efficiency is very low .
here opencv The method of using Hoff gradient in , The gradient information of the boundary is used here .

First, the image is processed canny edge detection , For every non in the edge 0 spot , adopt Sobel Local gradient is calculated by the algorithm . Then the gradient direction calculated , It's actually the normal of a circle tangent . Three normals determine the center of a circle , In the same way, the normal passing through the center of the circle is accumulated in the accumulator , We get the judgment of the ring .

cv2.HoughCircles The parameters of the function
cv2.HoughCircles(image, method, dp, minDist, circles, param1, param2, minRadius
image Input image for , Grayscale image required

method Is the detection method , Commonly used CV_HOUGH_GRADIENT

dp To detect the reciprocal of the ratio of the resolution of the accumulator image to the input image , as dp=1, The accumulator has the same resolution as the input image , If dp=2, The accumulator is half the width and height of the input image

minDist Represents the minimum distance between the centers of two circles

param1 There are default values 100, It is method The corresponding parameters of the set detection method , For the current unique method, Hoff gradient method is used cv2.HOUGH_GRADIENT, It means that the canny High threshold of edge detection operator , The low threshold is half of the high threshold

param2 There are default values 100, It is method The corresponding parameters of the set detection method , For the current unique method, Hoff gradient method is used cv2.HOUGH_GRADIENT, It represents the accumulator threshold at the center of the circle in the detection phase , The smaller it is , The more circles you can detect that don't exist at all , And the bigger it is , The circle that can pass the detection is closer to the perfect circle