connected_components#
- rlemasklib.RLEMask.connected_components(connectivity=4, min_size=1, filter_fn=None)[source][C source]#
Extract connected components from the mask.
- Parameters:
connectivity (int) – 4 or 8, the connectivity of the components. If 4, then only horizontal and vertical connections are considered, if 8, then also diagonal connections are considered.
min_size (int) – the minimum size of a component to return. Small components are ignored.
filter_fn (Optional[Callable[[ndarray, ndarray, ndarray], ndarray]]) – an optional filter function that receives three numpy arrays (areas, bboxes, centroids) and returns a boolean array indicating which components to extract. areas is shape (n,), bboxes is shape (n, 4) with columns (x, y, w, h), centroids is shape (n, 2) with columns (x, y).
- Returns:
A list of RLEMask objects, each representing a connected component of this mask.
Examples
With 4-connectivity, diagonal pixels are separate components (8 components):
.connected_components(connectivity=4) yields 8 componentsWith 8-connectivity, diagonally adjacent pixels are connected (1 component):
.connected_components(connectivity=8) yields 1 componentFilter to only get components with area > 100:
mask.connected_components(filter_fn=lambda a, b, c: a > 100)
Filter to only get components in the left half of the image:
mask.connected_components( filter_fn=lambda areas, bboxes, centroids: centroids[:, 0] < mask.shape[1] / 2 )