Content from OpenCV-Python Tutorials Translate and arrange by yourself
Using Hough transform to find circle in image
Using functions cv2.HoughCircles()
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
minRadius There are default values 0, Minimum value of circle radius
maxRadius There are default values 0, Maximum value of circle radius
import cv2 import numpy as np img = cv2.imread('19.jpg',0) img =
cv2.medianBlur(img,5) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) circles =
100,maxRadius=200) circles = np.uint16(np.around(circles)) for i in circles[0
,:]:# draw the outer circle cv2.circle(cimg,(i,i),i,(0,255,0),2) #
draw the center of the circle cv2.circle(cimg,(i,i),2,(0,0,255),3)
cv2.imshow('detected circles',cimg) cv2.waitKey(0) cv2.destroyAllWindows()