博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fast Affine Template Matching over Galois Field仿射模板匹配数据测试问题
阅读量:2050 次
发布时间:2019-04-28

本文共 7595 字,大约阅读时间需要 25 分钟。

Fast Affine Template Matching over Galois Field

关于其数据:

模板图片都是灰色的,我们这边需要彩色的,故生成:

# -*- 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/

你可能感兴趣的文章
【Loadrunner】【浙江移动项目手写代码】代码备份
查看>>
Python几种并发实现方案的性能比较
查看>>
【实战】10.10.1.9考试系统代码完成一次答题代码备份
查看>>
[Jmeter]jmeter之脚本录制与回放,优化(windows下的jmeter)
查看>>
Jmeter之正则
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【JMeter】如何用JMeter进行压力测试
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Python】用Python打开csv和xml文件
查看>>
【Loadrunner】性能测试报告实战
查看>>
【面试】一份自我介绍模板
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>