Create heatmap over image

I would like to create a heatmap that is overlaid on RGB imagery.

The map is the result of some attention mapping and has the same height and width as the camera image but only one channel consisting of values between 0 and 1, corresponding to probabilities. Is there a natural way to use this map to highlight areas on the camera images in a heatmap-like fasion prior to saving the image via torch.save_image() ?

thanks in advance

1 Like

You could try to use PIL.Image.blend to blend the heatmap with your image.
Here is a small dummy example:

# Create heatmap image in red channel
heatmap = torch.empty(1, 252, 271).uniform_(0, 1)
heatmap = torch.cat((heatmap, torch.zeros(2, 252, 271)))

import torchvision.transforms.functional as TF
img = TF.to_pil_image(x)  # assuming your image in x
h_img = TF.to_pil_image(heatmap)

res = Image.blend(img, h_img, 0.5)
1 Like

thank you patrick!

I suppose this only works for batch sizes of 1 and cpu-based tensors, am I right?

Yes, the current code uses a single image, since Image.blend and the transformations won’t take a batch of images. Usually the number of images you would like to visualize isn’t that large, so that it shouldn’t be a problem.

If you are working with CUDA tensors, just push them to the CPU first using tensor = tensor.to('cpu').

thank you! I am a little confused as to why the displayed image has its coloring all messed up…

edit: Ok the method does no scaling by default and so I have to rescale the 8bit image to the range (0,1)

you can perform such operation in batch using kornia.add_weighted

See: https://kornia.readthedocs.io/en/latest/color.html#kornia.color.AddWeighted