利用 opencv 显示 对比实验效果 选择有效区域
import os
import random
import uuid
from tqdm import tqdm
import cv2
import numpy as np
import matplotlib.pyplot as plt
GT_Dir="results_compare/GT/"
Bicubic_dir="results_compare/Bicubic4x/"
Phase_dir ="results_compare/JiLin-1_ori_resSr/"
LR_Dir="results_compare/LR4x/"
MSDTGP_Dir="results_compare/MSDTGP/"
SAVE_Dir="results_compare/Save_Dir/"
CACHE_DATA = True
cache_data={}
def getFileDict():
scenes=os.listdir(GT_Dir)
scene_files={}
for scene in scenes:
dir_scene=os.path.join(GT_Dir,scene)
fileNames=os.listdir(dir_scene)
scene_files[scene]=list(fileNames)
return scene_files
def save_residual_img(dir,scene,file,mode="type"):
ori_file = os.path.join(dir, scene, file)
if "_residual" in scene:
return 0
if not os.path.exists(ori_file):
return 0
if mode=="phase":
index = int(file.split(".")[0])+1
ori_file = os.path.join(dir, scene, "rgb_sr_%02d.png" % index)
residual_dir = os.path.abspath(os.path.join(dir,scene+"_residual",file))
os.makedirs(os.path.join(dir,scene+"_residual"),exist_ok=True)
gt_file = os.path.join(GT_Dir,scene,file)
if ori_file in cache_data:
ori_img = cache_data.get(ori_file)
else:
ori_img = cv2.imread(ori_file, cv2.IMREAD_GRAYSCALE)
cache_data[ori_file] = ori_img
if gt_file in cache_data:
gt_img = cache_data.get(gt_file)
else:
gt_img = cv2.imread(gt_file, cv2.IMREAD_GRAYSCALE)
cache_data[gt_file] = gt_img
if ori_img is None:
ccc=0
if gt_img is None:
ccc=0
res_img = ori_img-gt_img
cv2.imwrite(residual_dir,res_img)
return residual_dir,gt_file
def product_residual(scene_dict):
for scene,files in scene_dict.items():
for file_name in tqdm(files):
save_residual_img(Bicubic_dir,scene,file_name,"bic")
save_residual_img(GT_Dir,scene,file_name,"GT")
save_residual_img(Phase_dir,scene,file_name,"phase")
save_residual_img(MSDTGP_Dir,scene,file_name,"MSDTGP")
def drawBox(img,bbox):
img_c=img.copy()
cv2.rectangle(img_c,(bbox[0],bbox[1]),(bbox[2],bbox[3]),(0,255,0),2)
return img_c
def drwaPicture(img_src,img_target,bbox):
plt.imshow(img_src)
plt.show()
plt.imshow(img_target)
plt.show()
img_c=img_src.copy()
img_c[bbox[0]:bbox[2],bbox[1]:bbox[3]]=img_target
plt.imshow(img_c)
plt.show()
return img_c
def refreshWindow(LR_img,phase_img,bic_img,gt_img,msdtgp_img,bbox,x=0,y=0,window_name="aa"):
w,h=gt_img.shape[:2]
PhaseImage_box=drawBox(phase_img,bbox)
BicubicImage_box=drawBox(bic_img,bbox)
GTImage_box=drawBox(gt_img,bbox)
MSDTGPImage_box=drawBox(msdtgp_img,bbox)
GT_imgc=drawBox(gt_img,bbox)
GT_imgc_or=GT_imgc.copy()
phase_imgc = phase_img[bbox[1]:bbox[3],bbox[0]:bbox[2],:]
bic_imgc = bic_img[bbox[1]:bbox[3],bbox[0]:bbox[2],:]
gt_imgc = gt_img[bbox[1]:bbox[3],bbox[0]:bbox[2],:]
msdtgp_imgc = msdtgp_img[bbox[1]:bbox[3],bbox[0]:bbox[2],:]
LR_imgcu = np.zeros_like(LR_img)
phase_imgcu = np.zeros_like(LR_img)
bic_imgcu = np.zeros_like(LR_img)
gt_imgcu = np.zeros_like(LR_img)
msdtgp_imgcu = np.zeros_like(LR_img)
phase_imgcu = cv2.resize(phase_imgc,(int(gt_img.shape[0] // 2), int(gt_img.shape[1] // 2)),phase_imgcu)
bic_imgcu = cv2.resize(bic_imgc,(int(gt_img.shape[0] // 2), int(gt_img.shape[1] // 2)),bic_imgcu)
gt_imgcu = cv2.resize(gt_imgc,(int(gt_img.shape[0] // 2), int(gt_img.shape[1] // 2)),gt_imgcu)
msdtgp_imgcu = cv2.resize(msdtgp_imgc,(int(gt_img.shape[0] // 2), int(gt_img.shape[1] // 2)),msdtgp_imgcu)
phase_imgcu_gray=cv2.cvtColor(phase_imgcu,cv2.COLOR_RGB2GRAY)
bic_imgcu_gray=cv2.cvtColor(bic_imgcu,cv2.COLOR_RGB2GRAY)
gt_imgcu_gray=cv2.cvtColor(gt_imgcu,cv2.COLOR_RGB2GRAY)
msdtgp_imgcu_gray=cv2.cvtColor(msdtgp_imgcu,cv2.COLOR_RGB2GRAY)
phase_imgcu_residual = phase_imgcu_gray - gt_imgcu_gray
bic_imgcu_residual = bic_imgcu_gray - gt_imgcu_gray
gt_imgcu_residual = gt_imgcu_gray - gt_imgcu_gray
msdtgp_imgcu_residual = msdtgp_imgcu_gray - gt_imgcu_gray
min_phase,max_phase=np.min(phase_imgcu_residual),np.max(phase_imgcu_residual)
phase_imgcu_residual = 0.0 + (phase_imgcu_residual- min_phase )*((255.0-0.0)/(max_phase-min_phase))
min_phase,max_phase=np.min(bic_imgcu_residual),np.max(bic_imgcu_residual)
bic_imgcu_residual = 0.0 + (bic_imgcu_residual- min_phase )*((255.0-0.0)/(max_phase-min_phase))
min_phase,max_phase=np.min(msdtgp_imgcu_residual),np.max(msdtgp_imgcu_residual)
msdtgp_imgcu_residual = 0.0 + (msdtgp_imgcu_residual- min_phase )*((255.0-0.0)/(max_phase-min_phase))
phase_imgcu_residual_show = cv2.applyColorMap(phase_imgcu_residual.astype(np.uint8), cv2.COLORMAP_JET)
bic_imgcu_residual_show = cv2.applyColorMap(bic_imgcu_residual.astype(np.uint8), cv2.COLORMAP_JET)
gt_imgcu_residual_show = cv2.applyColorMap(gt_imgcu_residual.astype(np.uint8), cv2.COLORMAP_JET)
msdtgp_imgcu_residual_show = cv2.applyColorMap(msdtgp_imgcu_residual.astype(np.uint8), cv2.COLORMAP_JET)
phase_imgcu_residual_show_noadd = phase_imgcu_residual_show.copy()
bic_imgcu_residual_show_noadd = bic_imgcu_residual_show.copy()
gt_imgcu_residual_show_noadd = gt_imgcu_residual_show.copy()
msdtgp_imgcu_residual_show_noadd = msdtgp_imgcu_residual_show.copy()
phase_imgcu_residual_show = 0.1 * phase_imgcu_residual_show + phase_imgcu
bic_imgcu_residual_show = 0.1 * bic_imgcu_residual_show + bic_imgcu
gt_imgcu_residual_show = gt_imgcu
msdtgp_imgcu_residual_show = 0.1 * msdtgp_imgcu_residual_show + msdtgp_imgcu
img2=np.concatenate([phase_imgcu,phase_imgcu_residual_show.astype(np.uint8)],axis=0)
img3=np.concatenate([bic_imgcu,bic_imgcu_residual_show.astype(np.uint8)],axis=0)
img4=np.concatenate([gt_imgcu,gt_imgcu_residual_show.astype(np.uint8)],axis=0)
img5=np.concatenate([msdtgp_imgcu,msdtgp_imgcu_residual_show.astype(np.uint8)],axis=0)
img_show= np.concatenate([GT_imgc_or,img4,img3,img5,img2],axis=1)
cv2.imwrite(os.path.join(SAVE_Dir,"fusion_all.png"),img_show)
cv2.imshow(window_name,img_show)
while(True):
if cv2.waitKey(1) & 0xFF == ord('p'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
savePics(img_show,phase_imgcu_residual_show_noadd,bic_imgcu_residual_show_noadd, gt_imgcu_residual_show_noadd,msdtgp_imgcu_residual_show_noadd,
PhaseImage_box,BicubicImage_box,GTImage_box,MSDTGPImage_box,LR_img,GT_imgc_or,GT_imgc,phase_imgcu,bic_imgcu,gt_imgcu,msdtgp_imgcu,phase_imgcu_residual_show,bic_imgcu_residual_show,gt_imgcu_residual_show,msdtgp_imgcu_residual_show)
cv2.destroyAllWindows()
def savePics( img_show,phase_imgcu_residual_show_noadd,bic_imgcu_residual_show_noadd, gt_imgcu_residual_show_noadd,msdtgp_imgcu_residual_show_noadd,PhaseImage_box,BicubicImage_box,GTImage_box,MSDTGPImage_box,LR_img,GT_imgc_or,GT_imgc,phase_imgcu,bic_imgcu,gt_imgcu,msdtgp_imgcu,phase_imgcu_residual_show,bic_imgcu_residual_show,gt_imgcu_residual_show,msdtgp_imgcu_residual_show):
save_dir=os.path.join(SAVE_Dir,str(uuid.uuid4())[:8])
if not os.path.exists(save_dir):
os.makedirs(save_dir, exist_ok=True)
cv2.imwrite(os.path.join(save_dir, "a_image_overview.png"), img_show)
cv2.imwrite(os.path.join(save_dir, "phase_imgcu_residual_show_noadd.png"), phase_imgcu_residual_show_noadd)
cv2.imwrite(os.path.join(save_dir, "bic_imgcu_residual_show_noadd.png"), bic_imgcu_residual_show_noadd)
cv2.imwrite(os.path.join(save_dir, "gt_imgcu_residual_show_noadd.png"), gt_imgcu_residual_show_noadd)
cv2.imwrite(os.path.join(save_dir, "msdtgp_imgcu_residual_show_noadd.png"), msdtgp_imgcu_residual_show_noadd)
cv2.imwrite(os.path.join(save_dir,"Phase_imgbox_whole.png"),PhaseImage_box)
cv2.imwrite(os.path.join(save_dir,"Bicubic_imgbox_whole.png"),BicubicImage_box)
cv2.imwrite(os.path.join(save_dir,"GT_imgbox_whole.png"),GTImage_box)
cv2.imwrite(os.path.join(save_dir,"MSDTGP_imgbox_whole.png"),MSDTGPImage_box)
cv2.imwrite(os.path.join(save_dir,"LR_img.png"),LR_img)
cv2.imwrite(os.path.join(save_dir,"GT_img_box.png"),GT_imgc_or)
cv2.imwrite(os.path.join(save_dir,"GT_img_boxpt.png"),GT_imgc)
cv2.imwrite(os.path.join(save_dir,"phasevsr_img_area.png"),phase_imgcu)
cv2.imwrite(os.path.join(save_dir,"bicubic_img_area.png"),bic_imgcu)
cv2.imwrite(os.path.join(save_dir,"GT_img_area.png"),gt_imgcu)
cv2.imwrite(os.path.join(save_dir,"msdtgp_img_area.png"),msdtgp_imgcu)
cv2.imwrite(os.path.join(save_dir,"phasevsr_img_resdiual.png"),phase_imgcu_residual_show)
cv2.imwrite(os.path.join(save_dir,"bicubic_img_resdiual.png"),bic_imgcu_residual_show)
cv2.imwrite(os.path.join(save_dir,"GT_img_resdiual.png"),gt_imgcu_residual_show)
cv2.imwrite(os.path.join(save_dir,"msdtgp_img_resdiual.png"),msdtgp_imgcu_residual_show)
print(f"save picture at: {save_dir}")
pass
def MouseEvent(event, x, y, flags, param):
if flags == cv2.EVENT_FLAG_SHIFTKEY:
print("shift 键按下")
if event == cv2.EVENT_MOUSEMOVE:
LR_img, phase_img, bic_img, gt_img, msdtgp_img, box_size, windowname= param
print(f"move x:{x} y:{y} box_size={box_size}\n")
bbox=[x-box_size,y-box_size,x+box_size,y+box_size]
print(f"bbox:{bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]}")
refreshWindow(LR_img,phase_img,bic_img,gt_img,msdtgp_img,bbox,x,y,windowname)
if event == cv2.EVENT_LBUTTONDBLCLK:
print(f"左键双击")
LR_img, phase_img, bic_img, gt_img, msdtgp_img, box_size, windowname = param
print(f"move x:{x} y:{y} box_size={box_size}\n")
bbox = [x - box_size, y - box_size, x + box_size, y + box_size]
print(f"bbox:{bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]}")
refreshWindow(LR_img, phase_img, bic_img, gt_img, msdtgp_img, bbox, x, y, windowname)
'''
if flags == cv2.EVENT_FLAG_ALTKEY:
print('摁住Alt')
if flags == cv2.EVENT_FLAG_CTRLKEY:
print('摁住Ctrl')
if flags == cv2.EVENT_FLAG_SHIFTKEY:
print('摁住Shift')
if flags == cv2.EVENT_FLAG_LBUTTON:
print('摁住左键')
if flags == cv2.EVENT_FLAG_MBUTTON:
print('摁住中键')
if flags == cv2.EVENT_FLAG_RBUTTON:
print('摁住右键')
'''
'''
if event == cv2.EVENT_LBUTTONDOWN:
print('左键击下')
if event == cv2.EVENT_LBUTTONUP:
print('左键弹起')
if event == cv2.EVENT_MBUTTONDOWN:
print('中键击下')
if event == cv2.EVENT_MBUTTONUP:
print('中键弹起')
if event == cv2.EVENT_RBUTTONDOWN:
print('右键击下')
if event == cv2.EVENT_RBUTTONUP:
print('右键弹起')
'''
def choice_Windows(scene,file):
LR_file = os.path.join(LR_Dir, scene, file)
if not os.path.exists(LR_file):
return 0
index = int(file.split(".")[0])+1
phase_file = os.path.join(Phase_dir, scene, "rgb_sr_%02d.png" % index)
bic_file = os.path.join(Bicubic_dir, scene, file)
gt_file = os.path.join(GT_Dir, scene, file)
msdtgp_file = os.path.join(MSDTGP_Dir, scene, file)
LR_img = cv2.imread(LR_file)
phase_img = cv2.imread(phase_file)
bic_img = cv2.imread(bic_file)
gt_img = cv2.imread(gt_file)
msdtgp_img = cv2.imread(msdtgp_file)
windowname= scene
cv2.namedWindow(windowname,cv2.WINDOW_NORMAL)
cv2.setMouseCallback(windowname,MouseEvent,(LR_img,phase_img,bic_img,gt_img,msdtgp_img,40,windowname))
refreshWindow(LR_img,phase_img,bic_img,gt_img,msdtgp_img,[50,50,100,100],0,0,windowname)
def choice_scene(scene_dict):
scenes=["000","001","002","003","004","005","006","007","008","009"]
for scene in scenes:
files = scene_dict.get(scene)
file_name=random.choices(files)[0]
choice_Windows(scene,file_name)
def main():
scene_files = getFileDict()
choice_scene(scene_files)
print(f"deal ok!")
pass
if __name__ == '__main__':
main()