Skip to content

Commit

Permalink
added label transform class encorporating min_size of labels (#322)
Browse files Browse the repository at this point in the history
Added label transform class encorporating min_size of labels
  • Loading branch information
lufre1 committed Jul 11, 2024
1 parent f06b922 commit 0dcb25d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions torch_em/transform/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ def label_consecutive(labels, with_background=True):
return seg


class MinSizeLabelTransform:
def __init__(self, min_size=None, ndim=None, ensure_zero=False):
self.min_size = min_size
self.ndim = ndim
self.ensure_zero = ensure_zero

def __call__(self, labels):
components = connected_components(labels, ndim=self.ndim, ensure_zero=self.ensure_zero)
if self.min_size is not None:
ids, sizes = np.unique(components, return_counts=True)
filter_ids = ids[sizes < self.min_size]
components[np.isin(components, filter_ids)] = 0
components, _, _ = skimage.segmentation.relabel_sequential(components)
return components


# TODO smoothing
class BoundaryTransform:
def __init__(self, mode="thick", add_binary_target=False, ndim=None):
Expand Down

0 comments on commit 0dcb25d

Please sign in to comment.