Find Circles in an Image using OpenCV in Python

The OpenCV platform provides a cv2 library for Python. This can be used for various shape analyses which is useful in Computer Vision. To identify the shape of a circle using OpenCV we can use the cv2.HoughCircles() function. It finds circles in a grayscale image using the Hough transform.

Common Approaches

Some of the common methods to find circles in an image using OpenCV are as follows −

  • Circle detection using Hough transform OpenCV

  • Circle detection using SimpleBlobDetector

Circle Detection Using Hough Transform

The Hough transform is a popular technique for circle detection. It works by transforming the image into a parameter space, where each point represents a possible circle center. The technique then searches for the best parameters to define a circle that fits the image.

Syntax

The cv2.HoughCircles() function has the following syntax ?

cv2.HoughCircles(image, method, dp, minDist, param1, param2, minRadius, maxRadius)

Parameters

  • image: Input grayscale image

  • method: Detection method, typically cv2.HOUGH_GRADIENT

  • dp: Inverse ratio of the accumulator resolution to the image resolution

  • minDist: Minimum distance between the centers of the detected circles

  • param1: Upper threshold for edge detection (optional)

  • param2: Accumulator threshold for center detection (optional)

Example

We will take an image as input, convert it to grayscale, and apply the Hough transform to detect circles ?

import cv2
import numpy as np

# Load image
image = cv2.imread('circles.jpg')
output = image.copy()

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur to reduce noise
gray = cv2.medianBlur(gray, 5)

# Detect circles using HoughCircles
circles = cv2.HoughCircles(
    gray, 
    cv2.HOUGH_GRADIENT, 
    dp=1, 
    minDist=100,
    param1=50,
    param2=30,
    minRadius=10,
    maxRadius=200
)

# If circles are detected
if circles is not None:
    circles = np.round(circles[0, :]).astype("int")
    print(f"Detected {len(circles)} circles:")
    print("Coordinates (x, y, radius):")
    
    # Draw circles on the output image
    for (x, y, r) in circles:
        cv2.circle(output, (x, y), r, (0, 255, 0), 2)
        cv2.circle(output, (x, y), 2, (0, 0, 255), 3)
        print(f"Circle at ({x}, {y}) with radius {r}")

# Display the result
cv2.imshow("Detected Circles", output)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output shows detected circles highlighted in green with red center points ?

Detected 1 circles:
Coordinates (x, y, radius):
Circle at (93, 98) with radius 84

Circle Detection Using SimpleBlobDetector

The SimpleBlobDetector algorithm is another way to detect circles in an image. It detects blobs (regions of interest) based on criteria such as minimum size, circularity, and convexity.

Steps Involved

The steps involved in performing the SimpleBlobDetector algorithm are as follows ?

  • Reading the image using OpenCV's imread() function

  • Converting the input image to grayscale

  • Creating a SimpleBlobDetector object with custom parameters

  • Using the detect() function to detect blobs in the image

  • Drawing circles on the original image using detected blob centers

Example

In this example, we configure the SimpleBlobDetector to detect circular blobs and draw circles around them ?

import cv2

# Load image
image = cv2.imread('circles.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Setup SimpleBlobDetector parameters
params = cv2.SimpleBlobDetector_Params()

# Filter by Area
params.filterByArea = True
params.minArea = 100
params.maxArea = 10000

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.8

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.8

# Create detector with parameters
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs
keypoints = detector.detect(gray)

# Draw detected blobs as circles
output = image.copy()
for keypoint in keypoints:
    x = int(keypoint.pt[0])
    y = int(keypoint.pt[1])
    radius = int(keypoint.size / 2)
    cv2.circle(output, (x, y), radius, (0, 255, 0), 2)
    print(f"Blob detected at ({x}, {y}) with radius {radius}")

print(f"Total blobs detected: {len(keypoints)}")

# Display result
cv2.imshow("SimpleBlobDetector Circles", output)
cv2.waitKey(0)
cv2.destroyAllWindows()

Comparison

Method Best For Advantages Disadvantages
Hough Transform Perfect circles Robust to noise, finds partial circles Many parameters to tune
SimpleBlobDetector Circular blobs Easy to configure, filters by shape May miss imperfect circles

Conclusion

Use cv2.HoughCircles() for detecting perfect circles in images with adjustable parameters for accuracy. SimpleBlobDetector works well for circular blob detection with shape-based filtering. Choose the method based on your specific circle detection requirements.

Updated on: 2026-03-25T06:54:14+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements