[python]-分割结果可视化
常常会遇到需要将分割的结果可视化的问题,这里采用修改dataset的方式,传入原图路径,最终保存的图片是在原图上叠加分割的结果,可以根据需要修改颜色。
注意:这里的颜色三元组顺序是BGR。
示例1:
这是每张图包含两类分割标签的可视化,每个类别需要分别保存,而且加入gt和预测的overlap。
import matplotlib.pyplot as plt
import numpy as np
def masks_to_colorimg(masks,color):
colors = np.asarray([color])
colorimg = np.zeros((masks.shape[1], masks.shape[2], 3), dtype=np.float32) * 255
channels, height, width = masks.shape
for y in range(height):
for x in range(width):
selected_colors = colors[masks[:,y,x] > 0.5]
if len(selected_colors) > 0:
colorimg[y,x,:] = np.mean(selected_colors, axis=0)
return colorimg.astype(np.uint8)
c = 0
with torch.no_grad():
for inputs, labels, path in test_loader:
c += 1
inputs = inputs.to(device)
labels = labels.to(device)
pred = model(inputs)
pred = torch.sigmoid(pred)
pred = pred.data.cpu().numpy()
labels = labels.data.cpu().numpy()
pred_mask = pred[0,:,:,:,]
label_mask = labels[0,:,:,:,]
iou = np.zeros((2,512,512), dtype=np.float32)
# overlap大小为512*512,两个标签类别
for i in range(0,512):
for j in range(0,512):
if pred_mask[0][i][j] > 0.5 and label_mask[0][i][j] > 0.5:
iou[0][i][j] = 1
if pred_mask[1][i][j] > 0.5 and label_mask[1][i][j] > 0.5:
iou[1][i][j] = 1
target_0 = [helper.masks_to_colorimg(np.expand_dims(label_mask[0,:,:],0), (0,255,255))]
target_1 = [helper.masks_to_colorimg(np.expand_dims(label_mask[1,:,:],0), (0,255,153))]
pred_0 = [helper.masks_to_colorimg(np.expand_dims(pred_mask[0,:,:],0), (0,0,255))]
pred_1 = [helper.masks_to_colorimg(np.expand_dims(pred_mask[1,:,:],0), (255,0,255))]
image_origin_path = path[0]
img = cv2.imread(image_origin_path)
s = 0.5 # 阈值
iou_0 = [helper.masks_to_colorimg(np.expand_dims(iou[0,:,:],0), (0,102,255))]
iou_1 = [helper.masks_to_colorimg(np.expand_dims(iou[1,:,:],0), (255,0,0))]
target_0 = np.array(target_0) * s + img
target_1 = np.array(target_1) * s + img
pred_0 = np.array(pred_0) * s + img
pred_1 = np.array(pred_1) * s + img
iou_0 = np.array(iou_0) * s + img
iou_1 = np.array(iou_1) * s + img
cv2.imwrite('save_path' + '{}_gt_1'.format(c)+ '.jpg', np.squeeze(target_1))
cv2.imwrite('save_path/' + '{}_gt_0'.format(c)+ '.jpg', np.squeeze(target_0))
cv2.imwrite('save_path' + '{}_pred_1'.format(c)+ '.jpg', np.squeeze(pred_1))
cv2.imwrite('save_path' + '{}_pred_0'.format(c)+ '.jpg', np.squeeze(pred_0))
cv2.imwrite('save_path' + '{}_iou_1'.format(c)+ '.jpg', np.squeeze(iou_1))
cv2.imwrite('save_path' + '{}_iou_0'.format(c)+ '.jpg', np.squeeze(iou_0))
示例2:
如果不需要将不同标签分开保存,那么可以相应地修改传入维度和颜色列表
def masks_to_colorimg(masks,color):
colors = np.asarray(color) # 注意这里的color是颜色元组的列表
colorimg = np.zeros((masks.shape[1], masks.shape[2], 3), dtype=np.float32) * 255
channels, height, width = masks.shape
for y in range(height):
for x in range(width):
selected_colors = colors[masks[:,y,x] > 0.5]
if len(selected_colors) > 0:
colorimg[y,x,:] = np.mean(selected_colors, axis=0)
return colorimg.astype(np.uint8)
# 注意这里的label_mask是三维数组
target = [helper.masks_to_colorimg(label_mask, [(0,255,255), (255,0,0)])]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了