Masked Blur
app.shared.masked_blurShared alpha-mask and Gaussian blur rendering used by preview and export.
See Also
UIHandler— applies masked blur to the preview frameExportService— uses the same blur operation when exporting
Layer: Shared utilities. Context: Provides reusable cross-layer helpers without owning application workflow state.
compute_blur_alpha_mask(frame_shape: tuple[int, ...], boxes: Sequence[BBoxViewModel], feather_radius: int = _FEATHER_RADIUS) -> np.ndarray
Compute the combined interior-plus-outward-feather alpha mask.
| PARAMETER | DESCRIPTION |
|---|---|
frame_shape
|
Image shape beginning with
TYPE:
|
boxes
|
Bounding boxes whose interiors will be blurred.
TYPE:
|
feather_radius
|
Outward feather distance in pixels.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
A float32 alpha mask of shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the frame shape or feather radius is invalid. |
Note
Distance transform measures each outside pixel's distance to the union of
box interiors. Alpha reaches zero at feather_radius; with the default
value, the preview footprint includes the same 10-pixel outward zone as
exported blur.
For outside distance \(d\) and feather radius \(r\), the mask uses
Box interiors have \(\alpha=1\); alpha reaches zero at or beyond the feather radius.
Example
mask = compute_blur_alpha_mask(frame.shape, frame_boxes)
apply_masked_blur(frame: np.ndarray, boxes: Sequence[BBoxViewModel], strength: float, feather_radius: int = _FEATHER_RADIUS) -> np.ndarray
Blur the union of box interiors with a smooth outward alpha feather.
| PARAMETER | DESCRIPTION |
|---|---|
frame
|
RGB or BGR image to process.
TYPE:
|
boxes
|
Bounding boxes to blur.
TYPE:
|
strength
|
Requested Gaussian kernel width; converted to an odd width of at least three pixels.
TYPE:
|
feather_radius
|
Outward feather distance in pixels.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
The processed frame. The input frame is blurred in place. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Note
Processing is limited to a padded region of interest; padding includes
both feather distance and Gaussian context to avoid seams at ROI edges.
The non-negative strength is truncated to an integer, changed to the
next odd width when even, and clamped to at least three pixels; call
this width \(k\). OpenCV infers the Gaussian standard deviation from \(k\)
because GaussianBlur receives sigmaX=0 (and the default zero
sigmaY). For \(h=(k-1)/2\), the ROI blur is the discrete Gaussian
convolution
where \(Z\) normalizes the finite kernel to sum to one. For distance \(d\) outside the union of box interiors and feather radius \(r\), the feather alpha is
Box interiors have \(\alpha=1\). Each output channel is
The input frame is updated in place, and pixels with zero alpha are left unchanged.
Example
blurred = apply_masked_blur(frame, tracked_boxes, strength=15.0)
overlay_alpha_mask(frame: np.ndarray, alpha_mask: np.ndarray, color_rgb: tuple[int, int, int] = (255, 48, 48), opacity: float = 0.32) -> np.ndarray
Draw a semi-transparent RGB highlight for a blur alpha mask.
| PARAMETER | DESCRIPTION |
|---|---|
frame
|
RGB image to overlay.
TYPE:
|
alpha_mask
|
Float alpha mask with the same height and width.
TYPE:
|
color_rgb
|
Highlight color in RGB channel order.
TYPE:
|
opacity
|
Maximum overlay opacity from 0 to 1.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
A copy of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If dimensions differ or opacity is outside |
Note
Mask alpha modulates the maximum color opacity, so the feather remains visually gradual instead of appearing as a hard rectangle.
Example
preview = overlay_alpha_mask(frame, mask)