microproduct/Ortho/test/testMatch.py

114 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import logging
# from re import S
# from oneOrthoAuxData import OrthoAuxData
# from OrthoImage import ImageHandler
from tool.algorithm.image.ImageHandle import ImageHandler
import tarfile
# from OrthoDB import ManageAlgXML, CheckSource
from tool.algorithm.xml.AlgXmlHandle import ManageAlgXML, CheckSource # 导入xml文件读取与检查文件
from OrthoAlg import IndirectOrthorectification, DEMProcess,ImageMatchClass
# from logHandler import LogHandler
from tool.algorithm.algtools.logHandler import LogHandler
from tool.algorithm.xml.CreatMetafile import CreateMetafile
from OrthoXmlInfo import CreateDict, CreateStadardXmlFile
from osgeo import gdal, osr
import os
import glob
import gc
import datetime
import shutil
import sys
import cv2
ori_sar_path="D:\MicroWorkspace\C-SAR\Ortho\Temporary\TestSAR\GF3_SAY_QPSI_013952_E118.9_N31.5_20190404_L1A_HH_L10003923848.jpg"
sim_sar_path="D:\MicroWorkspace\C-SAR\Ortho\Temporary\TestSim\sim_img_sum.jpg"
work_sapce_path="D:\MicroWorkspace\C-SAR\Ortho\Temporary"
'''
import matplotlib.pyplot as plt
img1 = cv2.imread(ori_sar_path, 0)
img2 = cv2.imread(sim_sar_path, 0)
def cv_show(name,img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
bf = cv2.BFMatcher(crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None,flags=2)
cv2.imwrite(work_sapce_path,img3)
'''
# 匹配影像
def ImageMatch(ori_sar_path,sim_sar_path,work_sapce_path):
ori_sar=gdal.Open(ori_sar_path)
sim_sar=gdal.Open(sim_sar_path)
# 影像尺寸
ori_height=ori_sar.RasterYSize
ori_width=ori_sar.RasterXSize
sim_height=sim_sar.RasterYSize
sim_width=sim_sar.RasterXSize
# 分块匹配
ori_sar_arr=ori_sar.GetRasterBand(1).ReadAsArray(0,0,ori_width,ori_height) # 原始影像
ori_img=(255*ori_sar_arr/np.max(ori_sar_arr)).astype(np.uint8)
sim_sar_arr=np.log(sim_sar.GetRasterBand(1).ReadAsArray(0,0,sim_width,sim_height)+1) # 模拟影像
sim_img=(1+253*sim_sar_arr/np.max(sim_sar_arr)).astype(np.uint8)
res = cv.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
top_left = max_loc
# 范围
min_w=top_left[0] if top_left[0]>0 else 0
min_h=top_left[1] if top_left[1]>0 else 0
max_w=top_left[0]+ori_width if top_left[0]+ori_width<sim_width else sim_width
max_h=top_left[1]+ori_height if top_left[0]+ori_height<sim_height else sim_height
# 裁剪
sim_clip=sim_img[min_h:max_h,min_w:max_w]
for i in range(0,ori_img.shape[0],10):
for j in range(0,ori_img.shape[1],10):
pass
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread(sim_sar_path,0)
img2 = img.copy()
template = cv.imread(ori_sar_path,0)
w, h = template.shape[::-1]
# 列表中所有的6种比较方法
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
i=0
for meth in methods:
img = img2.copy()
method = eval(meth)
# 应用模板匹配
res = cv.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
# 如果方法是TM_SQDIFF或TM_SQDIFF_NORMED则取最小值
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img,top_left, bottom_right, 255, 2)
cv.imwrite(os.path.join(work_sapce_path,"matchresult_{}.jpg".format(i)),img)
i=i+1