def GenAndSaveWarp(A_list, B_list, flow_list, save_dir): ids = [str(id_) for id_ in range(len(A_list))] ratios = [] for id_, A_name, B_name, F_name in zip(ids, A_list, B_list, flow_list): flow = read_gen(F_name) A = read_gen(A_name) B = read_gen(B_name) A_Nwarp, A_Nwarp_mask = WarpNotEasy(A, flow, B) ratio = np.sum(A_Nwarp_mask) / A_Nwarp_mask.size A_Nwarp = A_Nwarp.astype(np.uint8) A_Nwarpimg = Image.fromarray(A_Nwarp) oname = os.path.join(save_dir, 'A_Nwarp' + str(id_) + '.png') A_Nwarpimg.save(oname) A_Nwarp_mask = A_Nwarp_mask.astype(np.uint8) * 255 A_Nwarp_maskimg = Image.fromarray(A_Nwarp_mask) oname = os.path.join(save_dir, 'A_Nwarp_mask' + str(id_) + '.png') A_Nwarp_maskimg.save(oname) print(ratio) ratios.append(ratio) oname = os.path.join(save_dir, 'mask_ratios.txt') save_list(oname, ratios)
def GenerateSparplots(self): flowlist = self.outlist gtflowlist = self.gtflow self.aepes=[] for i,flowname in enumerate(flowlist): n,e = splitext(flowname) resflowname = n + '_res' + e flow = read_gen(flowname) resflow = read_gen(resflowname) gtflow = read_gen(gtflowlist[i]) _,_,_,aepe0=self.SparplotSimple(i,flow,resflow,gtflow) self.aepes.append(aepe0) print("整个数据集抽样平均AEPE: ",np.mean(self.aepes))
def GenerateSparplots(self): assert (False == self.isempty) flowlist = self.flows['outFlow'] gtflowlist = self.rawData['gtFlow'] resflowlist = self.flows['resFlow'] assert (len(flowlist) == len(gtflowlist) == len(resflowlist)) for i in range(len(flowlist)): flow = read_gen(flowlist[i]) resflow = read_gen(resflowlist[i]) gtflow = read_gen(gtflowlist[i]) _, _, _, aepe0 = self.SparplotSimple(i, flow, resflow, gtflow) self.aepes.append(aepe0) self.avgAepe = np.mean(self.aepes) print("整个数据集抽样平均AEPE: ", self.avgAepe)
def _GenWarp(self, keys): flow_list = self.flows['outFlow'] A_list = self.rawData['imgA'] B_list = self.rawData['imgB'] assert len(flow_list) > 0 assert len(flow_list) == len(A_list) == len(B_list) for i, F_name, in enumerate(flow_list): A_name = A_list[i] B_name = B_list[i] flow = read_gen(F_name) A = read_gen(A_name) B = read_gen(B_name) if ('warp' in keys or 'warpOverlay' in keys or 'warpOverlayUsingMask' in keys): A_warp = warp_easy(A, flow) A_warp_mask = Mask(A_warp) if ('newWarp' in keys or 'newWarpOverlay' in keys or 'newWarpOverlayUsingMask' in keys): A_Nwarp, A_Nwarp_mask = WarpNotEasy(A, flow, B) #########A_Nwarp的定义需要修改 print('\rWarping... ', str(i), '/', str(len(flow_list)), end='Gen:') for key in keys: print(f'{key},', end='') O_name = self.warpPics[key][i] if ('warp' == key): self.Warp(A_warp, B, O_name) elif ('warpOverlay' == key): self.WarpOverlay(A_warp, B, O_name) elif ('warpOverlayUsingMask' == key): self.WarpOverlayUsingMask(A_warp, A_warp_mask, B, O_name) elif ('warpMask' == key): self.SaveMask(A_warp_mask, O_name) elif ('newWarp' == key): self.Warp(A_Nwarp, B, O_name) elif ('newWarpOverlay' == key): self.WarpOverlay(A_Nwarp, B, O_name) elif ('newWarpOverlayUsingMask' == key): self.WarpOverlayUsingMask(A_Nwarp, A_Nwarp_mask, B, O_name) elif ('newWarpMask' == key): self.SaveMask(A_Nwarp_mask, O_name) print('')
def VizGrayFlows(self, key, flowList): imGrayList = self.flowVizsGray[key + 'Vizg'] assert len(flowList) == len(imGrayList) if (len(flowList) == 0): print('WARRING VizGrayFlows: Not find any flowfiles') for flowName, imGrayName in zip(flowList, imGrayList): flow = read_gen(flowName) flow_g = abs_flow(flow) imsave(imGrayName, flow_g / np.max(flow_g))
def VizColorFlows(self, key, flowList): imList = self.flowVizs[key + 'Viz'] assert len(flowList) == len(imList) if (len(flowList) == 0): print('WARRING VizColorFlows: Not find any flowfiles') for flowName, imName in zip(flowList, imList): flow = read_gen(flowName) flow_viz = Image.fromarray(viz_flow(flow)) imsave(imName, flow_viz)
def _GenWarp(self, keys): flow_list = self.flows['outFlow'] A_list = self.rawData['imgA'] B_list = self.rawData['imgB'] assert len(flow_list) > 0 assert len(flow_list) == len(A_list) == len(B_list) for i, F_name, in enumerate(flow_list): A_name = A_list[i] B_name = B_list[i] flow = read_gen(F_name) A = read_gen(A_name) B = read_gen(B_name) if ('warp' in keys or 'warpOverlay' in keys or 'warpOverlayUsingMask' in keys): A_warp = warp_easy(A, flow) if ('newWarp' in keys or 'newWarpOverlay' in keys or 'newWarpOverlayUsingMask' in keys): res = WarpNotEasy(A, flow, B) if len(res) == 2: A_Nwarp, A_Nwarp_mask = res else: A_Nwarp = res A_Nwarp_mask = Mask(A_Nwarp) print('\r', str(i), '/', str(len(flow_list)), end='') for key in keys: O_name = self.warpPics[key][i] if ('warp' == key): self.Warp(A_warp, B, O_name) elif ('warpOverlay' == key): self.WarpOverlay(A_warp, B, O_name) elif ('warpOverlayUsingMask' == key): self.WarpOverlayUsingMask(A_warp, B, O_name, Mask(A_warp)) elif ('newWarp' == key): self.Warp(A_Nwarp, B, O_name) elif ('newWarpOverlay' == key): self.WarpOverlay(A_Nwarp, B, O_name) elif ('newWarpOverlayUsingMask' == key): self.WarpOverlayUsingMask(A_Nwarp, B, O_name, A_Nwarp_mask) elif ('newWarpMask' == key): A_Nwarp_mask_ = A_Nwarp_mask.astype(np.uint8) * 255 A_Nwarp_mask__ = Image.fromarray(A_Nwarp_mask_) A_Nwarp_mask__.save(O_name) print('')
imgB = '0007.png' gt = 'OpticalFlowIntoFuture_0006_L.pfm' save_path = path #'../data/Tools/TEST_EPE/2' p_imA = os.path.join(path, imgA) s_imA = os.path.join(save_path, 'imgA.png') p_imB = os.path.join(path, imgB) s_imB = os.path.join(save_path, 'imgB.png') p_gt = os.path.join(path, gt) s_gt = os.path.join(save_path, 'gt.jpg') if not os.path.exists(save_path): os.makedirs(save_path) print(f'Create {save_path}') imA = cv2.imread(p_imA) imB = cv2.imread(p_imB) flow = read_gen(p_gt) surf = cv2.xfeatures2d.SURF_create(800) keypointA, descriptorA = surf.detectAndCompute(imA, None) keypointB, descriptorB = surf.detectAndCompute(imB, None) bastMacher = cv2.FlannBasedMatcher_create() matches = bastMacher.match(descriptorA, descriptorB) matches.sort(key=lambda m: m.distance) goodmatch = matches[0:10] imagePointsA = np.array([keypointA[m.queryIdx].pt for m in goodmatch]) imagePointsB = np.array([keypointB[m.trainIdx].pt for m in goodmatch]) h**o, mask = cv2.findHomography(imagePointsA, imagePointsB) matchesMask = mask.ravel().tolist() h, w = imA.shape[0:2] imAwB = cv2.warpPerspective(imA, h**o, (w, h)) # (512, 384)) homoflow = GenFlowAB(h**o, (w, h))
# -*- coding: utf-8 -*- import numpy as np import sys from PIL import Image sys.path.append("../Server_EasyTest") from myflowlib import read_gen, viz_flow path = 'E:\\我的文档\\任务\\配准1\\测试图片\\展示在文档中的\\z1\\' gt_name = 'z1f.flo' res_name = 'z1res.flo' A_name = 'z1A.jpg' B_name = 'z1B.jpg' gt_name = path + gt_name A_name = path + A_name B_name = path + B_name res_name = path + res_name gt = read_gen(gt_name) A = read_gen(A_name) B = read_gen(B_name) res = read_gen(res_name) res = np.sqrt(res[:, :, 0] * res[:, :, 0] + res[:, :, 1] * res[:, :, 1]) res = res > 0.14 * (np.max(res)) res = res.astype(np.uint8) * 255 resimg = Image.fromarray(res) resimg.save(path + 'z1res_mask.png')
sys.path.append("../../Server_EasyTest") from myflowlib import read_gen, viz_flow, WarpNotEasy, warp_easy from yuyy_postprocess import warp_judge if __name__ == '__main__': path = '../../data/ds_v2/TEST5_1/' gt_name = 'flow/2gtAB.flo' A_name = 'show/2A.png' B_name = 'show/2B.png' gt_name = path + gt_name A_name = path + A_name B_name = path + B_name gt = read_gen(gt_name) A = read_gen(A_name) B = read_gen(B_name) # warp,mask = warp_judge(A,gt,B) warp = WarpNotEasy(A, gt) A_warpimg = Image.fromarray(warp) A_warpimg.save(path + 'show/2AF.jpg') gt_name = 'flow/2gtBA.flo' A_name = 'show/2B.png' B_name = 'show/2A.png' gt_name = path + gt_name A_name = path + A_name B_name = path + B_name
# -*- coding: utf-8 -*- """ Created on Tue Dec 4 16:31:24 2018 @author: Administrator """ import sys from os.path import join from scipy.misc import imsave,imread from PIL import Image import numpy as np sys.path.append("../Server_EasyTest") from myflowlib import read_gen,abs_flow path= 'E:\我的文档\任务\配准1\测试图片\展示在文档中的\z3' name = 'z3res.flo' save_name ='z3res_gray.jpg' name = join(path,name) save_name = join(path,save_name) res = read_gen(name) res = abs_flow(res) imsave(save_name,res/np.max(res))
import sys from os.path import join from PIL import Image import numpy as np sys.path.append("../Server_EasyTest") from myflowlib import read_gen path= 'E:\我的文档\任务\配准1\测试图片\展示在文档中的\z3' Aforward_name = 'z3A_forward.png' B_name = 'z3B.jpg' save_name = 'z3_F.jpg' Aforward_name = join(path,Aforward_name) B_name = join(path,B_name) save_name = join(path,save_name) Af = read_gen(Aforward_name) B = read_gen(B_name) save = np.array(Af * 0.5+ B * 0.5) save = save.astype(np.uint8) saveimg = Image.fromarray(save) saveimg.save(save_name)