5 Best Ways to Draw Filled Ellipses in OpenCV Using Python

πŸ’‘ Problem Formulation: Drawing filled ellipses is a common requirement in image processing tasks with OpenCV in Python. It finds applications in graphics design, data visualization, and UI development. You start with a blank image or a loaded image and you want to add a filled ellipse to it, specifying the size, location, rotation, and color. We aim to draw a filled ellipse with OpenCV’s ellipse() function, where the input parameters define the ellipse’s appearance and the output is a matrix representation of the image with the ellipse drawn on it.

Method 1: Basic Use of cv2.ellipse()

OpenCV’s cv2.ellipse() function allows drawing a filled ellipse by setting the thickness parameter to -1. This function requires the center coordinates, axes lengths, angle of rotation, start and end angle, color, and thickness to draw an ellipse on an image.

Here’s an example:

import numpy as np
import cv2

# Create a blank image
image = np.zeros((300, 300, 3), dtype='uint8')

# Define the parameters for the ellipse
center_coordinates = (150, 150)
axes_length = (100, 50)
angle = 30
start_angle = 0
end_angle = 360
color = (255, 0, 0)  # Blue color in BGR
thickness = -1  # Fill the ellipse

# Draw the filled ellipse
cv2.ellipse(image, center_coordinates, axes_length, angle,
            start_angle, end_angle, color, thickness)

# Display the image
cv2.imshow('Filled Ellipse', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output of this code is an image window displaying a blue filled ellipse on a black background.

This code snippet sets up a black image, defines an ellipse’s parameters, and then draws it filled with blue color. Using a thickness of -1 signals OpenCV’s drawing function to fill the shape.

Method 2: Using cv2.ellipse() with ALPHA channel

When you want an ellipse with transparency, using the ALPHA channel is required. Creating a four-channel image with an ALPHA channel allows for the filled ellipse to be semi-transparent, which can be useful for overlaying on other images.

Here’s an example:

import numpy as np
import cv2

# Create a blank image with an ALPHA channel
image = np.zeros((300, 300, 4), dtype='uint8')

# Define the parameters for the semi-transparent ellipse
color = (0, 255, 0, 100)  # Green with 100 as the alpha value for semi-transparency
cv2.ellipse(image, (150, 150), (100, 50), 0, 0, 360, color, -1)

# Display the image
cv2.imshow('Semi-Transparent Filled Ellipse', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output is an image window displaying a green, semi-transparent filled ellipse on a black background.

This snippet demonstrates how to draw ellipses with different levels of transparency by including an alpha channel in the image matrix and using a four-value color tuple, where the fourth value is the alpha component.

Method 3: Drawing a Filled Ellipse on an Existing Image

To overlay a filled ellipse on a pre-existing image, load the image first and then use the same cv2.ellipse() function to draw on it. This method is used when the background is not a solid color or has other content.

Here’s an example:

import cv2

# Load an existing image
image = cv2.imread('example.jpg')

# Draw a filled ellipse on the loaded image
cv2.ellipse(image, (150, 150), (100, 50), 45, 0, 360, (0, 0, 255), -1)

# Save the result
cv2.imwrite('example_with_ellipse.jpg', image)

The output is the original image with a red filled ellipse drawn at the specified location.

This code snippet demonstrates how to enhance an existing image by adding a filled ellipse overlay with the desired aesthetics.

Method 4: Combining Multiple Filled Ellipses

Multiple filled ellipses can be combined on the same image to create complex shapes or patterns. Each call to cv2.ellipse() with thickness set to -1 will add a new filled ellipse to the image.

Here’s an example:

import numpy as np
import cv2

# Create a blank image
image = np.zeros((300, 300, 3), dtype='uint8')

# Define and draw multiple ellipses
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
centers = [(100, 100), (200, 100), (150, 200)]
for center, color in zip(centers, colors):
    cv2.ellipse(image, center, (80, 40), 0, 0, 360, color, -1)

# Display the image
cv2.imshow('Multiple Filled Ellipses', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output is an image window displaying a combination of different colored filled ellipses on a black background.

This snippet combines multiple ellipses with different colors to create more complex visuals, showcasing OpenCV’s ability to layer shapes in a single image.

Bonus One-Liner Method 5: Drawing a Filled Ellipse with Default Settings

A filled ellipse can also be drawn using default parameters for some arguments, reducing code verbosity for quick tasks where specific details are not necessary.

Here’s an example:

import numpy as np
import cv2

# Create a blank image with default parameters
(cv2.ellipse(np.zeros((300, 300, 3), dtype='uint8'), (150, 150), (75, 50), 0, 0, 360, (255, 255, 255), -1))

The output is an image with a white filled ellipse at the center on a black background.

This one-liner illustrates the minimal amount of code required to create an ellipse when the default values are sufficient.

Summary/Discussion

  • Method 1: Basic Use of cv2.ellipse(). Straightforward and versatile. Works best for solid filled ellipses without transparency.
  • Method 2: Using cv2.ellipse() with ALPHA channel. Allows for transparent ellipses. Requires handling of four-channel images.
  • Method 3: Drawing a Filled Ellipse on an Existing Image. Useful for adding shapes to existing content. Results depend on the underlying image properties.
  • Method 4: Combining Multiple Filled Ellipses. Ideal for creating complex patterns. The processing may become intensive with a large number of shapes.
  • Bonus Method 5: Drawing a Filled Ellipse with Default Settings. Quick and easy, but less flexible. Best for default settings or simple tasks.