我正在使用 opencv 和 Python 进行拼接。一切都很好,除了一件事:我无法计算结果图片的确切最终尺寸。我的图像总是太大而且我有黑色边框。此外,偏移似乎不正确,因为图片合并有一条黑线。
这是我的功能:
def calculate_size(size_image1, size_image2, homography):
## Calculate the size and offset of the stitched panorama.
offset = abs((homography*(size_image2[0]-1,size_image2[1]-1,1))[0:2,2])
print offset
size = (size_image1[1] + int(offset[0]), size_image1[0] + int(offset[1]))
if (homography*(0,0,1))[0][1] > 0:
offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
offset[1] = 0
## Update the homography to shift by the offset
homography[0:2,2] += offset
return (size, offset)
## 4. Combine images into a panorama. [4] --------------------------------
def merge_images(image1, image2, homography, size, offset, keypoints):
## Combine the two images into one.
panorama = cv2.warpPerspective(image2,homography,size)
(h1, w1) = image1.shape[:2]
for h in range(h1):
for w in range(w1):
if image1[h][w][0] != 0 or image1[h][w][3] != 0 or image1[h][w][4] != 0:
panorama[h+offset[1]][w + offset[0]] = image1[h][w]
## TODO: Draw the common feature keypoints.
return panorama
我的结果:
第一张图片:
第二张图片:
拼接图像:
我究竟做错了什么?