Fast Affine Template Matching over Galois Field仿射模板匹配數據測試問題

Fast Affine Template Matching over Galois Field

http://cvhost.scv.cis.iwate-u.ac.jp/research/projects/affine_matching.html

關於其數據:

http://cvhost.scv.cis.iwate-u.ac.jp/research/projects/benchmark.zip

http://cvhost.scv.cis.iwate-u.ac.jp/research/projects/results.zip

模板圖片都是灰色的,我們這邊需要彩色的,故生成:

# -*- coding:utf-8 -*-
import os
import cv2
import numpy as np
np.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 os
import cv2
import numpy as np
np.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 = 1
for 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 all
close all
dbstop if error
% adding 2 subdirectories to Matlab PATH
AddPaths
% compiling the relevant Mex-files
CompileMex

vis_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;
end
fclose(compare_txt);
return;
end

文件夾佈局如下: 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章