GLMakie: masking values when plotting images

I’m using GLMakie to display 2 images.
GLMakie.image(GLMakie.rotr90(image_data))
GLMakie.image!(GLMakie.rotr90(some_extra_data), colormap=(“viridis”, 0.5))

The second image have a transparency of 0.5. See here: GLMakie: how to plot two images with transparency?
some_extra_data contains good values and also invalid values. Invalid values are equal to zero.

What I want to achieve is to mask the invalid values in some_extra_data such that they are not displayed at all. Only the valid values shall be displayed. In python this can be achieved via matplotlib and numpy.ma.masked_where.
So, how to do this masking with GLMakie?

NaN values are transparent by default, so you can replace the invalid numbers with NaNs

Replacing with NaN doesn’t work, I have tried the following:

some_with_nan = [e == 0 ? NaN : e for e in some_extra_data]
GLMakie.image(GLMakie.rotr90(image_data))
GLMakie.image!(GLMakie.rotr90(some_with_nan ), colormap=(“viridis”, 0.5))

I have tried to use heatmap for “some_with_nan”. And there indeed NaN values are transparent when I only display the heatmap. But the problem is that I can no longer display heatmap “some_with_nan” over “image_data”.

It seems to me that nan_color option in GLMakie.image is not implemented yet. I have opened an issue: https://github.com/JuliaPlots/Makie.jl/issues/1230

I have managed to overlap “image” and “heatmap” by setting a transparency value to the “image” as well.
image(rotr90(image_data), colormap=(:gray1, 0.5))
image!(rotr90(some_with_nan ), colormap=(:viridis, 0.5))

This is how the output looks like, currently. To be improved.

1 Like

With some extra tweaks, I have reached the desired output. Instead of GLMakie.image I have used GLMakie.heatmap also for the image data, in order to get transparency for the image as well, see bellow.

some_extra_data_nan = [e == 0 ? NaN : e for e in some_extra_data]

img_data2 = img_data/256
img_data2[some_extra_data .!= 0] .= NaN

img_data3 = imr_wide.img_data/256.0
img_data3[some_extra_data .== 0] .= NaN

# where extra data is invalid, show the image, no transparency
heatmap(rotr90(img_data2), colormap=:gray1)
# where the extra data is valid, show the image with 0.5 transparency
heatmap!(rotr90(img_data3), colormap=(:gray1, 0.5)) 
# show the extra data
heatmap!(rotr90(some_extra_data_nan), colormap=(:viridis, 0.5)) 

Here is the output:

1 Like