【问题标题】:Why does my panorama stitch black images instead of actual images?为什么我的全景图拼接的是黑色图像而不是实际图像?
【发布时间】:2021-04-11 07:02:15
【问题描述】:

我想使用 openCV(不使用 Stitcher 类)将一些图像(相同分辨率)拼接成全景图。我尝试了算法described here,但我得到的不是所需的全景图,而是由最后一个要拼接的图像和一个大的黑色区域组成的图像。我每次迭代都输出了一个图像,结果是一样的:当前图像+每次都更大的黑色区域。

import numpy
import cv2

# images is an array of images that i need to stitch

matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_BRUTEFORCE_HAMMING)
ORB = cv2.ORB_create()
homography = 0
panorama = 0

for image in range(0, len(images) -1):
    key1, desc1 = ORB.detectAndCompute(images[image], None)
    key2, desc2 = ORB.detectAndCompute(images[image + 1], None)

    matches = matcher.match(desc1, desc2, None)
    matches = sorted(matches, key=lambda x: x.distance, reverse=True)
    numGoodMatches = int(len(matches) * 0.15)
    matches2 = matches[-numGoodMatches:]

    points1 = numpy.zeros((len(matches2), 2), dtype=numpy.float32)
    points2 = numpy.zeros((len(matches2), 2), dtype=numpy.float32)

    for i, match in enumerate(matches2):
        points1[i, :] = key1[match.queryIdx].pt
        points2[i, :] = key2[match.trainIdx].pt

    h, mask = cv2.findHomography(points2, points1, cv2.RANSAC)

    if isinstance(homography, int):
        homography = h
        img1H, img1W = images[image].shape
        img2H, img2W = images[image + 1].shape
        aligned = cv2.warpPerspective(images[image + 1], h, (img1W + img2W, img2H))
        stitchedImage = numpy.copy(aligned)
        stitchedImage[0:img1H, 0:img2W] = images[image]
        panorama = stitchedImage
    else:
        h *= homography
        homography = h
        img1H, img1W = panorama.shape
        img2H, img2W = images[image + 1].shape
        aligned = cv2.warpPerspective(images[image + 1], h, (img1W + img2W, img2H))
        stitchedImage = numpy.copy(aligned)
        stitchedImage[0:img1H, 0:img2W] = images[image + 1]
        panorama = stitchedImage

我得到的图像示例:

第一张图是正确的。

最后一张图片的宽度正确(n * 原始宽度),但只有一张图片,其余为黑色区域。

【问题讨论】:

  • 请调试这个。显示每一步并查看它。
  • 我为每次迭代添加了一个imsave 并获得了我发布的图像(加上另外 2 个与上一个类似的图像),我可以看到每次迭代后,保存的图像是当前图像加上一个额外的黑色width(与上一次相比)。但我不知道为什么
  • 不,我的意思是 更精细 步骤,而不是每次迭代一次。

标签: python opencv matplotlib computer-vision


【解决方案1】:

这个

stitchedImage[0:img1H, 0:img2W] = images[image + 1]

将图像放置在“stitchedImage”的左上角。

必须为全景图的每个部分计算图像 x 偏移量:

stitchedImage[0:img1H, x_offset:x_offset+img2W] = images[image + 1]

【讨论】:

  • 感谢您的提示。它仍然不起作用,但我越来越近了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 2010-11-11
  • 2021-11-18
  • 2023-02-07
  • 2014-07-28
  • 2020-10-28
相关资源
最近更新 更多