
这段时间做了些工作,在Mac下基于opencv做了单应性矩阵的求解,下面的文档中给出了循环读取文件的代码,读出文件以后两两做siftImageAlignment,求出了几何校正后影像result和单应性矩阵H。imreadimread
import numpy
import os
import math
import cv2
def image_file(file_path):
if os.path.exists(file_path):
print(“files exists”)
else:
print(“no files exists”)
image_dir = []
files = os.listdir(file_path)
for fi in files:
fi_d = os.path.join(file_path, fi)
if os.path.isdir(fi_d):
print(os.path.join(file_path, fi_d))
image_file(fi_d)
else:
if fi.endswith(‘JPG’):
print(os.path.join(file_path, fi_d))
image_dir.append(fi_d)
else:

print(“error file”)
print len(image_dir)
return image_dir
def save_file(img_dir,save_image,save_path):
[dirname,filename] = os.path.split(img_dir)
save_image_ = os.path.join(save_path,filename)
cv2.imwrite(save_image_, save_image)
return save_image_
def sift_kp(image):
print ‘sift_kp’
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d_SIFT.create()
kp,des = sift.detectAndCompute(image,None)
kp_image = cv2.drawKeypoints(gray_image,kp,None)
return kp_image,kp,des
def get_good_match(des1,des2):
print ‘get_good_match’
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
return good
def siftImageAlignment(img1,img2):
print ‘siftImageAlignment’
_,kp1,des1 = sift_kp(img1)
_,kp2,des2 = sift_kp(img2)

goodMatch = get_good_match(des1,des2)
print ‘get_good_match_end’
if len(goodMatch) > 4:
ptsA= numpy.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-63299-1.html