Overview
The pixelation effect is achieved by downscaling regions to a reduced resolution and then upscaling back to original size using nearest neighbor interpolation, creating distinctive blocky patterns. This technique provides privacy protection while maintaining object silhouettes.Function Signature
Parameters
Input image as BGR or RGB format numpy array. Shape should be (height, width, 3) or (height, width).
Detection results containing bounding boxes. Each detection must have a ‘bbox’ attribute with (x1, y1, x2, y2) coordinates.
Size of pixelation blocks in pixels. Larger values create more pronounced blocky effects. If None, automatically calculated based on image size. Range: [1, inf]. Default is None (adaptive sizing).
Additional padding around detections as percentage of bounding box dimensions. Range: [0.0, 0.5]. Default is 0.05 (5% padding on each side).
Returns
Modified image with pixelated regions where objects were detected. Original image is modified in-place and also returned.
Examples
Error Handling
This function may raise the following exceptions:
- AssertionError: If input image is not a numpy array.
- AttributeError: If detections don’t have required ‘bbox’ attribute.
- ValueError: If image dimensions are invalid or bounding boxes are malformed.
Notes
- Image is modified in-place for memory efficiency
- Pixel size is automatically clamped to minimum value of 1
- Padding percentage is automatically clamped to range [0.0, 0.5]
- Bounding boxes are automatically clipped to image boundaries
- Small regions (smaller than pixel_size) are skipped to prevent artifacts
- Uses INTER_LINEAR for downscaling (quality) and INTER_NEAREST for upscaling (pixelation effect)
- Processes 11,000+ FPS on 320x240 images with small pixel sizes
- Processes 900+ FPS on 4K images with moderate pixel sizes
- Performance scales inversely with pixel_size (smaller blocks = more operations)
- Uses OpenCV’s SIMD-optimized resize operations for maximum efficiency
- Memory usage is minimal due to in-place ROI processing