Skip to content

Commit 0dcb25d

Browse files
authored
added label transform class encorporating min_size of labels (#322)
Added label transform class encorporating min_size of labels
1 parent f06b922 commit 0dcb25d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

torch_em/transform/label.py

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ def label_consecutive(labels, with_background=True):
3737
return seg
3838

3939

40+
class MinSizeLabelTransform:
41+
def __init__(self, min_size=None, ndim=None, ensure_zero=False):
42+
self.min_size = min_size
43+
self.ndim = ndim
44+
self.ensure_zero = ensure_zero
45+
46+
def __call__(self, labels):
47+
components = connected_components(labels, ndim=self.ndim, ensure_zero=self.ensure_zero)
48+
if self.min_size is not None:
49+
ids, sizes = np.unique(components, return_counts=True)
50+
filter_ids = ids[sizes < self.min_size]
51+
components[np.isin(components, filter_ids)] = 0
52+
components, _, _ = skimage.segmentation.relabel_sequential(components)
53+
return components
54+
55+
4056
# TODO smoothing
4157
class BoundaryTransform:
4258
def __init__(self, mode="thick", add_binary_target=False, ndim=None):

0 commit comments

Comments
 (0)