Skip to content

4. Masked Images and Labels

Masked images (or labels) are images that are masked by an instance segmentation mask.

In this section we will show how to create a MaskedImage object and how to use it to get the data of the image.

Similar to the Image and Label objects, the MaskedImage can be initialized from an OME-Zarr Container object using the get_masked_image method.

Let's create a masked image from the nuclei label:

>>> masked_image = ome_zarr_container.get_masked_image("nuclei")
>>> masked_image
MaskedImage(path=0, Dimensions(c: 3, z: 1, y: 4320, x: 5120), nuclei)

Since the MaskedImage is a subclass of Image, we can use all the methods available for Image objects.

The two most notable exceptions are the get_roi and set_roi which now instead of requiring a roi object, require an integer label.

>>> roi_data = masked_image.get_roi(label=1009, c=0)
>>> roi_data.shape
(1, 1, 76, 83)
2025-04-04T15:12:06.326158 image/svg+xml Matplotlib v3.10.1, https://matplotlib.org/

Additionally we can used the zoom_factor argument to get more context around the ROI. For example we can zoom out the ROI by a factor of 2:

>>> roi_data = masked_image.get_roi(label=1009, c=0, zoom_factor=2)
>>> roi_data.shape
(1, 1, 152, 166)
2025-04-04T15:12:06.395014 image/svg+xml Matplotlib v3.10.1, https://matplotlib.org/

Masked operations

In addition to the get_roi method, the MaskedImage class also provides a masked operation method that allows you to perform reading and writing only on the masked pixels.

For these operations we can use the get_roi_masked and set_roi_masked methods. For example, we can use the get_roi_masked method to get the masked data for a specific label:

>>> masked_roi_data = masked_image.get_roi_masked(label=1009, c=0, zoom_factor=2)
>>> masked_roi_data.shape
(1, 1, 152, 166)
2025-04-04T15:12:06.495336 image/svg+xml Matplotlib v3.10.1, https://matplotlib.org/

We can also use the set_roi_masked method to set the masked data for a specific label:

>>> masked_data = masked_image.get_roi_masked(label=1009, c=0)
>>> masked_data = np.random.randint(0, 255, masked_data.shape, dtype=np.uint8)
>>> masked_image.set_roi_masked(label=1009, c=0, patch=masked_data)
2025-04-04T15:12:06.597239 image/svg+xml Matplotlib v3.10.1, https://matplotlib.org/

Masked Labels

The MaskedLabel class is a subclass of Label and provides the same functionality as the MaskedImage class.

The MaskedLabel class can be used to create a masked label from an OME-Zarr Container object using the get_masked_label method.

>>> masked_label = ome_zarr_container.get_masked_label(label_name = "wf_2_labels", masking_label_name = "nuclei")
>>> masked_label
MaskedLabel(path=0, Dimensions(z: 1, y: 4320, x: 5120), nuclei)