Skip to content

FilterROIBySize

eitprocessing.roi.filter_by_size

FilterROIBySize dataclass

FilterROIBySize(
    *,
    min_region_size: int = DEFAULT_MIN_REGION_SIZE,
    connectivity: Literal[1, 2] | ndarray = 1
)

Class for labeling and selecting connected regions in a PixelMask.

This dataclass identifies and labels regions of interest (ROIs) in a PixelMask. You can specify the minimum region size and the connectivity structure.

Connectivity

For 2D images, connectivity determines which pixels are considered neighbors when labeling regions. - 1-connectivity (also called 4-connectivity in image processing): Only directly adjacent pixels (up, down, left, right) are considered neighbors. - 2-connectivity (also called 8-connectivity in image processing): Both directly adjacent and diagonal pixels are considered neighbors.

The default value is 1-connectivity.

If a custom array is provided, it must be a boolean or integer array specifying the neighborhood structure. See the documentation for scipy.ndimage.label: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html

PARAMETER DESCRIPTION
min_region_size

Minimum number of pixels in a region for it to be considered an ROI. Defaults to 10.

TYPE: int DEFAULT: DEFAULT_MIN_REGION_SIZE

connectivity

Connectivity type or custom array. Defaults to 1.

TYPE: Literal[1, 2] | ndarray DEFAULT: 1

apply

apply(mask: PixelMask) -> PixelMask

Identify connected regions in a PixelMask, filter them by size, and return a combined mask.

This method:

  1. Converts the input PixelMask into a binary representation where all non-NaN values are treated as part of a region and NaNs are excluded.
  2. Labels connected components using the specified connectivity structure.
  3. Keeps only those connected regions whose pixel count is greater than or equal to self.min_region_size.
  4. Combines the remaining regions into a single PixelMask.
PARAMETER DESCRIPTION
mask

Input mask where non-NaN pixels are considered valid region pixels. NaNs are treated as excluded/background.

TYPE: PixelMask

RETURNS DESCRIPTION
PixelMask

A new PixelMask representing the union of all regions that meet the min_region_size criterion.

TYPE: PixelMask

RAISES DESCRIPTION
RuntimeError

If no connected regions meet the size threshold (e.g., mask is empty, all regions are too small, or connectivity is too restrictive).