【问题标题】:Stitching final size and offset拼接最终尺寸和偏移量
【发布时间】:2013-07-21 00:35:39
【问题描述】:

我正在使用 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 opencv image-resizing homography image-stitching


    【解决方案1】:

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

    perspectiveTransform( obj_original_corners, scene_corners, homography);
    

    之后,我只在两张图片中搜索了smallest_Xsmallest_Ybiggest_Xbiggest_Y

    这些数字我随后用于:

    cv::warpPerspective(img_2,WarpedImage,homography,cv::Size(biggestX-smallestX,biggestY-smallestY));
    

    所以在这种情况下,即使第二张图像的 x 或 y 为负,新图像本身也将具有适当的尺寸。

    此时我唯一还在为自己苦苦挣扎的是如何将转换应用到warpPerspective,因为现在我的部分图像由于负数而被截断。

    【讨论】:

      【解决方案2】:
      if (homography*(0,0,1))[0][1] > 0:
          offset[0] = 0
      if (homography*(0,0,1))[1][2] > 0:
          offset[1] = 0
      

      你的代码是错误的。正确的代码如下:

      if (homography*(0,0,1))[0][2] > 0:
          offset[0] = 0
      if (homography*(0,0,1))[1][2] > 0:
          offset[1] = 0
      

      【讨论】:

        【解决方案3】:

        根据拼接,你所有的过程都是对的。结果是因为你的源图片。

        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]
        

        该操作只过滤颜色为零的像素。实际上,某些像素看起来像黑色,但它不是纯黑色,非常接近黑色。所以这些看起来黑色的像素不会被你的程序过滤掉。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 2019-03-28
          • 1970-01-01
          相关资源
          最近更新 更多