OpenCV Hough Circles
This tutorial will discuss detecting circles in an image using the HoughCircles()
function of OpenCV in Python.
Use the HoughCircles()
Function of OpenCV to Detect Circles in an Image in Python
We can use the HoughCircles()
function of OpenCV to detect circles present in an image. The HoughCircles()
function uses the Hough transform to find the circles present in a grayscale image.
Hough transform extracts features from an image and then, using a voting procedure, determines the shape of the objects present in an image.
The first argument of the HoughCircles()
is the image in which we want to detect circles, and it should be in grayscale. The second argument is the method used for the detection of circles.
The Hough modes contain cv2.HOUGH_STANDARD
, a classical or standard Hough transform, cv2.HOUGH_PROBABILISTIC
is a probabilistic Hough transform and is useful if long linear segments are present in the image, cv2.HOUGH_MULTI_SCALE
, a multi-scale variant of the classical Hough transform, cv2.HOUGH_GRADIENT
, and cv2.GRADIENT_ALT
.
The third argument is the ratio of accumulator resolution and image resolution. If the ratio is 1, the accumulator and image resolution will be equal, and if the ratio is 2, the accumulator’s width and height will become half.
The recommended ratio is 1.5 for cv2.HOUGH_GRADIENT_ALT
method. The fourth argument is the minimum distance between the center of two circles.
The fifth argument is the specific parameter for the first method. In the case of cv2.HOUGH_GRADIENT
and cv2.HOUGH_GRADIENT_ALT
, the fifth argument will be used as the threshold for the Canny edge detector.
The sixth argument is the specific parameter for the second method; in the case of cv2.HOUGH_GRADIENT
, the sixth argument will be used as the threshold for the center of circles.
In the case of cv2.HOUGH_GRADIENT_ALT
, the sixth argument will be used as the perfectness value for the circle. The seventh argument is the minimum circle radius, and the eighth is the maximum circle radius.
For example, let’s detect circles present in an image using the HoughCircles()
function and draw the detected circles and their center on the original image using the circle()
function of OpenCV.
See the code below.
import numpy as np
import cv2 as cv
src_img = cv.imread("blob1.jpg", 0)
color_img = cv.cvtColor(src_img, cv.COLOR_GRAY2BGR)
circles_img = cv.HoughCircles(
src_img, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0
)
circles_img = np.uint16(np.around(circles_img))
for i in circles_img[0, :]:
cv.circle(color_img, (i[0], i[1]), i[2], (0, 255, 0), 2)
cv.circle(color_img, (i[0], i[1]), 2, (0, 0, 255), 3)
cv.imshow("Original Image", src_img)
cv.imshow("Detected Circles", color_img)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
We can change the parameters of the HoughCircles()
according to the given image. The around()
function of the numpy
library is used to round the circle value to the nearest integer.
We can also change the color of the circles drawn on the image and the circle’s center by changing the fourth argument of the circle()
function, a BGR triplet.