简体   繁体   中英

make a grayscale heatmap from an image

I'm trying to make a heatmap from a grayscaled image with cv2. The problem is, I get a strange looking image with this code:

import cv2
import numpy as np

img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)

cv2.imshow('img', img)
cv2.waitKey(0)

hist = cv2.calcHist([img], [0], None, [256], [0, 256])
hist2 = np.uint8(hist)

heat = cv2.LUT(img, hist2)

cv2.imshow('img', heat)
cv2.waitKey(0)
cv2.destroyAllWindows()

Not sure what do you mean by heatmap, but there are some colormaps in matplotlib which you can use to get similar results.

import matplotlib.pyplot as plt
import numpy as np
import cv2

image = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
colormap = plt.get_cmap('inferno')
heatmap = (colormap(image) * 2**16).astype(np.uint16)[:,:,:3]
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_RGB2BGR)

cv2.imshow('image', image)
cv2.imshow('heatmap', heatmap)
cv2.waitKey()

Also, you can use cv2.applyColorMap

image = cv2.imread('test.jpg', 0)
heatmap = cv2.applyColorMap(image, cv2.COLORMAP_HOT)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM