我正在用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我的结果是:
第一张图片:

第二张图片:

拼接后的图片:

我做错了什么?
发布于 2014-07-05 19:07:55
if (homography*(0,0,1))[0][1] > 0:
offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
offset[1] = 0你的代码是wrong.The right,如下所示:
if (homography*(0,0,1))[0][2] > 0:
offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
offset[1] = 0发布于 2013-09-19 21:35:13
嗯,我对Python了解不多,但基本上我遇到了一些问题。为了解决大小问题,我做了以下操作:
perspectiveTransform( obj_original_corners, scene_corners, homography);在那之后,我只搜索了smallest_X,smallest_Y,biggest_X和biggest_Y这两张图片。
然后我在以下代码中使用了这些数字:
cv::warpPerspective(img_2,WarpedImage,homography,cv::Size(biggestX-smallestX,biggestY-smallestY));因此,在这种情况下,新图像本身将具有适当的大小,即使第二个图像具有负x或负y。
此时此刻,我唯一还在与自己斗争的是如何将这种转变应用于warpPerspective,因为现在我的图像的一部分由于负数而被截断。
发布于 2014-06-29 12:10:18
根据拼接,你所有的过程都是right.The结果,因为你的源图片。
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]该操作只过滤像素,它的颜色是zero.In事实上,一些像素看起来像黑色,但它不是纯粹的黑色,非常接近黑色。所以这些黑像素似乎不会被你的程序过滤掉。
https://stackoverflow.com/questions/17784810
复制相似问题