Sub topic 14
Semi-transparent overlays
Prerequisites and the blending model
Assume you know basic Python, NumPy array slicing, and OpenCV image loading. You should also know that OpenCV stores color images as NumPy arrays in BGR order. Pixel coordinates for drawing use (x, y), while array indexing uses [y, x].
A semi-transparent overlay is a second image layer drawn on top of the original. The overlay can highlight an object without completely hiding the pixels below it. OpenCV blends the two layers with cv2.addWeighted.
For each pixel, the result is calculated as:
result = overlay × alpha + original × (1 - alpha)
The alpha value controls the overlay strength:
| Alpha value | Appearance |
|---|---|
| `0.0` | Only the original image is visible |
| `0.4` | The overlay is visible but partly transparent |
| `1.0` | Only the overlay is visible |
The two images must have the same width, height, and number of channels. Their data types should also be compatible.
Drawing on a separate overlay
Start by copying the original image. Draw filled shapes on the copy, then blend that copy with the original. Drawing on a separate array preserves the unmodified image and makes it easy to change the overlay strength.
import cv2
image = cv2.imread("photo.jpg")
if image is None:
raise FileNotFoundError("Could not read photo.jpg")
overlay = image.copy()
# OpenCV colors use BGR order: green is (0, 255, 0).
cv2.rectangle(
overlay,
(50, 50),
(250, 200),
(0, 255, 0),
thickness=-1,
)
alpha = 0.4
blended = cv2.addWeighted(
overlay,
alpha,
image,
1.0 - alpha,
0.0,
)
if not cv2.imwrite("highlighted.jpg", blended):
raise IOError("Could not save highlighted.jpg")A thickness of -1 fills the rectangle. If you use a positive thickness, only the border is drawn. You can add several shapes to overlay before blending:
cv2.circle(overlay, (320, 180), 45, (0, 0, 255), -1)
cv2.putText(
overlay,
"Target",
(50, 40),
cv2.FONT_HERSHEY_SIMPLEX,
1.0,
(255, 255, 255),
2,
cv2.LINE_AA,
)The text and circle become transparent by the same amount as the rectangle because they are all part of the overlay layer.
Common errors and design choices
Keep the original image unchanged when you may need it later. A statement such as cv2.rectangle(image, ...) modifies image in place. Use image.copy() when the annotation should be reversible.
Do not confuse alpha blending with transparency stored in a fourth channel. cv2.addWeighted creates a final three-channel image. It does not create or preserve an alpha channel for later compositing.
A large filled region can reduce readability even with a small alpha value. Use contrasting colors and choose an alpha that keeps important image details visible. For a subtle highlight, values around 0.2 to 0.4 are often useful. For a strong mask, use a larger value.
Exercise
Exercise
Why should you draw the filled rectangle on image.copy() instead of directly on image before calling cv2.addWeighted?
Show answer
A copy keeps the original pixels unchanged. The copied image can hold the shapes for blending, while the original remains available for comparison, repeated processing, or another output.