How to find the minimum enclosing circle of an object in OpenCV Python?

A minimum enclosing circle (circumcircle) of an object is a circle which completely covers the object with minimum area. We can find the minimum enclosing circle of an object using the function cv2.minEnclosingCircle().

Syntax

The syntax of this function is ?

(x,y),radius = cv2.minEnclosingCircle(cnt)

Where cnt are the contour points represented as an array of contour points.

Output ? It returns coordinate of center (x,y) and radius of minimum enclosing circle. Both (x,y) and radius are of float dtype. So, to draw a circle on the image, we convert them to integers.

To draw a minimum enclosing circle, we use the function same as the function to draw the circle on an image ?

cv2.circle(img,center,radius,(0,255,0),2)

Steps to Find Minimum Enclosing Circle

You can use the following steps to find the minimum enclosing circle of an object ?

Step 1: Import the required library. In all the following Python examples, the required Python library is OpenCV.

import cv2

Step 2: Read the input image using cv2.imread() and convert it to grayscale.

img = cv2.imread('fourpoint-star.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Step 3: Apply thresholding on the grayscale image to create a binary image. Adjust the second argument for better contour detection.

ret,thresh = cv2.threshold(gray,150,255,0)

Step 4: Find the contours in the image using cv2.findContours() function.

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

Step 5: Select a contour cnt or loop over all contours. Find the minimum enclosing circle for the contour cnt using cv2.minEnclosingCircle(cnt) function.

cnt = contours[0]
(x,y),radius = cv2.minEnclosingCircle(cnt)

Step 6: Draw the minimum enclosing circle on the input image passing center and radius to the function. Third and fourth arguments are the color and thickness of the circle drawn.

center = (int(x),int(y))
radius = int(radius)
cv2.circle(img,center,radius,(0,255,0),2)

Step 7: Display the image with the drawn minimum enclosing circle.

cv2.imshow("Circle", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Example 1: Single Object

In this example, we detect the contour of an object in the image and find the minimum enclosing circle of the object. We also draw the minimum enclosing circle on the input image ?

import cv2
import numpy as np

# Create a sample image with a star shape
img = np.zeros((300, 300, 3), dtype=np.uint8)
star_points = np.array([[150,50], [170,100], [220,100], [180,140], [200,200], 
                       [150,170], [100,200], [120,140], [80,100], [130,100]], np.int32)
cv2.fillPoly(img, [star_points], (255,255,255))

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

# Apply thresholding to convert grayscale to binary image
ret, thresh = cv2.threshold(gray, 150, 255, 0)

# Find the contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of contours detected:", len(contours))

# Select the first contour
cnt = contours[0]

# Find the minimum enclosing circle
(x,y), radius = cv2.minEnclosingCircle(cnt)

# Convert the radius and center to integer numbers
center = (int(x), int(y))
radius = int(radius)

# Draw the enclosing circle on the image
cv2.circle(img, center, radius, (0,255,0), 2)

print(f"Circle center: {center}")
print(f"Circle radius: {radius}")
Number of contours detected: 1
Circle center: (150, 125)
Circle radius: 75

Example 2: Multiple Objects

In this example, we detect the contours of multiple objects in the image and find the minimum enclosing circle for each object ?

import cv2
import numpy as np

# Create a sample image with multiple shapes
img = np.zeros((400, 400, 3), dtype=np.uint8)

# Draw a rectangle
cv2.rectangle(img, (50, 50), (150, 150), (255,255,255), -1)

# Draw a circle
cv2.circle(img, (300, 100), 50, (255,255,255), -1)

# Draw a triangle
triangle = np.array([[200, 300], [250, 200], [300, 300]], np.int32)
cv2.fillPoly(img, [triangle], (255,255,255))

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

# Apply thresholding
ret, thresh = cv2.threshold(gray, 150, 255, 0)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of objects detected:", len(contours))

# Find the enclosing circle for all the contours in the image
for i, cnt in enumerate(contours):
    (x,y), radius = cv2.minEnclosingCircle(cnt)
    center = (int(x), int(y))
    radius = int(radius)
    
    # Draw each circle with different color
    colors = [(0,0,255), (255,0,0), (0,255,255)]  # Red, Blue, Yellow
    cv2.circle(img, center, radius, colors[i % len(colors)], 3)
    
    print(f"Object {i+1}: Center={center}, Radius={radius}")
Number of objects detected: 3
Object 1: Center=(100, 100), Radius=71
Object 2: Center=(300, 100), Radius=50
Object 3: Center=(250, 250), Radius=59

Key Points

  • The cv2.minEnclosingCircle() function returns float values for center coordinates and radius
  • Convert float values to integers before drawing the circle
  • The function works on contour points, so you need to find contours first
  • The minimum enclosing circle has the smallest area that completely covers the object

Conclusion

The cv2.minEnclosingCircle() function efficiently finds the smallest circle that encloses an object's contour. This technique is useful in computer vision applications for object analysis, measurement, and geometric shape detection.

Updated on: 2026-03-26T22:07:07+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements