Skip to content

Commit 2a6ab2a

Browse files
committed
added resize_labels processing function
1 parent d9b4105 commit 2a6ab2a

2 files changed

Lines changed: 41 additions & 30 deletions

File tree

src/napari_tmidas/_tests/test_skimage_filters.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,31 @@
55
adaptive_threshold_bright,
66
invert_image,
77
percentile_threshold,
8-
resize_mask,
98
rolling_ball_background,
109
simple_thresholding,
1110
)
1211

1312

1413
class TestSkimageFilters:
15-
def test_resize_mask_nearest(self):
16-
"""Test resizing a mask with nearest-neighbor interpolation preserves mask integrity."""
17-
mask = np.zeros((10, 10), dtype=np.uint8)
18-
mask[2:8, 2:8] = 1
19-
output_shape = (20, 20)
20-
resized = resize_mask(mask, output_shape=output_shape)
21-
assert resized.shape == output_shape
22-
# Should only contain 0 and 1
23-
assert set(np.unique(resized)).issubset({0, 1})
24-
# Check that the central region is still 1
25-
assert np.sum(resized == 1) > 0
14+
15+
def test_resize_labels(self):
16+
"""Test resizing a label image by scale factor preserves label values and shape."""
17+
from napari_tmidas.processing_functions.skimage_filters import (
18+
resize_labels,
19+
)
20+
21+
label_image = np.zeros((10, 10), dtype=np.uint8)
22+
label_image[2:8, 2:8] = 3
23+
scale_factor = 0.5
24+
scaled = resize_labels(label_image, scale_factor=scale_factor)
25+
expected_shape = tuple(
26+
(np.array(label_image.shape) * scale_factor).astype(int)
27+
)
28+
assert scaled.shape == expected_shape
29+
# Should only contain 0 and 3
30+
assert set(np.unique(scaled)).issubset({0, 3})
31+
# Check that the central region is still present
32+
assert np.sum(scaled == 3) > 0
2633

2734
def test_invert_image_basic(self):
2835
"""Test basic image inversion functionality"""

src/napari_tmidas/processing_functions/skimage_filters.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,39 @@
3434
if SKIMAGE_AVAILABLE:
3535

3636
@BatchProcessingRegistry.register(
37-
name="Resize Mask (Nearest)",
38-
suffix="_resized",
39-
description="Resize a mask using nearest-neighbor interpolation (order=0, anti_aliasing=False) to preserve mask integrity.",
37+
name="Resize Labels (Nearest)",
38+
suffix="_scaled",
39+
description="Resize a label mask or label image by a scale factor using nearest-neighbor interpolation (order=0, anti_aliasing=False) to preserve label integrity.",
4040
parameters={
41-
"output_shape": {
42-
"type": "tuple",
43-
"default": None,
44-
"description": "Desired output shape as a tuple (e.g., (height, width)). If None, no resizing is performed.",
41+
"scale_factor": {
42+
"type": "float",
43+
"default": 1.0,
44+
"min": 0.01,
45+
"max": 10.0,
46+
"description": "Factor by which to resize the label image (e.g., 0.8 for 80% size, 1.2 for 120% size). 1.0 means no resizing.",
4547
},
4648
},
4749
)
48-
def resize_mask(mask: np.ndarray, output_shape=None) -> np.ndarray:
50+
def resize_labels(label_image: np.ndarray, scale_factor=1.0) -> np.ndarray:
4951
"""
50-
Resize a mask using nearest-neighbor interpolation to preserve mask integrity.
51-
Uses skimage.transform.resize with order=0 and anti_aliasing=False.
52+
Resize a label mask or label image by a scale factor using nearest-neighbor interpolation to preserve label integrity.
5253
"""
53-
if output_shape is None:
54-
return mask
54+
if scale_factor == 1.0:
55+
return label_image
56+
import numpy as np
5557
from skimage.transform import resize
5658

57-
resized = resize(
58-
mask,
59-
output_shape,
59+
new_shape = tuple(
60+
(np.array(label_image.shape) * scale_factor).astype(int)
61+
)
62+
scaled_labels = resize(
63+
label_image,
64+
new_shape,
6065
order=0,
6166
preserve_range=True,
6267
anti_aliasing=False,
63-
)
64-
# Ensure integer type for mask
65-
return resized.astype(mask.dtype)
68+
).astype(label_image.dtype)
69+
return scaled_labels
6670

6771
# Equalize histogram
6872
@BatchProcessingRegistry.register(

0 commit comments

Comments
 (0)