我正在做一个项目,要求我将图像缝合在一起。由于可以计算大量可能的关键点,因此我决定对建筑物进行测试。我一直在遵循几本指南,但是本指南中https://towardsdatascience.com/image-stitching-using-opencv-817779c86a83是2-3张图像效果最好的指南。我决定缝合多个图像的方式是缝合前两个图像,然后获取输出,然后将其与第三个图像缝合,依此类推。我对图像描述符的匹配很有信心。但是随着我缝制越来越多的图像,先前的缝制部分被越来越远地推入-z轴。意味着它们变得失真并且更小。我用来完成此操作的代码如下:
import cv2
import numpy as np
import os
os.chdir('images')
img_ = cv2.imread('Output.jpg', cv2.COLOR_BGR2GRAY)
img = cv2.imread('DJI_0019.jpg', cv2.COLOR_BGR2GRAY)
#Setting up orb key point detector
orb = cv2.ORB_create()
#using orb to compute keypoints and descriptors
kp, des = orb.detectAndCompute(img_, None)
kp2, des2 = orb.detectAndCompute(img, None)
print(len(kp))
#Setting up BFmatcher
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=False)
matches = bf.knnMatch(des, des2, k=2) #Find 2 best matches for each descriptors (This is required for ratio test?)
#Using lowes ratio test as suggested in paper at .7-.8
good = []
for m in matches:
if m[0].distance < .8 * m[1].distance:
good.append(m)
matches = np.asarray(good) #matches is essentially a list of matching descriptors
#Aligning the images
if(len(matches)) >= 4:
src = np.float32([kp[m.queryIdx].pt for m in matches[:, 0]]).reshape(-1, 1, 2)
dst = np.float32([kp2[m.trainIdx].pt for m in matches[:, 0]]).reshape(-1, 1, 2)
#Creating the homography and mask
H, masked = cv2.findHomography(src, dst, cv2.RANSAC, 5.0)
print(H)
else:
print("Could not find 4 good matches to find homography")
dst = cv2.warpPerspective(img_, H, (img.shape[1] + 900, img.shape[0]))
dst[0:img.shape[0], 0:img.shape[1]] = img
cv2.imwrite("Output.jpg", dst)
如您所见,图像以一种奇怪的方式变得越来越远。我对于发生此类事件的理论是由于摄像头的位置和拍摄角度,但我不确定。如果是这种情况,是否有最佳参数可以产生最佳拼接图像?
有没有一种方法可以解决此问题,使内容可以相对于x轴“冲洗”?
编辑:添加源图像:https://imgur.com/zycPQuV