Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to create masks grouped by label id #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/omero_rois/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import numpy as np
from collections import defaultdict
from omero.gateway import ColorHolder
from omero.model import MaskI
from omero.rtypes import (
Expand Down Expand Up @@ -134,3 +135,35 @@ def masks_from_label_image(
raise_on_no_mask)
masks.append(mask)
return masks


def masks_from_3d_label_image(
planes, rgba=None, z=None, c=None, z_stack=True, t=None, text=None):
"""
Create mask shapes from a 3d label image, either z stack or timepoints,
grouped by label ID (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 z: Optional Z-index for the mask
:param c: Optional C-index for the mask
:param t: Optional T-index for the mask
:param z_stack: Flag if the planes represent a z stack, timepoints
otherwise (default: True)
:param text: Optional text for the mask
:return: A dictionary of OMERO masks with the labels as keys
({} if no labels found)

"""
masks = defaultdict(list)
for i, plane in enumerate(planes):
if z_stack:
plane_masks = masks_from_label_image(plane, rgba, i, c, t,
text, False)
else:
plane_masks = masks_from_label_image(plane, rgba, z, c, i,
text, False)
for label, mask in enumerate(plane_masks):
if mask.getBytes().any():
masks[label].append(mask)
return masks