Circle Detection using OpenCV | Python
Last Updated :
11 Aug, 2025
Circle detection is a powerful computer vision technique with applications in fields like biomedicine (e.g., detecting iris in an eye or identifying white blood cells), robotics, quality inspection and object tracking.
OpenCV provides Hough Circle Transform, an algorithm similar to line detection but adapted to identify circular shapes.
Basics of Circle Detection
A circle can be described by the following equation:

A circle in a 2D image has three parameters:
- h: x-coordinate of the circle’s center
- k: y-coordinate of the circle’s center
- r: radius of the circle
If we fix a point (x, y) on an image, finding a circle involves searching for (h, k, r) in a 3D search space.
The classical Hough Transform for Circles:
- Pre-process the image: Apply blurring, convert to grayscale and detect edges to highlight circular shapes.
- Accumulator Matrix: Imagine a 3D array storing possible (h, k, r) values for circles found in the image.
- Voting: Edge pixels "vote" for potential circle centers and radii.
- Pick best candidates: The highest votes correspond to most likely circles.
OpenCV’s HoughCircles() Function
Instead of manually filling a 3D matrix, OpenCV uses an optimized approach called HOUGH_GRADIENT, which leverages edge gradients for much faster circle detection.
Function Syntax:
cv2.HoughCircles(image, method, dp, minDist, param1=100, param2=100, minRadius=0, maxRadius=0)
Parameters:
- cv2.HOUGH_GRADIENT: Detection method.
- dp: Inverse ratio of accumulator resolution to the image resolution. dp=1 means same resolution.
- minDist: Minimum distance between detected circle centers (prevents overlapping detections).
- param1: Higher threshold for the Canny edge detector (lower threshold is set to param1/2).
- param2: Accumulator threshold for circle detection. Higher values --> fewer detections.
- minRadius: Minimum circle radius.
- maxRadius: Maximum circle radius.
Example: Detecting Circles in an Eye Image
Python
import cv2
import numpy as np
# Read image
img = cv2.imread("eyes.jpg")
output = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Reduce noise
gray = cv2.medianBlur(gray, 5)
# Detect circles
circles = cv2.HoughCircles(
gray,
cv2.HOUGH_GRADIENT,
dp=1,
minDist=100,
param1=100,
param2=40,
minRadius=30,
maxRadius=60
)
# Draw only the first detected circle
if circles is not None:
circles = np.uint16(np.around(circles))
x, y, r = circles[0][0]
cv2.circle(output, (x, y), r, (0, 255, 0), 2) # Circle outline
cv2.circle(output, (x, y), 2, (0, 0, 255), 3) # Center point
# Show result
cv2.imshow('Detected Circle', output)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

Explanation:
- img.copy(): duplicates image to draw on without changing the original..
- cv2.cvtColor(img, cv2.COLOR_BGR2GRAY): Converts image from color (BGR) to grayscale.
- cv2.medianBlur(gray, 5): Applies a median blur with a kernel size of 5 to remove noise and smooth the image. Noise reduction helps avoid false circle detections.
- cv2.HoughCircles(): Detects circles in a grayscale image using edge gradients.
- Checks if circles exist, rounds and converts their values to uint16, then extracts first circle’s center (x, y) and radius.
- cv2.circle(): Draws the circle’s green outline and a small red center dot.
- cv2.waitKey(0): Waits indefinitely until a key is pressed.
- cv2.destroyAllWindows(): Closes all OpenCV windows.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice