Skip to content

Commit a3e2562

Browse files
committed
added mask resize processing function
1 parent 91bc234 commit a3e2562

2 files changed

Lines changed: 44 additions & 56 deletions

File tree

src/napari_tmidas/_tests/test_skimage_filters.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33

44
from napari_tmidas.processing_functions.skimage_filters import (
55
adaptive_threshold_bright,
6-
h_maxima_transform,
76
invert_image,
87
percentile_threshold,
8+
resize_mask,
99
rolling_ball_background,
1010
simple_thresholding,
1111
white_tophat,
1212
)
1313

1414

1515
class TestSkimageFilters:
16+
def test_resize_mask_nearest(self):
17+
"""Test resizing a mask with nearest-neighbor interpolation preserves mask integrity."""
18+
mask = np.zeros((10, 10), dtype=np.uint8)
19+
mask[2:8, 2:8] = 1
20+
output_shape = (20, 20)
21+
resized = resize_mask(mask, output_shape=output_shape)
22+
assert resized.shape == output_shape
23+
# Should only contain 0 and 1
24+
assert set(np.unique(resized)).issubset({0, 1})
25+
# Check that the central region is still 1
26+
assert np.sum(resized == 1) > 0
27+
1628
def test_invert_image_basic(self):
1729
"""Test basic image inversion functionality"""
1830
image = np.random.rand(100, 100)
@@ -134,21 +146,6 @@ def test_rolling_ball_background_subtraction(self):
134146
# Center of bright spot should be brighter in result than in corners
135147
assert result[50, 50] > result[10, 10]
136148

137-
def test_h_maxima_transform(self):
138-
"""Test H-maxima transform suppresses small peaks"""
139-
# Create image with peaks of different heights
140-
image = np.zeros((100, 100), dtype=np.uint8)
141-
image[20, 20] = 100 # Small peak
142-
image[50, 50] = 200 # Large peak
143-
image[80, 80] = 80 # Very small peak
144-
145-
result = h_maxima_transform(image, h=50.0)
146-
147-
# Should suppress small peaks, keep large ones
148-
assert result.shape == image.shape
149-
# Large peak should remain prominent
150-
assert result[50, 50] > result[20, 20]
151-
152149
def test_adaptive_threshold_bright(self):
153150
"""Test adaptive thresholding with bright bias"""
154151
# Create image with varying brightness

src/napari_tmidas/processing_functions/skimage_filters.py

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,37 @@
3333

3434
if SKIMAGE_AVAILABLE:
3535

36+
@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.",
40+
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.",
45+
},
46+
},
47+
)
48+
def resize_mask(mask: np.ndarray, output_shape=None) -> np.ndarray:
49+
"""
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+
"""
53+
if output_shape is None:
54+
return mask
55+
from skimage.transform import resize
56+
57+
resized = resize(
58+
mask,
59+
output_shape,
60+
order=0,
61+
preserve_range=True,
62+
anti_aliasing=False,
63+
)
64+
# Ensure integer type for mask
65+
return resized.astype(mask.dtype)
66+
3667
# Equalize histogram
3768
@BatchProcessingRegistry.register(
3869
name="Equalize Histogram",
@@ -646,46 +677,6 @@ def rolling_ball_background(
646677

647678
return result
648679

649-
@BatchProcessingRegistry.register(
650-
name="H-Maxima Transform",
651-
suffix="_hmaxima",
652-
description="Suppress all maxima with height less than h (keeps only prominent bright peaks)",
653-
parameters={
654-
"h": {
655-
"type": float,
656-
"default": 10.0,
657-
"min": 0.1,
658-
"max": 255.0,
659-
"description": "Height threshold (suppress maxima smaller than this)",
660-
}
661-
},
662-
)
663-
def h_maxima_transform(image: np.ndarray, h: float = 10.0) -> np.ndarray:
664-
"""
665-
Apply H-maxima transform to extract prominent bright peaks.
666-
667-
The H-maxima transform suppresses all local maxima whose height
668-
(difference from surrounding pixels) is less than h. This is excellent
669-
for finding bright spots and peaks while ignoring small intensity
670-
variations.
671-
672-
Parameters:
673-
-----------
674-
image : numpy.ndarray
675-
Input image array
676-
h : float
677-
Height threshold. Local maxima with peak height less than h
678-
will be suppressed. Larger values keep only more prominent peaks.
679-
680-
Returns:
681-
--------
682-
numpy.ndarray
683-
Image with only prominent bright peaks preserved
684-
"""
685-
from skimage.morphology import h_maxima
686-
687-
return h_maxima(image, h=h)
688-
689680
@BatchProcessingRegistry.register(
690681
name="Adaptive Threshold (Bright Bias)",
691682
suffix="_adaptive_bright",

0 commit comments

Comments
 (0)