Circle Detection Using Hough Transform

Hough Transform is a popular technique for detecting circles in an image. Here's how you can use it to detect circles:

  1. First, you need to read in your image and convert it to grayscale. You can use any programming language or library of your choice to achieve this step.
  2. Next, you need to perform edge detection on the grayscale image using an edge detection algorithm such as the Canny edge detector. This will give you an image with edges highlighted.
  3. Once you have the edge image, you can apply the Hough Transform algorithm to detect circles. The Hough Transform works by first creating a Hough space, which is a 2D array with the same dimensions as the input image. For each edge point in the image, a set of circles that pass through that point are voted on in the Hough space. The circles with the most votes are then considered to be the detected circles.
  4. To detect circles using the Hough Transform, you need to set the method parameter of the HoughCircles function to cv2.HOUGH_GRADIENT. This method uses a gradient-based approach to detect circles. You also need to set the dp, minDist, param1, and param2 parameters to appropriate values. These parameters control the resolution of the Hough space, the minimum distance between detected circles, and the threshold for circle detection.

Here's an example code snippet that demonstrates how to use cv2.HoughCircles() to detect circles in an image using the Hough Transform:

Code 1-1 Detecting only vertical lines using Probabilistic Hough Line Transform
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import cv2
import numpy as np
dir = r'D:\\img\\'
imgname = 'coins.jpg'
path = dir  + imgname
imgOriginal = cv2.imread(path, cv2.IMREAD_COLOR)
gray = imgOriginal[:,:,0]
blur = cv2.GaussianBlur(gray, (7,7), 0) 
edges = cv2.Canny(blur, threshold1=100, threshold2=200) 
circles = cv2.HoughCircles(blur,cv2.HOUGH_GRADIENT,1,20,param1=200,param2=30, minRadius=0, maxRadius=0)   
if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        cv2.circle(imgOriginal, (i[0], i[1]), i[2], (0, 255, 0), 2)
        cv2.circle(imgOriginal, (i[0], i[1]), 2, (0, 0, 255), 3)  
    cv2.imshow("Hough Lines", imgOriginal)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
else:
    print('No Lines found')
In this code, we first apply Gaussian blur (Line#8) to remove the noise and then Canny edge detection algorithm (Line#9) is applied to the grayscale image to get the edge image. We then use the cv2.HoughCircles() function to detect circles in the edge image. We set the dp parameter to 1, which means that the resolution of the Hough space is the same as the input image. We set the minDist parameter to 20, which is the minimum distance between the centers of detected circles. We set the param1 parameter to 200 and the param2 parameter to 30, which control the threshold for Canny edge detection. We set the minRadius and maxRadius parameters to 0, which means that we detect circles of any radius. Finally, we draw the detected circles on the original image and display it. The sample output obtained for this code is shown below in Figure 1.
No image
Figure 1: Circle detection using Hough Transform.
You can see in the above figure that not only vertical lines are detected, but also horizontal lines are also detected. If we want horizontal, vertical, and diagonal lines, then we have to change Line#14 to np.pi/4.

OpenCV Tutorials