python - 缝合最终尺寸和偏移

标签 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.

      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

我的结果:

第一张图片:First image

第二张图片:Second Image

拼接图像:Stitched result

我做错了什么?

最佳答案

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

关于python - 缝合最终尺寸和偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784810/

相关文章:

java - 操作图像而不删除其 EXIF 数据

python - 如何在 Apache Beam Python 中获取窗口时间戳的结束

python - 如何从 matplotlib/seaborn 图中删除或隐藏 y 轴刻度标签

python - Pandas 嵌套 for 循环在创建的不同数据帧上插入多个数据

python - BeautifulSoup 选择 sibling 不工作

opencv - OpenCV 中的 cvGetRows() 函数有什么用?

Java Eclipse "process"关闭应用程序后未结束

c++ - 使用OpenCV在YAML文件中不能放置多个Mat对象

c# - 在 C# 中调整单色图像的大小

javascript - 如何在页面中尽可能大地显示整个图像(无滚动)保持其纵横比