问题
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?
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)
回答1:
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)
Heatmap:
heatmap_img = cv2.applyColorMap(gray_img, cv2.COLORMAP_JET)
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)
You can vary the weight parameters in the function for both the images.
回答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
来源:https://stackoverflow.com/questions/46020894/how-to-superimpose-heatmap-on-a-base-image