OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Drawing an ellipse in OpenCV is straightforward using the ellipse()
function. Let's go through a step-by-step tutorial on how to do this.
First, ensure you have OpenCV installed:
pip install opencv-python
Next, import necessary libraries:
import cv2 import numpy as np
For this tutorial, let's create a blank image. You can also load an existing image if you prefer:
# Create a blank image (white background) image = np.ones((500, 500, 3), np.uint8) * 255
Or load an existing image:
# image = cv2.imread('path_to_image.jpg')
You can use cv2.ellipse()
to draw an ellipse:
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
Where:
img
: The image on which the ellipse will be drawn.center
: Center of the ellipse.axes
: Length of the major and minor axes.angle
: Ellipse rotation angle in degrees.startAngle
: Starting angle of the elliptic arc (also in degrees).endAngle
: Ending angle of the elliptic arc.color
: Ellipse color.thickness
(optional): Thickness of the ellipse boundary. If this is set to a negative value (e.g., -1
), it will fill the ellipse.lineType
(optional): Type of the ellipse boundary (like anti-aliased, etc.).shift
(optional): Number of fractional bits in the coordinates of the center and the axes values.Here's an example:
center = (250, 250) axes = (150, 100) # major axis = 150, minor axis = 100 angle = 45 # Ellipse rotation angle start_angle = 0 end_angle = 360 # For a complete ellipse color = (0, 0, 255) # Red color in BGR format thickness = 2 # Set this to -1 if you want to fill the ellipse cv2.ellipse(image, center, axes, angle, start_angle, end_angle, color, thickness)
cv2.imshow('Ellipse', image) cv2.waitKey(0) cv2.destroyAllWindows()
When you run the code, you'll see a window displaying an image with a red ellipse. You can adjust the center, axes, angle, color, and thickness as desired.
That concludes our tutorial! The ellipse()
function in OpenCV is powerful and versatile, allowing for various ellipse styles and orientations by adjusting its parameters.
Python code for drawing ellipses in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw an ellipse (center, axes, angle) cv2.ellipse(img, (200, 150), (50, 30), 30, 0, 360, (0, 255, 0), 2) # Display the image cv2.imshow('Ellipse Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Drawing filled and unfilled ellipses in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a filled ellipse cv2.ellipse(img, (200, 150), (50, 30), 30, 0, 360, (0, 255, 0), -1) # Draw an unfilled ellipse cv2.ellipse(img, (300, 150), (50, 30), 45, 0, 360, (0, 0, 255), 2) # Display the image cv2.imshow('Filled and Unfilled Ellipses', img) cv2.waitKey(0) cv2.destroyAllWindows()
Customizing ellipse appearance in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw an ellipse with customized appearance cv2.ellipse(img, (200, 150), (50, 30), 45, 0, 360, (255, 0, 0), 2, cv2.LINE_AA) # Display the image cv2.imshow('Customized Ellipse', img) cv2.waitKey(0) cv2.destroyAllWindows()
Ellipse drawing functions and parameters in OpenCV:
# OpenCV ellipse function signature cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType)
center
: Center coordinates of the ellipse.axes
: Tuple containing major and minor axis lengths.angle
: Rotation angle of the ellipse.startAngle
: Starting angle of the elliptical arc.endAngle
: Ending angle of the elliptical arc.color
: Color of the ellipse.thickness
: Thickness of the ellipse boundary (-1 for filled).lineType
: Type of line used for drawing.Drawing ellipses with specified color and thickness in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw an ellipse with specified color and thickness cv2.ellipse(img, (200, 150), (50, 30), 45, 0, 360, (0, 0, 255), 3) # Display the image cv2.imshow('Ellipse with Color and Thickness', img) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time ellipse drawing in OpenCV:
import cv2 import numpy as np # Callback function for mouse events def draw_ellipse(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: cv2.ellipse(img, (x, y), (30, 20), 0, 0, 360, (0, 255, 0), 2) cv2.imshow('Real-time Ellipse Drawing', img) # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Set the callback function for mouse events cv2.namedWindow('Real-time Ellipse Drawing') cv2.setMouseCallback('Real-time Ellipse Drawing', draw_ellipse) while True: cv2.imshow('Real-time Ellipse Drawing', img) if cv2.waitKey(1) & 0xFF == 27: # Press 'Esc' to exit break cv2.destroyAllWindows()
Drawing ellipses on feature points in OpenCV:
import cv2 import numpy as np # Load an image img = cv2.imread('image.jpg') # Detect features (example: using Harris corner detection) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) corners = cv2.cornerHarris(gray, 2, 3, 0.04) # Draw ellipses on detected feature points corners = np.int0(corners) for i in corners: x, y = i.ravel() cv2.ellipse(img, (x, y), (10, 10), 0, 0, 360, (0, 255, 0), 2) # Display the image with ellipses on feature points cv2.imshow('Ellipses on Feature Points', img) cv2.waitKey(0) cv2.destroyAllWindows()
Drawing ellipses with specified center, axes, and angle in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw an ellipse with specified center, axes, and angle center = (200, 150) axes = (50, 30) angle = 45 cv2.ellipse(img, center, axes, angle, 0, 360, (0, 255, 0), 2) # Display the image cv2.imshow('Ellipse with Specified Parameters', img) cv2.waitKey(0) cv2.destroyAllWindows()
Drawing concentric ellipses in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw concentric ellipses for i in range(5): cv2.ellipse(img, (200, 150), (i * 20, i * 10), 0, 0, 360, (0, 255, 0), 2) # Display the image cv2.imshow('Concentric Ellipses', img) cv2.waitKey(0) cv2.destroyAllWindows()
Adding transparency to drawn ellipses in OpenCV:
import cv2 import numpy as np # Create a transparent image img = np.zeros((300, 400, 4), dtype=np.uint8) img[:, :, 3] = 255 # Set alpha channel to 255 (fully opaque) # Draw a transparent ellipse cv2.ellipse(img, (200, 150), (50, 30), 45, 0, 360, (0, 255, 0), 2) # Display the image with transparency cv2.imshow('Transparent Ellipse', img) cv2.waitKey(0) cv2.destroyAllWindows()