Overview
Provides efficient mask visualization using optimized single-pass rendering and OpenCV’s accelerated blending operations. Supports both binary mask arrays and polygon coordinate lists, with automatic format detection and conversion for seamless integration.Function Signature
Parameters
Input video frame in BGR format (height, width, 3). Modified in-place with overlaid masks.
Detections object containing mask data. Each detection’s masks attribute can contain binary arrays or polygon coordinate lists.
Opacity level for blending masks with the frame. Range: [0.0, 1.0]. Default is 0.5 (semi-transparent). Value of 1.0 creates opaque masks, 0.0 makes them invisible.
List of BGR color tuples to override default colors. Colors are mapped to unique class_ids in order of appearance. If None, uses default ColorManager colors.
Returns
Frame with masks overlaid using the specified opacity. The input frame is modified in-place for memory efficiency.
Examples
Error Handling
This function may raise the following exceptions:
- ValueError: If binary mask dimensions do not match frame dimensions.
- AttributeError: If detection objects lack required ‘masks’ attribute.
Notes
- Input frame is modified in-place for memory efficiency
- Supports both binary mask arrays (boolean or uint8) and polygon coordinate lists
- Binary masks must match frame dimensions exactly
- Polygon coordinates are automatically converted to binary masks using cv2.fillPoly
- Uses single-pass rendering for optimal performance with multiple masks
- OpenCV’s addWeighted function provides hardware-accelerated blending
- Empty or None mask data is automatically skipped
- Single overlay creation minimizes memory allocation
- Batch blending operation reduces computational overhead
- Direct pixel assignment used for full opacity (opacity=1.0) to bypass blending
- Polygon-to-mask conversion cached within the function scope