24

Please look at this github page. I want to generate heat maps in this way using Python PIL,open cv or matplotlib library. Can somebody help me figure it out? Superimposed heatmaps

I could create a heat map for my network at the same size as the input, but I am not able superimpose them. The heatmap shape is (800,800) and the base image shape is (800,800,3)

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
curio17
  • 660
  • 1
  • 6
  • 15

3 Answers3

32

Updated Answer -- 29th April, 2022.

After the repeated comments I have decided to update this post with a better visualization.

Consider the following image:

img = cv2.imread('image_path')

enter image description here

I obtained a binary image after performing binary threshold on the a-channel of the LAB converted image:

lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
a_component = lab[:,:,1]
th = cv2.threshold(a_component,140,255,cv2.THRESH_BINARY)[1]

enter image description here

Applying Gaussian blur:

blur = cv2.GaussianBlur(th,(13,13), 11)

enter image description here

The resulting heatmap:

heatmap_img = cv2.applyColorMap(blur, cv2.COLORMAP_JET)

enter image description here

Finally, superimposing the heatmap over the original image:

super_imposed_img = cv2.addWeighted(heatmap_img, 0.5, img, 0.5, 0)

enter image description here

Note: You can vary the weight parameters in the function cv2.addWeighted and observe the differences.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 1
    I mean the second image. please correct it! heated_img and fin look same. – curio17 Sep 03 '17 at 12:33
  • 4
    Your overall method is correct. But second and third image look the same, that's it! Change the picture of second. – curio17 Sep 03 '17 at 12:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153551/discussion-between-curio1729-and-jeru-luke). – curio17 Sep 03 '17 at 12:38
  • Answer works well. However it's odd to create a direct heatmap of an image and overlay this on the image. I think that's what confused @curio1729. Typically the heatmap overlaid is a result of estimation/prediction/other done on the image. – user3731622 Mar 16 '19 at 01:52
  • I agree. This would've been more clear if you dramatically blurred the input. I understand your heatmap is just an example, but because it is exactly the same underlying data is the original image, it doesn't look like what folks are expecting. – Geoff Sep 24 '19 at 15:32
2

My code starts from a heatmap matrix (224,224) called cam, which is applied to the original image called frame, via opencv;

and it seems to work pretty well:

import numpy as np
from cv2 import cv2
from skimage import exposure 
...

capture = cv2.VideoCapture(...)
while True:
    ret, frame = capture.read()

    if ret:
        #resize original frame
        frame = cv2.resize(frame, (224, 224)) 

        #get color map
        cam = getMap(frame)
        map_img = exposure.rescale_intensity(cam, out_range=(0, 255))
        map_img = np.uint8(map_img)
        heatmap_img = cv2.applyColorMap(map_img, cv2.COLORMAP_JET)

        #merge map and frame
        fin = cv2.addWeighted(heatmap_img, 0.5, frame, 0.5, 0)

        #show result
        cv2.imshow('frame', fin)

the getMap() function gets the headmap given the frame;

I found some interesting free videos about this topic:

https://www.youtube.com/watch?v=vTY58-51XZA&t=14s

https://www.youtube.com/watch?v=4v9usdvGU50&t=208s

0

I had some problems with grayscale images at this line

super_imposed_img = cv2.addWeighted(heatmap_img, 0.5, img, 0.5, 0)

but this one worked for me

plt.imshow(binary_classification_result * 0.99 + original_gray_image * 0.01)
user2809176
  • 1,042
  • 12
  • 29