Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/tracksdata/nodes/_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,24 +373,26 @@ def move(
if image_shape is not None:
self._crop_overhang(image_shape)

def regionprops(self, spacing: tuple[float, ...] | None = None) -> "RegionProperties":
def regionprops(self, **kwargs) -> "RegionProperties":
"""
Compute scikit-image regionprops for this mask.

The computation is aware of the mask bounding box, so coordinate-based
properties (e.g. centroid, coords) are returned in absolute
image coordinates.

IMPORTANT: When providing an intensity image it should match the bounding box shape, not the actual image shape.

Parameters
----------
spacing : tuple[float, ...] | None
The spacing of the image in each dimension (scale).
**kwargs : dict
Keyword arguments to pass to regionprops.
"""
props = regionprops(
self._mask.astype(np.uint16),
cache=True,
offset=tuple(self._bbox[: self._mask.ndim]),
spacing=spacing,
**kwargs,
)

if len(props) != 1:
Expand Down
11 changes: 11 additions & 0 deletions src/tracksdata/nodes/_test/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def test_mask_regionprops_empty() -> None:
_ = Mask(mask_array, bbox).regionprops()


def test_mask_regionprops_intensity_image() -> None:
"""Regionprops should handle intensity images."""
mask_array = np.array([[False, True], [True, False]], dtype=bool)
bbox = np.array([0, 0, 2, 2])
intensity_image = np.array([[1, 2], [3, 4]])

props = Mask(mask_array, bbox).regionprops(intensity_image=intensity_image)
assert props.intensity_max == 3 # 4 is outside the mask
assert props.intensity_min == 2 # 0 is outside the mask


def test_mask_getstate_setstate() -> None:
"""Test Mask serialization and deserialization."""
mask_array = np.array([[True, False], [False, True]], dtype=bool)
Expand Down
Loading