# Create heatmap over image

**URL:** <https://discuss.pytorch.org/t/create-heatmap-over-image/41408>\
**Category:** Uncategorized\
**Created:** [April 1, 2019, 4:28pm UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408 "2019-04-01T16:28:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carsten\_Ditzel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/carsten_ditzel/32/8251_2.png) [@Carsten\_Ditzel](https://discuss.pytorch.org/u/Carsten_Ditzel)\
**Post date:** [April 1, 2019, 4:28pm UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408/1 "2019-04-01T16:28:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 1, 2019, 11:12pm UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408/2 "2019-04-01T23:12:48Z")

</div>

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

```python
# 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)

```

---

<div class="post-metadata">

**Author:** ![Carsten\_Ditzel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/carsten_ditzel/32/8251_2.png) [@Carsten\_Ditzel](https://discuss.pytorch.org/u/Carsten_Ditzel)\
**Post date:** [April 2, 2019, 8:27am UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408/3 "2019-04-02T08:27:09Z")

</div>

thank you patrick!

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

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 2, 2019, 8:30am UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408/4 "2019-04-02T08:30:19Z")

</div>

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')`.

---

<div class="post-metadata">

**Author:** ![Carsten\_Ditzel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/carsten_ditzel/32/8251_2.png) [@Carsten\_Ditzel](https://discuss.pytorch.org/u/Carsten_Ditzel)\
**Post date:** [April 2, 2019, 9:09am UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408/5 "2019-04-02T09:09:47Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![edgarriba](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/edgarriba/32/57_2.png) [@edgarriba](https://discuss.pytorch.org/u/edgarriba)\
**Post date:** [January 14, 2020, 11:40am UTC](https://discuss.pytorch.org/t/create-heatmap-over-image/41408/6 "2020-01-14T11:40:14Z")

</div>

you can perform such operation in batch using `kornia.add_weighted`

See: [https://kornia.readthedocs.io/en/latest/color.html#kornia.color.AddWeighted](https://kornia.readthedocs.io/en/latest/color.html#kornia.color.AddWeighted)
