Python, Hough Circle Transformation in OpenCV-cv2.HoughCircles()

created at 08-06-2021 views: 23

This blog will talk about how to use the Hough Circle Transform to find circles in an image. OpenCV uses cv2.HoughCircles() to implement the Hough Circle Transform.

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, the detected circle is half of the original image
  • 20: The minimum distance between the centers of adjacent circles detected (If the parameter is too small, in addition to a real circle, multiple adjacent circles may be detected by mistake. If it is too large, some circles may be missed. )
  • param1: In the case of #HOUGHŠu gradient, it is higher. Two thresholds are passed to the Canny edge detector (the lower one is twice as small).
  • param2: In the case of #HOUGHŠu gradient, it is the accumulator threshold of the center of the detection phase. The smaller it is, the more likely it is to detect the false circle;
  • minRadius: minimum circle radius
  • maxRadius: the maximum radius of the circle, if \<=0, the maximum image size is used. If \<0, return the center where the radius is not found.

1 sample graph

The original picture VS detection circle effect picture is as follows:

As shown in the right figure below, you can see that the 3 outer circles are green, and the center of the red circle is successfully detected;

sample graph

The minimum radius of the circle is adjusted from 25 to 10, and the circle may be detected incorrectly. The effect diagram is as follows:

circle be detected incorrectly

As a result of detecting the circle radius threshold (param2 is set to 35), the minimum circle radius is set to 10, and the circle may also be detected incorrectly. The effect diagram is as follows:

param2 is set to 35

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, in addition to a real circle, multiple adjacent circles may be detected erroneously. If it is too large, some circles may be missed.)
# - param1:In the case of the #HOUGHŠu gradient, it is higher. Two thresholds are passed to the Canny edge detector (the lower one is twice as small).
# - param2:In the case of #HOUGHŠu gradient, it is the accumulator threshold of the center of the detection phase. The smaller it is, the more likely it is to detect the false circle;
# - minRadius:Minimum circle radius, false circle may also be detected
# - maxRadius:The maximum circle radius, if <=0, the maximum image size is used. If <0, return the center where the radius is not found.
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20,
                           param1=50, param2=40, minRadius=0, maxRadius=0)
# The minimum circle radius is not set properly, false circle may also be detected
# 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[0, :]:
    # Draw the outer circle (blue)
    cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)

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

cv2.imshow('detected circles', cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
created at:08-06-2021
edited at: 08-06-2021: