【发布时间】:2022-08-11 04:18:12
【问题描述】:
我已经对 python 上的图像拼接进行了相当多的搜索,大多数都是针对全景图像,扭曲和旋转图像以将它们组合成一个。
我正在尝试做的是使用计算机图像,因此它们是数字的并且可以毫无问题地进行模板匹配,它始终是 2D 而无需变形。
基本上在这里我有一张放大的地图,我想为这些小图片制作一张大图,这里我们使用了所有图片:https://imgur.com/a/HZIeT3z
import os
import numpy as np
import cv2
def stitchImagesWithoutWarp(img1, img2):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)
good_matches = matches[:10]
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
start = (abs(int(dst_pts[0][0][0]-src_pts[0][0][0])), abs(int(dst_pts[0][0][1]-src_pts[0][0][1])))
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
vis = np.zeros((start[1]+h1,start[0]+w1,3), np.uint8)
vis[start[1]:start[1]+h1, start[0]:start[0]+w1, :3] = img1
vis[:h2, :w2, :3] = img2
return vis
imgList = []
for it in os.scandir(\"images\"):
imgList.append(cv2.imread(it.path))
vis = stitchImagesWithoutWarp(imgList[0],imgList[1])
for index in range(2,len(imgList)):
cv2.imshow(\"result\", vis)
cv2.waitKey()
vis = stitchImagesWithoutWarp(vis,imgList[index])
但是一旦我缝合第五张图像,它似乎有错误的匹配并且不正确,但我总是在 NORM_HAMMING 上得到最佳的距离匹配,结果如下:
问题是,这是第一张图像,按此顺序,最佳匹配点(var开始) 在 x 轴上为负数,这里是 imgur 顺序中的匹配点:
- (7, 422)
- (786, 54)
- (394, 462)
- (-350, 383)
我尝试切换顶部图像,为否定匹配执行特定代码,但我相信我正在偏离性能。
还从码头注意到第一个图像应该是查询,第二个应该是目标,但我无法通过反转可见函数参数中的变量。
标签: python opencv feature-detection