import cv2 as cv import numpy as np def mark_type(infer,gt,ori_path,save_path): print(ori_path) ori=cv.imread(ori_path) #print(ori.shape) mark_r = ori[:,:,2] mark_g = ori[:,:,1] mark_b = ori[:,:,0] #print(mark_r.shape) tp_matrix=np.multiply(infer,gt)#真正例,标记为白色 fp_matrix=np.multiply(infer,1-gt)#假正例,标记为绿色 tn_matrix = np.multiply(1-infer, 1 - gt)#真反例,标记为黑色 fn_matrix = np.multiply(1-infer, gt)#假反例,标记为红色 mark_r[np.where(tp_matrix==1)]=255 mark_g[np.where(tp_matrix==1) ]=255 mark_b[np.where(tp_matrix == 1)]=255 mark_r[np.where(fn_matrix == 1)] = 255 mark_g[np.where(fp_matrix == 1)]=255 mark=np.stack((mark_b,mark_g,mark_r),axis=2) cv.imwrite(save_path,mark) print(save_path) ori_dir="C:/test_ori_img/" gt_path="C:/gt_label/" inferred_path="C:/inferred_jointed/" mark_dst_path="C:/marked/" pic_list_path="C:/TransUNet-main/infer_test_list.txt" inferred_list=[] gt_list=[] with open(pic_list_path,'r') as f: lines = f.readlines() for line in lines: inferred=inferred_path+line.strip("\n")+".png" gt=gt_path+line.strip("\n")+".tif" infer_arr=cv.imread(inferred,cv.IMREAD_GRAYSCALE) gt_arr=cv.imread(gt,cv.IMREAD_GRAYSCALE) infer_arr[infer_arr==255]=1 gt_arr[gt_arr==255]=1 save_path=mark_dst_path+line.strip("\n")+".png" ori_path=ori_dir+line.strip("\n")+".tiff" mark_type(infer_arr,gt_arr,ori_path,save_path)
根据语义分割二分类结果用不同颜色标注
于 2023-10-21 16:45:51 首次发布