Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to superimpose heatmap on a base image?

Tags:

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)

like image 377
curio17 Avatar asked Sep 03 '17 06:09

curio17


People also ask

How do you combine heatmaps?

To concatenate heatmaps, simply use + operator. Under default mode, dendrograms from the second heatmap will be removed and row orders will be the same as the first one. Also row names for the first two heatmaps are removed as well. The returned value of the concatenation is a HeatmapList object.


2 Answers

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.

like image 61
Jeru Luke Avatar answered Nov 28 '22 22:11

Jeru Luke


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

like image 25
Massimiliano D'Amico Avatar answered Nov 28 '22 23:11

Massimiliano D'Amico