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
,maxRadius)
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 =
cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,100, param1=100,param2=30,minRadius=
100,maxRadius=200) 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()

Technology
©2019-2020 Toolsou All rights reserved,
Forbes China Auto rich list : He xiaopengdi 11 Li Xiangdi 14 Li Bindi 15 Change one's mind ! Tesla starts to deliver made in China to European market Model 3 The difference between memory overflow and memory leak , Causes and Solutions Character recognition technology of vehicle license plate based on Neural Network Vue Transfer parameters and receiving of page Jump SparkSQL Achieve partition overlay write 1190 Reverses the substring between each pair of parentheses leetcode Note 14 : The second biggest obstacle to motivating others in R & D management Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type