How to fit the ellipse to an object in an image using OpenCV Python?

We can fit an ellipse to an object using the function cv2.fitEllipse(). The ellipse is inscribed in a rotated rectangle, which is a bounding rectangle with minimum area enclosing the object.

Syntax

The syntax for fitting an ellipse to a contour is

ellipse = cv2.fitEllipse(cnt)

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

Output It returns a tuple in ((x,y), (majorAxis, minorAxis), angle) format where:

  • (x,y) is the coordinates of the center
  • (majorAxis, minorAxis) are the lengths of major and minor axes
  • angle is the rotation angle of the ellipse

To draw an ellipse on the input image, we use

cv2.ellipse(img, ellipse, (0,0,255), 3)

Steps to Fit an Ellipse

Follow these steps to fit an ellipse to an object

Step 1: Import the required library. Make sure you have OpenCV installed.

import cv2
import numpy as np

Step 2: Read the input image and convert it to grayscale.

img = cv2.imread('star1.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Step 3: Apply thresholding to create a binary image.

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

Step 4: Find contours in the binary image.

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

Step 5: Select a contour and fit an ellipse to it.

cnt = contours[0]
ellipse = cv2.fitEllipse(cnt)

Step 6: Draw the ellipse on the original image.

cv2.ellipse(img, ellipse, (0,0,255), 3)

Example 1: Basic Ellipse Fitting

In this example, we detect contours and fit an ellipse to the largest object ?

import cv2
import numpy as np

# Create a sample image with a shape for demonstration
img = np.zeros((300, 400, 3), dtype=np.uint8)
cv2.ellipse(img, (200, 150), (80, 40), 30, 0, 360, (255, 255, 255), -1)

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

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

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

# Select the first contour
if contours:
    cnt = contours[0]
    
    # Fit ellipse to the contour
    ellipse = cv2.fitEllipse(cnt)
    print("Ellipse parameters:", ellipse)
    
    # Draw the fitted ellipse in red
    result_img = img.copy()
    cv2.ellipse(result_img, ellipse, (0, 0, 255), 2)
    
    print("Ellipse fitted successfully")
Number of contours detected: 1
Ellipse parameters: ((200.0, 150.0), (160.0, 80.0), 30.0)
Ellipse fitted successfully

Example 2: Ellipse with Minimum Area Rectangle

This example shows both the fitted ellipse and its enclosing rotated rectangle ?

import cv2
import numpy as np

# Create a sample star-like shape
img = np.zeros((300, 400, 3), dtype=np.uint8)
points = np.array([[200, 50], [220, 120], [280, 120], [240, 160], 
                   [260, 230], [200, 200], [140, 230], [160, 160], 
                   [120, 120], [180, 120]], np.int32)
cv2.fillPoly(img, [points], (255, 255, 255))

# Convert to grayscale and apply threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, 0)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

if contours:
    cnt = contours[0]
    
    # Find minimum area rectangle
    rect = cv2.minAreaRect(cnt)
    box = cv2.boxPoints(rect)
    box = np.int0(box)
    
    # Draw the minimum area rectangle in yellow
    result_img = img.copy()
    cv2.drawContours(result_img, [box], 0, (0, 255, 255), 2)
    
    # Fit and draw ellipse in red
    ellipse = cv2.fitEllipse(cnt)
    cv2.ellipse(result_img, ellipse, (0, 0, 255), 2)
    
    print("Center:", ellipse[0])
    print("Axes lengths:", ellipse[1])
    print("Rotation angle:", ellipse[2])
Center: (200.0, 140.0)
Axes lengths: (180.0, 90.0)
Rotation angle: 0.0

Key Points

  • The contour must have at least 5 points for cv2.fitEllipse() to work
  • The ellipse is always inscribed within the minimum area rotated rectangle
  • The function returns center coordinates, axis lengths, and rotation angle
  • Use proper thresholding to get clean contours for better ellipse fitting

Conclusion

OpenCV's cv2.fitEllipse() function provides an effective way to fit ellipses to object contours. It returns the ellipse parameters including center, axes lengths, and rotation angle, making it useful for shape analysis and object detection tasks.

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

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements