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()
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值