plotly / dash-slicer

A volume slicer for Dash
https://dash.plotly.com/slicer
MIT License
24 stars 8 forks source link

Applying colormap to a Slicer object #54

Open SM-93 opened 3 years ago

SM-93 commented 3 years ago

Hi, I am trying to apply a colormap to already segmented images, i.e. I want to be able to color the labels when I change the slice index but I dont know how to incorporate input argument "value" in my function or do I even need that since I based my code on the example threshold_overlay.py but I don't need the contrast limit. This is the code snippet:

@app.callback(
    [
        Output(slicer1.overlay_data.id, "data")
    ],
    [Input("main-slider", "value")]
)
def apply_colors(value):
    print(value)
    mask = mask.astype(np.uint8)

    return slicer1.create_overlay_data(mask, colormap_gt)

I have three Slicer objects and I want on the two of those three to color the labels. The thing is my mask is already defined, i.e. my segmented images with 8 possible values (from 1 through 8).

I would really appreciate if someone could help me with this.

When I run the app in the browser, it displays the error:

dash.exceptions.InvalidCallbackReturnValue: Invalid number of output values for ..slicer2-overlay.data...
Expected 1, got 139

But my mask is already the same shape as the volume.

almarklein commented 3 years ago

That errror means that the return value of the callback for slicer2-overlay.data (slicer2, not slicer1!) does not have the correct number of values. I think you should either remove the [ ] around the Output, or use return [slicer1.create_overlay_data(mask, colormap_gt)]. That way the outputs should match.

SM-93 commented 3 years ago

Hi almarklein, I really appreciate you answering me, I tried as you said and it works! Quick question, when the callback is made, is there a way to actually assign certain label one particular color from the colormap ? My variable colormap_gt is a list of 9 hex colors.

almarklein commented 3 years ago

Great!

is there a way to actually assign certain label one particular color from the colormap ?

Well, if you know that the pixels that have e.g. value 5 in the mask, you must make colormap[5] the color you want. Is this what you meant?

SM-93 commented 3 years ago

Yeah definitely, I just thought that I had to do something else in the callback that is specific to the Dash for example. Thank you for taking the time to answer me.

SM-93 commented 3 years ago

Hi almarklein! I have one more question regarding colormaps. I don't understand why the colors change when displayed in the app, since I have different colors in my colormap. For example, one label is colored orange but I don't have even similar color to orange.

I would appreciate the help.

almarklein commented 3 years ago

The colors of the mask should match the color in the colormap. However, the mask is semi-transparent, so the colors might appear slightly differently, especially if the underlying image has colors.

SM-93 commented 3 years ago

Ok, so it should remain the same. Well some colors displayed in the app are similar to mine ( i.e. more transparent). The underlying image is just grayscale, nothing more. Anyways, thank you for the help, I appreciate it.