0

我正在做一个需要我将图像拼接在一起的项目。由于可以计算出大量可能的关键点,我决定用建筑物进行测试。我一直在关注几个指南,但对 2-3 张图像效果最好的指南是本指南:https ://towardsdatascience.com/image-stitching-using-opencv-817779c86a83 。我决定拼接多个图像的方式是拼接前两个,然后获取输出,然后将其与第三个图像拼接,依此类推。我对图像描述符的匹配很有信心。但随着我缝合越来越多的图像,之前缝合的部分会被越来越多地推入 -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)

第 4 针以上的输出如下所示: 在此处输入图像描述

正如你所看到的,图像以一种奇怪的方式变得越来越远。我对这种事件发生的理论是由于拍摄图像的相机位置和角度,但我不确定。如果可能是这种情况,是否有最佳参数可以产生最佳拼接图像?

有没有办法解决这个问题,可以将内容“刷新”到 x 轴上?

编辑:添加源图像:https ://imgur.com/zycPQuV

4

0 回答 0