Sub topic 10
Lines, Rectangles, and Circles with OpenCV
This chapter assumes that you know basic Python, can install packages with pip, and understand NumPy arrays and image coordinates. It also assumes that you can load an image with cv2.imread. OpenCV drawing functions modify an image array in place, so work on a copy when you need to keep the original unchanged.
Drawing conventions
An OpenCV color image is usually a NumPy array with shape (height, width, 3). Pixel positions use (x, y) coordinates in drawing functions:
xincreases from left to right.yincreases from top to bottom.- The top-left pixel is
(0, 0). - Color tuples use BGR order, not RGB order.
| Color | BGR tuple |
|---|---|
| Blue | `(255, 0, 0)` |
| Green | `(0, 255, 0)` |
| Red | `(0, 0, 255)` |
| White | `(255, 255, 255)` |
The thickness argument is measured in pixels. A positive value creates an outline. A thickness of -1 fills the shape when the function supports filling.
Drawing lines
cv2.line draws a line between two points:
cv2.line(image, start_point, end_point, color, thickness)The endpoint coordinates are included in the drawing operation. This example draws a blue diagonal across the image:
import cv2
image = cv2.imread("photo.jpg")
if image is None:
raise FileNotFoundError("Could not read photo.jpg")
canvas = image.copy()
height, width = canvas.shape[:2]
cv2.line(
canvas,
(0, 0),
(width - 1, height - 1),
(255, 0, 0),
3,
)
cv2.imwrite("line.jpg", canvas)Using width - 1 and height - 1 keeps the endpoint inside the array. OpenCV clips parts of a line that extend outside the image, but explicit bounds make the intent clearer.
Drawing rectangles
cv2.rectangle takes opposite corners. The first point is commonly the top-left corner, and the second is the bottom-right corner:
cv2.rectangle(image, top_left, bottom_right, color, thickness)Use a positive thickness for a border. Use -1 for a filled rectangle.
import cv2
image = cv2.imread("photo.jpg")
if image is None:
raise FileNotFoundError("Could not read photo.jpg")
canvas = image.copy()
cv2.rectangle(canvas, (50, 50), (250, 200), (0, 255, 0), 2)
cv2.rectangle(canvas, (300, 50), (500, 200), (0, 0, 255), -1)
cv2.imwrite("rectangles.jpg", canvas)The first rectangle has a green outline. The second has a filled red interior. If a filled shape covers important image content, draw it on a separate overlay instead of directly on the working image.
Drawing circles
cv2.circle takes a center point, radius, color, and thickness:
cv2.circle(image, center, radius, color, thickness)The center and radius are in pixels. A negative thickness fills the circle.
import cv2
image = cv2.imread("photo.jpg")
if image is None:
raise FileNotFoundError("Could not read photo.jpg")
canvas = image.copy()
cv2.circle(canvas, (150, 125), 40, (0, 0, 255), -1)
cv2.imwrite("circle.jpg", canvas)This draws a filled red circle at (150, 125). If the center is close to an edge, part of the circle may lie outside the image and be clipped.
Preserve the original
Drawing changes the array immediately. Use .copy() before drawing if you need both versions. Always check the return value of cv2.imwrite in production code when failed output must be detected.
Exercise
Which BGR tuple draws a red circle, and what thickness fills it?
Show answer
Use (0, 0, 255) for red and -1 for a filled circle.