attention map灰度图可视化彩色映射(热力图heatmap)

【python图像处理】彩色映射_guduruyu的专栏-CSDN博客_python中的彩色映射

先用这个程序导出jet_int.txt

from matplotlib import cm
import numpy as np

colormap_int = np.zeros((256, 3), np.uint8)
colormap_float = np.zeros((256, 3), np.float)

for i in range(0, 256, 1):
   colormap_float[i, 0] = cm.jet(i)[0]
   colormap_float[i, 1] = cm.jet(i)[1]
   colormap_float[i, 2] = cm.jet(i)[2]

   colormap_int[i, 0] = np.int_(np.round(cm.jet(i)[0] * 255.0))
   colormap_int[i, 1] = np.int_(np.round(cm.jet(i)[1] * 255.0))
   colormap_int[i, 2] = np.int_(np.round(cm.jet(i)[2] * 255.0))

np.savetxt("jet_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')
np.savetxt("jet_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')

print(colormap_int)

读入image和attention map,可视化

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

def gray2color(gray_array, color_map):
    
    rows, cols = gray_array.shape
    color_array = np.zeros((rows, cols, 3), np.uint8)
 
    for i in range(0, rows):
        for j in range(0, cols):
            color_array[i, j] = color_map[gray_array[i, j]]
    
    #color_image = Image.fromarray(color_array)
 
    return color_array

jet_map = np.loadtxt('jet_int.txt', dtype = np.int)

img = Image.open('img.jpg').convert('L')
img = np.array(img)

img_color_jet = gray2color(img, jet_map)
# plt.subplot(212)


attention = Image.open('attention_0.jpg').convert('L')
attention = np.array(attention)
for i in range(1,8):
    x = Image.open('attention_'+str(i)+'.jpg').convert('L')
    x = np.array(x)
    attention += x
attention_color_jet = gray2color(attention, jet_map)
# plt.subplot(212)
# print(img_color_jet.shape)
# plt.imshow(img_color_jet)
# plt.imshow(attention_color_jet)
plt.imshow(img_color_jet)
plt.imshow(attention_color_jet,alpha=0.5)
plt.show()
 

attention map灰度图可视化彩色映射(热力图heatmap)_第1张图片

你可能感兴趣的:(Python,attention)