我正在用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
我的结果是:
第一张图片:
第二张图片:
缝合图像:
我做错什么了?在
根据缝纫工艺,你的所有工序是的。那个结果是因为你的源图片。在
该操作仅过滤像素,其颜色为零。在事实上,有些像素看起来像黑色,但它不是纯黑色,而且非常接近黑色。所以这些黑色像素看起来不会被你的程序过滤掉。在
你的代码是错了,那个右图如下:
^{pr2}$我基本上不知道Python的问题。 为了解决尺寸问题,我做了以下工作:
之后,我只在这两个图像中搜索}。在
smallest_X
、smallest_Y
、biggest_X
和{然后我用这些数字:
^{pr2}$所以在这种情况下,即使第二张图像的x或y为负数,新图像本身的大小也将是适当的
我现在唯一还在纠结的事情是如何将偏移应用到
warpPerspective
上,因为现在我的部分图像由于负数而被截断。在相关问题 更多 >
编程相关推荐