Hough circle transformation in Python, OpenCV

This blog will learn how to use the HoughCircle transform to find circles in an image. OpenCV implements the HoughCircle transform using cv2.houghCircles ().

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=40, minRadius=25, maxRadius=0)

  • Img: grayscale image to be detected
  • Cv2.HOUGH_GRADIENT: Detection method, Hough gradient
  • 1: The detected circle has the same size as the original image, dp=2, and the detected circle is half of the original image
  • 20: The minimum distance detected between the centers of adjacent circles (if the parameter is too small, it is possible to incorrectly detect multiple adjacent circles in addition to one real circle. If it’s too big, you might miss some circles.)
  • Param1: It is higher in the case of #HOUGH š u gradient. Two thresholds are passed to the Canny edge detector (the lower one is twice as small).
  • Param2: In the case of the #HOUGH STALker gradient, this is the multiplicator threshold for detecting the center of the circle of the stage. The smaller it is, the more likely it is to detect false circles;
  • MinRadius: indicates the minimum circle radius
  • MaxRadius: Maximum circle radius, if <=0, the maximum image size is used. If <0, the center of the radius not found is returned.

1. The rendering

The original picture VS detection circle effect is as follows:

As can be seen in the figure on the right below, the three outer circles are green and the center of the circle is red, which has been successfully detected. If the minimum radius of a circle is set from 25 to 10, the circle may be incorrectly detected. The result of detecting the threshold value of the circle radius (param2 set to 35), and the minimum circle radius set to 10, may also incorrectly detect the circle, as shown in the following image:

2. The source code

# Hough circle detection

import cv2
import numpy as np

cimg = cv2.imread('opencv_logo_350.jpg')
cv2.imshow("origin", cimg)
cv2.waitKey(0)
img = cv2.cvtColor(cimg,cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(img, 5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

# -img: Grayscale image to be detected
# -cv2.HOUGH_GRADIENT: Detection method, Hough gradient
# -1: The detected circle has the same size as the original image, dp=2, the detected circle is half of the original image
# -20: The minimum distance from the center of the detected circle (if the parameter is too small, it is possible to incorrectly detect multiple adjacent circles in addition to one real circle. If it's too big, you might miss some circles.)
# -param1: It is higher in the case of #HOUGH š u gradient. Two thresholds are passed to the Canny edge detector (the lower one is twice as small).
# -param2: In the case of the #HOUGH STALker gradient, this is the multiplicator threshold for detecting the center of the circle of the stage. The smaller it is, the more likely it is to detect false circles;
# -minradius: minimum circle radius. False circles may also be detected
# -maxradius: maximum circle radius, if <=0, the maximum image size is used. If <0, the center of the radius not found is returned.
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.20,
                           param1=50, param2=40, minRadius=0, maxRadius=0)
False circles may also be detected if the minimum circle radius is set improperly
# circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20,
# param1=50, param2=40, minRadius=0, maxRadius=0)
# circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20,
# param1=50, param2=30, minRadius=10, maxRadius=0)

circles = np.uint16(np.around(circles))

print(len(circles))
print(circles)
for i in circles[0And:] :# Draw the outer circle (blue)
    cv2.circle(cimg, (i[0], i[1]), i[2], (0.255.0), 2)

    # Draw center of circle (red)
    cv2.circle(cimg, (i[0], i[1]), 2, (0.0.255), 3)

cv2.imshow('detected circles', cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

reference

  • Docs.opencv.org/3.0-beta/do…