how to superimpose heatmap on a base image?

curio17 picture curio17 · Sep 3, 2017 · Viewed 15.4k times · Source

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)

Answer

Jeru Luke picture Jeru Luke · Sep 3, 2017

You can superimpose your heatmap on the image using the function cv2.addweighted() available in OpenCV.

Here is an example

Sample image:

img = cv2.imread('Sample.jpg', 1)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

enter image description here

Heatmap:

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

enter image description here

Superimposed:

Now if you want to superimpose this on top of the original image, you can use cv2.addweighted() function

fin = cv2.addWeighted(heatmap_img, 0.7, img, 0.3, 0)

enter image description here

You can vary the weight parameters in the function for both the images.