在将 24 个拼接图像的结果拼接到下一个第 25 个图像后,我得到了如下所示的输出。在那之前缝合很好。
有谁知道为什么/什么时候拼接的输出是这样的?这样输出的可能性是什么?这可能是什么原因?
拼接代码遵循标准拼接步骤,例如查找关键点、描述符然后匹配点、计算单应性然后对图像进行变形。但我不明白为什么会出现这种输出。
拼接的核心部分如下:
detector = cv2.SIFT_create(400)
# find the keypoints and descriptors with SIFT
gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
ret1, mask1 = cv2.threshold(gray1,1,255,cv2.THRESH_BINARY)
kp1, descriptors1 = detector.detectAndCompute(gray1,mask1)
gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
ret2, mask2 = cv2.threshold(gray2,1,255,cv2.THRESH_BINARY)
kp2, descriptors2 = detector.detectAndCompute(gray2,mask2)
keypoints1Im = cv2.drawKeypoints(image1, kp1, outImage = cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(0,0,255))
keypoints2Im = cv2.drawKeypoints(image2, kp2, outImage = cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(0,0,255))
# BFMatcher with default params
matcher = cv2.BFMatcher()
matches = matcher.knnMatch(descriptors2,descriptors1, k=2)
# Apply ratio test
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
print (str(len(good)) + " Matches were Found")
if len(good) <= 10:
return image1
matches = copy.copy(good)
matchDrawing = util.drawMatches(gray2,kp2,gray1,kp1,matches)
#Aligning the images
src_pts = np.float32([ kp2[m.queryIdx].pt for m in matches ]).reshape(-1,1,2)
dst_pts = np.float32([ kp1[m.trainIdx].pt for m in matches ]).reshape(-1,1,2)
H = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)[0]
h1,w1 = image1.shape[:2]
h2,w2 = image2.shape[:2]
pts1 = np.float32([[0,0],[0,h1],[w1,h1],[w1,0]]).reshape(-1,1,2)
pts2 = np.float32([[0,0],[0,h2],[w2,h2],[w2,0]]).reshape(-1,1,2)
pts2_ = cv2.perspectiveTransform(pts2, H)
pts = np.concatenate((pts1, pts2_), axis=0)
# print("pts:", pts)
[xmin, ymin] = np.int32(pts.min(axis=0).ravel() - 0.5)
[xmax, ymax] = np.int32(pts.max(axis=0).ravel() + 0.5)
t = [-xmin,-ymin]
Ht = np.array([[1,0,t[0]],[0,1,t[1]],[0,0,1]]) # translate
result = cv2.warpPerspective(image2, Ht.dot(H), (xmax-xmin, ymax-ymin))
resizedB = np.zeros((result.shape[0], result.shape[1], 3), np.uint8)
resizedB[t[1]:t[1]+h1,t[0]:w1+t[0]] = image1
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(result,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 0, 255, cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
k1 = (kernel == 1).astype('uint8')
mask = cv2.erode(mask, k1, borderType=cv2.BORDER_CONSTANT)
mask_inv = cv2.bitwise_not(mask)
difference = cv2.bitwise_or(resizedB, resizedB, mask=mask_inv)
result2 = cv2.bitwise_and(result, result, mask=mask)
result = cv2.add(result2, difference)
编辑:
我总共有 97 张图像要缝合。如果我分别缝合 24 和 25 图像,它们会正确缝合。如果我从第 23 张图像开始缝合,那么缝合也很好,但是当我从第 1 张图像开始缝合时它会给我带来问题。我无法理解这个问题。
拼接第 25 张图像后的结果如上所示,其中出错了。
奇怪的观察:如果我用相同的代码分别缝合 23、24、25 个图像,它会被缝合。如果我在 23 到 97 之后缝合图像,它会被缝合。但不知何故,如果我从第 1 个图像缝合图像,它会在缝合第 25 个图像时中断。我不明白为什么会这样。
我尝试了不同的组合,例如不同的关键点检测、提取方法、匹配方法、不同的单应性计算、不同的扭曲代码,但这些组合不起作用。步骤组合代码中缺少某些内容或错误。我无法弄清楚。
对不起这个长长的问题。由于我对此完全陌生,因此我无法正确解释和理解这些内容。感谢您的帮助和指导。
使用不同的代码(在缝合之间给出黑线),如果我缝合了 97 个图像,那么第 25 个在缝合和缝合中上升,如下所示(右角点):