Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to draw filled ellipses in OpenCV using Python?
To draw a filled ellipse on an image, we use the cv2.ellipse() method. This method accepts different arguments to draw different types of ellipses with various shapes, sizes, and orientations.
Syntax
cv2.ellipse(img, center, axes, angle, start_angle, end_angle, color, thickness)
Parameters
img ? The input image on which the ellipse is to be drawn.
center ? The center coordinate of the ellipse as (x, y).
axes ? A tuple in (major axis length, minor axis length) format.
angle ? The rotation angle of an ellipse in degrees.
start_angle ? The starting angle of the elliptic arc in degrees.
end_angle ? The ending angle of the elliptic arc in degrees.
color ? The color of the ellipse in BGR format (B, G, R).
thickness ? The thickness of the ellipse border line in pixels. To draw a filled ellipse, set thickness = -1.
Return Value ? It returns the input image with the drawn ellipse.
Basic Example
Here's a simple example to draw a filled ellipse on a blank image ?
import cv2
import numpy as np
# Create a blank white image
img = np.ones((400, 600, 3), dtype=np.uint8) * 255
# Define ellipse parameters
center = (300, 200)
axes = (150, 80) # major axis = 150, minor axis = 80
angle = 30 # rotation angle in degrees
start_angle = 0 # full ellipse
end_angle = 360
color = (255, 0, 0) # blue color in BGR
thickness = -1 # filled ellipse
# Draw the filled ellipse
cv2.ellipse(img, center, axes, angle, start_angle, end_angle, color, thickness)
# Display the result (simulated output)
print("Filled ellipse drawn successfully!")
print(f"Center: {center}, Axes: {axes}, Angle: {angle}°")
Filled ellipse drawn successfully! Center: (300, 200), Axes: (150, 80), Angle: 30°
Drawing Multiple Ellipses
You can draw multiple filled ellipses with different properties ?
import cv2
import numpy as np
# Create a blank image
img = np.ones((400, 600, 3), dtype=np.uint8) * 255
# Draw three different filled ellipses
# Yellow ellipse (full)
cv2.ellipse(img, (150, 150), (80, 40), 0, 0, 360, (0, 255, 255), -1)
# Green ellipse (rotated)
cv2.ellipse(img, (300, 150), (60, 100), 45, 0, 360, (0, 255, 0), -1)
# Red ellipse (partial arc)
cv2.ellipse(img, (450, 150), (70, 70), -30, 0, 270, (0, 0, 255), -1)
print("Three filled ellipses with different properties:")
print("1. Yellow: Full ellipse, no rotation")
print("2. Green: Full ellipse, rotated 45°")
print("3. Red: Partial ellipse (270°), rotated -30°")
Three filled ellipses with different properties: 1. Yellow: Full ellipse, no rotation 2. Green: Full ellipse, rotated 45° 3. Red: Partial ellipse (270°), rotated -30°
Key Points
Set thickness = -1 to create a filled ellipse
The axes parameter defines (major_axis_length, minor_axis_length)
Use start_angle = 0 and end_angle = 360 for a complete ellipse
The angle parameter rotates the entire ellipse around its center
Colors are specified in BGR format (Blue, Green, Red)
Comparison of Ellipse Types
| Type | Start Angle | End Angle | Description |
|---|---|---|---|
| Full Ellipse | 0 | 360 | Complete elliptical shape |
| Half Ellipse | 0 | 180 | Semi-elliptical arc |
| Quarter Ellipse | 0 | 90 | Quarter elliptical arc |
Conclusion
The cv2.ellipse() function with thickness=-1 creates filled ellipses in OpenCV. You can control the shape, size, rotation, and color to create various elliptical designs on your images.
