Skip to content

Commit c5c2224

Browse files
committed
updated save_rois to ensure labels match mask ids
1 parent 87d35ae commit c5c2224

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cellpose/io.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,21 +638,28 @@ def save_to_png(images, masks, flows, file_names):
638638
save_masks(images, masks, flows, file_names, png=True)
639639

640640

641-
def save_rois(masks, file_name, multiprocessing=None):
641+
def save_rois(masks, file_name, multiprocessing=None, prefix='', pad=False):
642642
""" save masks to .roi files in .zip archive for ImageJ/Fiji
643+
When opened in ImageJ, the ROIs will be named [prefix][0000]n where n is 1,2,... corresponding to the masks label
643644
644645
Args:
645646
masks (np.ndarray): masks output from Cellpose.eval, where 0=NO masks; 1,2,...=mask labels
646647
file_name (str): name to save the .zip file to
648+
multiprocessing (bool, optional): Flag to enable multiprocessing. Defaults to None (disabled).
649+
prefix (str, optional): prefix to add at the beginning of the ROI labels in ImageJ. Defaults to no prefix
650+
pad (bool, optional): Whether to pad the numerical part of the label with zeros so that all labels have the same length
647651
648652
Returns:
649653
None
650654
"""
651655
outlines = utils.outlines_list(masks, multiprocessing=multiprocessing)
656+
657+
n_digits = int(np.floor(np.log10(masks.max()))+1) if pad else 0
658+
fmt = f'{{prefix}}{{:0{n_digits}d}}'
652659
rois = []
653-
for i,outline in enumerate(outlines):
660+
for n,outline in zip(np.unique(masks)[1:], outlines):
654661
if len(outline) > 0:
655-
rois.append(ImagejRoi.frompoints(outline, name=str(i+1)))
662+
rois.append(ImagejRoi.frompoints(outline, name=fmt.format(n)))
656663

657664
if len(outlines) != len(rois):
658665
print(f"empty outlines found, saving {len(rois)} ImageJ ROIs to .zip archive.")

0 commit comments

Comments
 (0)