圖像匹配

在圖像超分辨率任務中,要計算test result與原圖的psnr等,需要result和原圖按按順序一一對應,即在原圖文件夾中與在RESULT文件夾中,兩幅圖的名稱相同。但經過神經網絡得到的test resullt的圖片順序比較隨機,因此需要對其重新排序命名。

該程序效果是把source_path中的圖重命名爲與result_path中的相同,並將其轉移到trans_path中。使用的圖像匹配算法爲歐式距離。

# -*- coding: UTF-8 -*-    
import numpy as np
import cv2   
import os
from PIL import Image
 
source_path=r'./urban'#被改名
result_path=r'/res'#參照
trans_path=r'./diff'
source_files = os.listdir(source_path)
source_files.sort(key=lambda x: int(x.split('.')[0]))
result_files = os.listdir(result_path)
result_files.sort(key=lambda y: int(y.split('.')[0]))
 
for source_file in  source_files:
    MinDis = float('inf')
    source_img = Image.open((os.path.join(source_path,source_file)))
    x = np.asarray(source_img)
    for result_file in  result_files:
        result_img = Image.open((os.path.join(result_path,result_file)))
        y = np.asarray(result_img)
        if (x.shape[0]!=y.shape[0] or x.shape[1]!=y.shape[1]):
            continue
        EuDis = np.sqrt(np.sum(x - y)*(x - y))
        EuDist = sum(EuDis)
        EuDista = sum(EuDist)
        EuDistan = sum(EuDista)
#        print('EuDistan: '+ str(EuDistan))
#        print("shape: ")
#        print(EuDis.shape[0],EuDis.shape[1],EuDis.shape[2])  
        if (EuDistan < MinDis):
            MinDis = EuDistan
            number = int(result_file.split('.')[0])
#    print('最小歐拉距離: '+ str(MinDis))
    os.rename(os.path.join(source_path,source_file),os.path.join(trans_path,"%d.png" %number))

需要注意的是,該程序準確率並非100% 。

發佈了19 篇原創文章 · 獲贊 2 · 訪問量 5546
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章