首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拼接最终大小和偏移

拼接最终大小和偏移
EN

Stack Overflow用户
提问于 2013-07-22 18:06:47
回答 3查看 2.4K关注 0票数 2

我正在用opencv和Python拼接。所有的工作都很好,除了一件事:我没有设法计算结果图片的确切最终大小。我的图像总是太大,而且我有黑色的边框。此外,偏移量似乎不正确,因为在图片合并的地方有一条黑线。

下面是我的函数:

代码语言:javascript
复制
    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

我的结果是:

第一张图片:

第二张图片:

拼接后的图片:

我做错了什么?

EN

回答 3

Stack Overflow用户

发布于 2014-07-05 19:07:55

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
if (homography*(0,0,1))[0][2] > 0:
    offset[0] = 0
if (homography*(0,0,1))[1][2] > 0:
    offset[1] = 0
票数 2
EN

Stack Overflow用户

发布于 2013-09-19 21:35:13

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

代码语言:javascript
复制
perspectiveTransform( obj_original_corners, scene_corners, homography);

在那之后,我只搜索了smallest_Xsmallest_Ybiggest_Xbiggest_Y这两张图片。

然后我在以下代码中使用了这些数字:

代码语言:javascript
复制
cv::warpPerspective(img_2,WarpedImage,homography,cv::Size(biggestX-smallestX,biggestY-smallestY));

因此,在这种情况下,新图像本身将具有适当的大小,即使第二个图像具有负x或负y。

此时此刻,我唯一还在与自己斗争的是如何将这种转变应用于warpPerspective,因为现在我的图像的一部分由于负数而被截断。

票数 1
EN

Stack Overflow用户

发布于 2014-06-29 12:10:18

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

代码语言:javascript
复制
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事实上,一些像素看起来像黑色,但它不是纯粹的黑色,非常接近黑色。所以这些黑像素似乎不会被你的程序过滤掉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17784810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档