本文共 7595 字,大约阅读时间需要 25 分钟。
关于其数据:
模板图片都是灰色的,我们这边需要彩色的,故生成:
# -*- coding:utf-8 -*-import osimport cv2import numpy as npnp.set_printoptions(suppress=True)def mkdir_os(path): if not os.path.exists(path): os.makedirs(path)savepath = "./image_save"mkdir_os(savepath)oripath = "./targets"templatespath = "./templates"imgs = os.listdir(oripath)templates_imgs = os.listdir(templatespath)GT_affine = cv2.FileStorage("./GT_affine.yml", cv2.FileStorage_READ)GT_coordinate = cv2.FileStorage("./GT_coordinate.yml", cv2.FileStorage_READ)GT_homography = cv2.FileStorage("./GT_homography.yml", cv2.FileStorage_READ)GT_SAD = cv2.FileStorage("./GT_SAD.yml", cv2.FileStorage_READ)for index in range(len(imgs)): img = imgs[index] templates = templates_imgs[index] print(img) print(templates) imageMat = cv2.imread(os.path.join(oripath, img), -1) name = 'G'+ str(index+1) homography = GT_homography.getNode(name).mat() result = np.zeros((100, 100, 3), np.uint8) result = cv2.warpPerspective(imageMat, homography, (100, 100), result) cv2.imwrite(os.path.join(savepath, templates), result)GT_affine.release()GT_coordinate.release()GT_homography.release()GT_SAD.release()
上面的代码最后的代码,在这之前也做了一些其他代码尝试,一并放出:
# -*- coding:utf-8 -*-import osimport cv2import numpy as npnp.set_printoptions(suppress=True)def mkdir_os(path): if not os.path.exists(path): os.makedirs(path)savepath = "./image_save"mkdir_os(savepath)oripath = "./targets"templatespath = "./templates"imgs = os.listdir(oripath)templates_imgs = os.listdir(templatespath)GT_affine = cv2.FileStorage("./GT_affine.yml", cv2.FileStorage_READ)GT_coordinate = cv2.FileStorage("./GT_coordinate.yml", cv2.FileStorage_READ)GT_homography = cv2.FileStorage("./GT_homography.yml", cv2.FileStorage_READ)GT_SAD = cv2.FileStorage("./GT_SAD.yml", cv2.FileStorage_READ)debug = 1for index in range(len(imgs)): img = imgs[index] templates = templates_imgs[index] print(img) imageMat = cv2.imread(os.path.join(oripath,img), -1) templatesMat = cv2.imread(os.path.join(templatespath,templates), -1) HH,WW = imageMat.shape[:2] imageTemp = np.zeros(imageMat.shape, np.uint8) name = 'G'+ str(index+1) coordinate = GT_coordinate.getNode(name).mat() pts = np.array([[coordinate[0][0], coordinate[0][1]], [coordinate[1][0], coordinate[1][1]], [coordinate[2][0], coordinate[2][1]], [coordinate[3][0], coordinate[3][1]]], np.int32) pts = pts.reshape((-1, 1, 2)) mask = np.zeros((HH,WW), np.uint8) mask = cv2.polylines(mask, [pts], True, (0, 255, 255)) mask2 = cv2.fillPoly(mask.copy(), [pts], (255, 255, 255)) intpts = np.int0(pts) x, y, w, h = cv2.boundingRect(intpts) if debug: cv2.imwrite("1.png", mask2) imageTemp = cv2.bitwise_and(imageMat, imageMat, imageTemp, mask2) if debug: cv2.imwrite("2.png", imageTemp) min_rect = cv2.minAreaRect(pts) box = cv2.boxPoints(min_rect) box = np.int0(box) if debug: #cv2.drawContours(imageMat, [box], 0, (0, 0, 255), 2) cv2.imwrite("3.png", imageMat) imgsub = imageTemp[y:y + h, x:x + w] if debug: cv2.imwrite("4.png", imgsub) affine = GT_affine.getNode(name).mat() #仿射变换 #points1 = np.float32([[50, 50], [200, 50], [50, 200]]) #points2 = np.float32([[10, 100], [200, 50], [100, 250]]) #matrix = cv2.getAffineTransform(points1, points2) #output = cv2.warpAffine(img, matrix, (cols, rows)) A2 = cv2.warpAffine(templatesMat, affine[:2,:], imageMat.shape[:2], borderValue=255) if debug: cv2.imwrite("5.png", A2) homography = GT_homography.getNode(name).mat() homography_inv = np.linalg.inv(homography) newcoordlist = [] for key in range(4): coord = np.array([coordinate[key][0], coordinate[key][1], 1]) newcoord = np.matmul(homography,coord) newcoordlist.append([newcoord[0],newcoord[1]]) oricoord = np.array(newcoordlist) A2 = cv2.warpAffine(templatesMat, homography[:2,:], imageMat.shape[:2], borderValue=255) if debug: cv2.imwrite("5.png", A2) result = np.zeros((100, 100, 3), np.uint8) #透视变换 #points1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]]) #points2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]]) #homography = cv2.getPerspectiveTransform(points1, points2) #output = cv2.warpPerspective(img, matrix, (cols, rows)) result = cv2.warpPerspective(imageMat, homography, (100, 100), result) cv2.imwrite("6.png", result)# 关闭文件GT_affine.release()GT_coordinate.release()GT_homography.release()GT_SAD.release()
最后关于这篇论文的对比对象是Fast-Match
我们修改matlab代码,使其可以使用这边论文的数据,给出遍历所有图像那部分的代码:
注意这里使用了mexopencv,故可以使用opencv的函数,用来读取yml文件
function FastMatch_demo%%%%%%%%%%%%%%%%%%%%%%%clc% clear allclose alldbstop if error% adding 2 subdirectories to Matlab PATHAddPaths% compiling the relevant Mex-filesCompileMexvis_resultpath = '.\dataset_FATMoGF\vis_result';mkdir(vis_resultpath)GT_affine = cv.FileStorage('.\dataset_FATMoGF\GT_affine.yml');GT_coordinate = cv.FileStorage('.\dataset_FATMoGF\GT_coordinate.yml');GT_homography = cv.FileStorage('.\dataset_FATMoGF\GT_homography.yml');GT_SAD = cv.FileStorage('.\dataset_FATMoGF\GT_SAD.yml');affine = struct2cell(GT_affine);coordinate = struct2cell(GT_coordinate);filename = '.\dataset_FATMoGF\file_ID.txt';[file_ID] = textread(filename,'%s');compare_txt = fopen(fullfile('.\dataset_FATMoGF','compare.txt'),'w');fprintf(compare_txt,'overlapError fullError\r\n'); for i=1:length(file_ID) optMat = affine{i,1}; coord = coordinate{i,1}; ID = file_ID(i); Name = ID{1}; templateImgName = fullfile('.\dataset_FATMoGF','image_save',['template_', Name]); targetImgName = fullfile('.\dataset_FATMoGF','targets',Name); img = imread(targetImgName); imgColor = im2double(img); %imgColor = MakeOdd(imgColor); img = im2double(rgb2gray(img)); %img = MakeOdd(img); template = imread(templateImgName); templateColor = im2double(template); %templateColor = MakeOdd(templateColor); template = im2double(rgb2gray(template)); %template = MakeOdd(template); [bestConfig,bestTransMat,sampledError] = FastMatch(template,img,templateColor, imgColor); % Visualize result %[optError,fullError,overlapError] = MatchingResult(template,img,templateColor,imgColor,bestTransMat,[],'example 1'); [optError,fullError,overlapError] = MatchingResultOpencv(template,img,templateColor,imgColor,coord, Name, vis_resultpath, bestTransMat,optMat,'example 1'); fprintf(compare_txt,'%.6f %.6f\r\n',overlapError,fullError); a = 1;endfclose(compare_txt);return;end
文件夹布局如下:
顺便给出和Fast-Match方法重叠误差对比图代码
import numpy as npimport matplotlib.pyplot as pltplt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签plt.rcParams['axes.unicode_minus']=False #用来正常显示负号plt.rcParams['font.family'] = 'Arial'dataArray = np.genfromtxt('compare.txt', delimiter = ' ', names = True)print(dataArray.dtype.names)dataArray_my = np.genfromtxt('compare_my.txt', delimiter = ' ', names = True)print(dataArray_my.dtype.names)plt.figure(figsize=(15,3))ax = plt.gca()plt.xticks(fontsize = 15,fontname='Arial')plt.yticks(fontsize = 15,fontname='Arial')plt.xlim(1,500)plt.ylim(0,1)plt.grid()plt.title(r'重叠误差的比较结果', fontsize=15, fontname='SimHei')plt.ylabel('重叠误差', fontsize=15, fontname='SimHei')plt.xlabel('图像序号', fontsize=15, fontname='SimHei')plt.gcf().subplots_adjust(bottom=0.2)plt.plot(dataArray['our_error'], 'ok', label='FATMoGF')plt.plot(dataArray['other_error'], 'o', label='Fast-Match', alpha=.5)plt.legend(numpoints=1)plt.savefig("compare.pdf",bbox_inches='tight')plt.show()
转载地址:http://uzwlf.baihongyu.com/