Python 缝合最终尺寸和偏移量

Python 缝合最终尺寸和偏移量,python,opencv,image-resizing,homography,image-stitching,Python,Opencv,Image Resizing,Homography,Image Stitching,我正在用opencv和Python进行缝合。除了一件事之外,所有的方法都很有效:我无法计算出结果图片的确切最终大小。 我的图像总是太大,我有黑色的边框。此外,偏移似乎不正确,因为在图片合并的地方有一条黑线 以下是我的功能: def calculate_size(size_image1, size_image2, homography): ## Calculate the size and offset of the stitched panorama. off

我正在用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
我的结果是:

第一张图片:

第二张图片:

缝合图像:


我做错了什么?

嗯,我对Python了解不多,但基本上我遇到了一些问题。 为了解决尺寸问题,我做了以下工作:

perspectiveTransform( obj_original_corners, scene_corners, homography);
if (homography*(0,0,1))[0][2] > 0:
    offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
    offset[1] = 0
在那之后,我只是在两幅图像中搜索了
最小的
最小的
最大的
最大的

这些数字我随后用于:

cv::warpPerspective(img_2,WarpedImage,homography,cv::Size(biggestX-smallestX,biggestY-smallestY));
因此,在这种情况下,新图像本身将具有适当的大小,即使第二个图像具有负x或负y


目前我唯一还在努力解决的问题是如何将转换应用到
透视图
,因为现在我的部分图像由于负数而被截断。

根据缝合,您的所有过程都是正确的。结果是因为您的源图片

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]
该操作只过滤颜色为零的像素。事实上,有些像素看起来像黑色,但它不是纯黑色,非常接近黑色。所以这些黑色像素似乎不会被你的程序过滤掉

if (homography*(0,0,1))[0][1] > 0:
    offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
    offset[1] = 0
您的代码错误。正确的代码如下:

perspectiveTransform( obj_original_corners, scene_corners, homography);
if (homography*(0,0,1))[0][2] > 0:
    offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
    offset[1] = 0