Skip to content

Morphology Utils

Boolean-mask morphology and region helpers.

These reproduce the OpenCV morphology operations the analysis pipelines used to call, so mask post-processing stays available without the opencv-python dependency. Dilation is pure NumPy; region labeling uses scikit-image.

dilate_rect(mask, size)

Dilate a boolean mask with a rectangular all-ones kernel.

A rectangular kernel is separable, so the mask is dilated along columns and then along rows, each in one running-sum pass independent of the kernel extent. The result matches OpenCV's rectangular dilation with constant zero-padding. Only odd kernel extents are supported so the anchor is unambiguous.

Parameters:

Name Type Description Default
mask ndarray

Two-dimensional boolean mask to dilate.

required
size tuple[int, int]

Kernel (width, height), both odd.

required

Returns:

Type Description
ndarray

np.ndarray: Dilated boolean mask, same shape as mask.

Raises:

Type Description
ValueError

If mask is not two-dimensional, or either kernel extent is even, not positive, or greater than 65535.

mask_regions(mask, min_area=1)

Return bounding boxes of the connected foreground regions in a boolean mask.

Delegates labeling to skimage.measure.label with full connectivity, so pixels touching along an edge or a corner belong to the same region.

Parameters:

Name Type Description Default
mask ndarray

Two-dimensional boolean mask.

required
min_area int

Minimum number of foreground pixels a region needs to be kept. Defaults to 1.

1

Returns:

Type Description
list[tuple[int, int, int, int]]

list[tuple[int, int, int, int]]: (x, y, width, height) boxes ordered top to bottom, then left to right.

Raises:

Type Description
ImportError

If scikit-image is unavailable.

ValueError

If mask is not two-dimensional or min_area is less than 1.