Add layers containing masks

Hi,
I have a biological images on top of which I would like to add masks of different colors. So far these masks are binary images.
If I add the masks as label layers, all the masks have the same color, so it’s not useful to visual multiple mask overlaid.
What is the best way to do it?

An obvious workaround would be to transform the mask images who are binary [0,255], into images containing [0,X]. But is there something easier?

Else, for each label layer, can I specify the label color?

Thank you in advance.

Hi @FiReTiTi, there is a “new_colormap” method available for labels layers that will do what you want. Hold on a sec, I’ll make you an example…

1 Like
# If starting napari from ipython or a jupyter notebook you need these lines
%gui qt
import napari
viewer = napari.Viewer()

# Example data
from skimage.data import binary_blobs
blobs = binary_blobs()

# Add several label image layers
for _ in range(3):
    viewer.add_labels(blobs)

# Randomize the colormaps for all the label layers
for layer in viewer.layers:
    if isinstance(layer, napari.layers.labels.labels.Labels):
        layer.new_colormap()

# Choose a specific layer to change the colormap
viewer.layers[0].new_colormap()  # first layer
viewer.layers[-1].new_colormap()  # last layer
3 Likes

Given a labels image, you can also directly specify the color of each label. See this file for an example:

4 Likes

Thank you both for your solutions, they both work perfectly fine and are very easy.

I indeed get a randomized new color using:

Or I can pick up the color I want using

1 Like