From de199659765ea3dbfbf959f90e20eee35c5d5dd1 Mon Sep 17 00:00:00 2001 From: Dominik Lindner Date: Wed, 10 Jun 2020 19:50:20 +0100 Subject: [PATCH] Add method to create masks grouped by label id --- src/omero_rois/library.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/omero_rois/library.py b/src/omero_rois/library.py index 55a7a6c..f6cac60 100644 --- a/src/omero_rois/library.py +++ b/src/omero_rois/library.py @@ -134,3 +134,37 @@ def masks_from_label_image( raise_on_no_mask) masks.append(mask) return masks + + +def masks_from_3d_label_image( + planes, rgba=None, c=None, t=None, text=None, + raise_on_no_mask=True): + """ + Create mask shapes from a 3d label image (background=0) + + :param list of numpy.array planes: list of 2D label arrays in z order + :param rgba int-4-tuple: Optional (red, green, blue, alpha) colour + :param c: Optional C-index for the mask + :param t: Optional T-index for the mask + :param text: Optional text for the mask + :param raise_on_no_mask: If True (default) throw an exception if no mask + found, otherwise return an empty Mask + :return: A dictionary of OMERO masks with the labels as keys + ({} if no labels found) + + """ + maxlabel = 1 + for plane in planes: + pmax = plane.max() + if pmax > maxlabel: + maxlabel = pmax + + masks = {} + for label in range(1, maxlabel + 1): + label_masks = [] + for z, plane in enumerate(planes): + mask = mask_from_binary_image(plane == label, rgba, z, c, t, text, + raise_on_no_mask) + label_masks.append(mask) + masks[label] = label_masks + return masks