设置坐标轴,colorbar和绘图
绘制特定分辨率的图像,并去除坐标系和白边,得到完整纯粹的图片。
fig = plt.figure(figsize=(6,3),dpi=200)#分辨率设置
plt.figure(1)
im = plt.imshow(error_channels[:, :], cmap='jet', vmax=0.06, vmin=0, origin='lower') # 保存整个图像的误差热力图
plt.xlim(1, 110)
plt.ylim(1, 31)#设置坐标轴范围
plt.xlabel('Epoch')
plt.ylabel('Channels')
#plt.axis('off')#不显示坐标轴
plt.yticks([1,10,20,31])#设置坐标轴刻度
plt.xticks([1,20,40,60,80,100])
plt.colorbar(im, fraction=0.0135, pad=0.03)#fraction表示设置colorbar的大小,pad表示与图像之间的间隙
#mp.imsave()#按矩阵形式保存图像
plt.axis('off')
fig = plt.gcf()
fig.set_size_inches(13.0/3,13.29/3) #dpi = 300, output = 1300*1329 pixels
plt.gca().xaxis.set_major_locator(plt.NullLocator())#取出白边的操作
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
plt.margins(0,0)
plt.show()
fig.savefig('picture_path.png', transparent=True, dpi=300, pad_inches = 0)
为图片添加蒙层:
# 为图片设置蒙层的函数
def put_mask(image, mask_color, beta=0.5):
# 1.读取图片尺寸,确定和图片尺寸大小一致的蒙层
# 2.将蒙层与图片融合并输出
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # 将RGB图像转化为CV2中的BGR格式
x, y, c = image.shape
bbox = [0, 0, y, x]
# 3.画出mask
zeros = np.zeros((image.shape), dtype=np.uint8)
zeros_mask = cv2.rectangle(zeros, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
color=mask_color, thickness=-1) # thickness=-1 表示矩形框内颜色填充
try:
# alpha 为第一张图片的透明度
alpha = 1
# beta 为第二张图片的透明度
gamma = 0
mask_img = cv2.addWeighted(image, alpha, zeros_mask, beta, gamma)
print(mask_img.shape)
mask_img = cv2.cvtColor(mask_img, cv2.COLOR_BGR2RGB)
return mask_img
except:
print('异常')
# 绘制带有蒙层的图初始图像和恢复图像
# 410:蓝色[0, 0, 255]
# 480:深天蓝色[0,191,255]
# 520:纯绿色[0, 255, 0]
# 550:鲜绿色[0, 100, 0]
# 620:橙红色[255, 69, 0]
# 689:深红色[255,0,0]
#对应波段的BGR三原色数据(CV2)
color_m = np.array([[255, 0, 0], [255, 191, 0], [0, 255, 0], [47, 255, 173], [0, 69, 255], [0, 0, 255]])
# 绘制各通道的恢复图像
save_channels = [1, 8, 12, 15, 22, 29]
# 绘制原始高光谱图像(添加蒙层)
for i in np.arange(len(save_channels)):
im1 = hsi_G[:, :, save_channels[i]]
im1 = (im1 * 255).astype(np.uint8)
im1 = put_mask(im1, mask_color=(int(color_m[i, 0]), int(color_m[i, 1]), int(color_m[i, 2])), beta=0.3)
plt.imshow(im1, vmax=255, vmin=0)
# plt.colorbar(im, fraction=0.0495, pad=0.03)
plt.axis('off')
fig = plt.gcf()
fig.set_size_inches(13.0 / 3, 13.29 / 3)
plt.gca().xaxis.set_major_locator(plt.NullLocator()) # 取出白边的操作
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
fig.savefig(save_picture_path + str(save_channels[i]) + '_hsi_G_m.png', transparent=True, dpi=300, pad_inches=0)
plt.show()
plt.clf()
# 绘制重建的高光谱图像(添加蒙层)
for i in np.arange(len(save_channels)):
im1 = hsi_R[:, :, save_channels[i]]
im1 = (im1 * 255).astype(np.uint8)
im1 = put_mask(im1, mask_color=(int(color_m[i, 0]), int(color_m[i, 1]), int(color_m[i, 2])), beta=0.3)
plt.imshow(im1, vmax=255, vmin=0)
plt.axis('off')
fig = plt.gcf()
fig.set_size_inches(13.0 / 3, 13.29 / 3) # dpi = 300, output = 700*700 pixels
plt.gca().xaxis.set_major_locator(plt.NullLocator()) # 取出白边的操作
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
fig.savefig(save_picture_path + str(save_channels[i]) + '_hsi_R_m.png', transparent=True, dpi=300, pad_inches=0)
plt.show()
plt.clf()
绘制误差热力图的方法:
####绘制误差热力图
#平均误差图:
def picture_mean_error(hsi_R,hsi_G):
error = np.abs(hsi_R - hsi_G)
error = error / hsi_G
error = np.mean(error, axis=2)
return error
#单个通道的误差图
def picture_error(hsi_R,hsi_G):
error = np.abs(hsi_R - hsi_G)
error = error / hsi_G
return error
save_channels = [1, 8, 12, 15, 22, 29]
# 绘制save_channels中指出的误差图
for i in (save_channels):
im1 = picture_error(hsi_R[:, :, i], hsi_G[:, :, i])
im = plt.imshow(im1, cmap='jet', vmax=0.1, vmin=0)
plt.axis('off')
fig = plt.gcf()
fig.set_size_inches(13.0 / 3, 13.29 / 3) # dpi = 300, output = 700*700 pixels
plt.gca().xaxis.set_major_locator(plt.NullLocator()) # 取出白边的操作
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
fig.savefig(save_error_picture_path + str(i) + '_error.png', transparent=True, dpi=300, pad_inches=0)
plt.show()
plt.clf()
# 绘制所有通道的平均误差图
im2 = picture_mean_error(hsi_R, hsi_G)
im = plt.imshow(im2, cmap='jet', vmax=0.1, vmin=0)
plt.axis('off')
fig = plt.gcf()
fig.set_size_inches(13.0 / 3, 13.29 / 3) # dpi = 300, output = 700*700 pixels
plt.gca().xaxis.set_major_locator(plt.NullLocator()) # 取出白边的操作
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
fig.savefig(save_error_picture_path + 'error.png', transparent=True, dpi=300, pad_inches=0)