sciai-lab/covid-if-annotations

How to set custom color map for image layer?

Closed this issue · 2 comments

@sofroniewn: I am using the image layer to display the segmentation edges showing the semantic classification labels after seeing this post.

This makes more sense, because I actually don't need any of the labels layer functionality instead of having a transparent background label.
However, I am not quite sure how to create the colormap I need with vispy.Colormap.
I need the following color map {1: red, 2: cyan, 3: yellow} (and {0: transparent}, but that works already). See https://github.com/hci-unihd/covid-if-annotations/blob/master/napari_covid_if_annotations/layers.py#L87-L98 for details.

(I could also dig into the vispy documentation more to figure this out, but I hope you know the answer immediately ;)).

Colormap in your example looks great, you just need to set edge_contrast_limits = [0, 3] and then it should work fine if it doesn't already. For example this toy demo works for the image layer

import numpy as np
from vispy.color import Colormap
import napari


data = np.random.randint(4, size=(10, 10))
custom = Colormap([
        [0., 0., 0., 0.],  # label 0 is fully transparent
        [1., 0., 0., 1.],  # label 1 is red
        [0., 1., 1., 1.],  # label 2 is cyan
        [1., 1., 0., 1.],  # label 3 is yellow
    ]
)

with napari.gui_qt():
    viewer = napari.view_image(data, colormap=custom, contrast_limits=[0, 3])

Screen Shot 2020-05-24 at 1 36 01 PM

Let me know if you need any more guidance here/ if that wasn't what you wanted

edge_contrast_limits = [0, 3] and then it should work fine if it doesn't already

Yes, that fixed it.
Thanks!