Combine two color channels in a plot / heatmap / image?

I have an image with two grayscale color channels I would like to visualize in the same plot.

How can I combine them?

I think I would need to add an alpha or something that scales with the gray value …
Or instead of ch1_grad = cgrad([:black, pal[1]]) for the gradient, I would use something like:
ch1_grad = cgrad([:clear, pal[1]]) or
ch1_grad = cgrad([:empty, pal[1]]) if it existed.

using Plots, Images

# Set up color palette
pal = palette(:batlow,2)
# The blue selected is too dark; let's lighten it and use it.
pal_light = pal .+ 0.5*ones(RGB{Float32},2,1)
pal = [pal_light[1],pal[2]]

# Make a gradient from black for each channel.
ch1_grad = cgrad([:black, pal[1]])
ch2_grad = cgrad([:black, pal[2]])

# Test images with gradients.
ch1 = Gray.(rand(1000,1000).*range(0.0,stop=1.0,length=1000))
ch2 = Gray.(rand(1000,1000).*range(1.0,stop=0.0,length=1000))

fig_test = plot(
    legend = false,
    colorbar = false)

heatmap!(fig_test,
    reverse(Float64.(ch1),dims=1),
    color = ch1_grad,
    colorbar = false,
    axis = nothing,
    xaxis = false,
    yaxis = false,
    aspect_ratio = :equal)

heatmap!(fig_test,
    reverse(Float64.(ch2),dims=1),
    color = ch2_grad,
    colorbar = false,
    axis = nothing,
    xaxis = false,
    yaxis = false,
    aspect_ratio = :equal)

image
image

If you don’t need axes on the plot, try Conversions vs. views · JuliaImages.

2 Likes

Thanks for the hint, Tim. I’m still not clear what to write though to apply these ideas to produce an image with two channels that range from black for 0 to the specified colors for each channel.

It seems like I need to use channelview or colorview, but how do I apply the palette to the grayscale images? I guess I would need something like the color_me function from this post? But it doesn’t accept my ch1_grad as a second argument, so I still can’t tell how to get my colors applied.