我从无人机上拍摄了20张航拍照片,但顺序并不特殊。我正在尝试使用opencv缝合器api将它们缝合在一起。首先,我尝试编写一些代码将两张照片缝合在一起,但是我认为单应性存在问题。这是我的代码:
import numpy as np
import cv2
import sys
img1 = cv2.imread('images/00000710.png', 0)
img1 = cv2.blur(img1,(5,5))
h1, w1 = img1.shape[:2]
img2 = cv2.imread('images/00000711.png', 0)
img2 = cv2.blur(img2,(5,5))
h2, w2 = img2.shape[:2]
sift = cv2.xfeatures2d.SIFT_create()
kp1, desc1 = sift.detectAndCompute(img1, None)
kp2, desc2 = sift.detectAndCompute(img1, None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
#matches
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(desc1, desc2, k=2)
matches = sorted(matches, key = lambda x:x[0].distance)
good = [m1 for (m1, m2) in matches if m1.distance < 0.7 * m2.distance]
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2)
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
pts = np.float32([[0,0],[0,h1-1],[w1-1,h1-1],[w1-1,0]]).reshape(-1,1,2)
warped_img = cv2.warpPerspective(img2, H, (1200,1200))
cv2.imwrite('warped.jpg', warped_img)
stitcher = cv2.createStitcher(False)
result = stitcher.stitch((img1, warped_img))
cv2.imwrite('result.jpg', result[1])
我从未做过这样的事情,但是我在线阅读了许多指南,并且阅读了文档。但是,变形的图像看起来不正确,并且此代码输出空白的jpg-甚至不是全黑或全白,它只是在打开图像文件时给出错误。任何帮助将不胜感激!