Skip to content

Commit 31c6d62

Browse files
Merge branch 'Tomvl117-fill_holes_replacement' into main
2 parents 3d82e4e + ebcd861 commit 31c6d62

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

cellpose/utils.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import numpy as np
1212
import colorsys
1313
import fastremap
14+
import fill_voids
1415
from multiprocessing import Pool, cpu_count
1516

1617
from . import metrics
@@ -619,7 +620,7 @@ def size_distribution(masks):
619620
def fill_holes_and_remove_small_masks(masks, min_size=15):
620621
""" Fills holes in masks (2D/3D) and discards masks smaller than min_size.
621622
622-
This function fills holes in each mask using scipy.ndimage.morphology.binary_fill_holes.
623+
This function fills holes in each mask using fill_voids.fill.
623624
It also removes masks that are smaller than the specified min_size.
624625
625626
Parameters:
@@ -640,20 +641,25 @@ def fill_holes_and_remove_small_masks(masks, min_size=15):
640641
raise ValueError("masks_to_outlines takes 2D or 3D array, not %dD array" %
641642
masks.ndim)
642643

644+
# Filter small masks
645+
counts = fastremap.unique(masks, return_counts=True)[1][1:]
646+
if min_size > 0:
647+
masks = fastremap.mask(masks, np.nonzero(counts < min_size)[0] + 1)
648+
fastremap.renumber(masks, in_place=True)
649+
643650
slices = find_objects(masks)
644651
j = 0
645-
for i, slc in enumerate(slices):
652+
for i in np.arange(0, len(slices)):
653+
slc = slices[i]
646654
if slc is not None:
647655
msk = masks[slc] == (i + 1)
648-
npix = msk.sum()
649-
if min_size > 0 and npix < min_size:
650-
masks[slc][msk] = 0
651-
elif npix > 0:
652-
if msk.ndim == 3:
653-
for k in range(msk.shape[0]):
654-
msk[k] = binary_fill_holes(msk[k])
655-
else:
656-
msk = binary_fill_holes(msk)
657-
masks[slc][msk] = (j + 1)
658-
j += 1
656+
msk = fill_voids.fill(msk)
657+
masks[slc][msk] = (j + 1)
658+
j += 1
659+
660+
if min_size > 0:
661+
counts = fastremap.unique(masks, return_counts=True)[1][1:]
662+
masks = fastremap.mask(masks, np.nonzero(counts < min_size)[0] + 1)
663+
fastremap.renumber(masks, in_place=True)
664+
659665
return masks

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ dependencies:
2626
- zarr
2727
- dask_jobqueue
2828
- bokeh
29+
- fill-voids
2930

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'fastremap',
1515
'imagecodecs',
1616
'roifile',
17+
'fill-voids',
1718
]
1819

1920
image_deps = ['nd2', 'pynrrd']

0 commit comments

Comments
 (0)