首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python OpenCV实时图像拼接(n = 5)性能优化

Python OpenCV实时图像拼接(n = 5)性能优化
EN

Stack Overflow用户
提问于 2019-01-10 10:03:29
回答 1查看 3.1K关注 0票数 3

我需要在飞行中缝上五个视频流。摄像机记录这些视频被并排安装在机架上,永远不会改变它们之间的相对位置。因此,单形矩阵是静态的。

我遵循这个github回购的方法:

从中间图像开始,首先将图像缝到左边,然后再将剩余的图像缝到右侧。

回购程序的代码工作正常,但速度慢得令人痛苦。我已经能够显着地提高它的性能(因子300),但它仍然需要0.25秒来缝制五幅图片的全景(在2015年Macbook Pro上)。

慢的部分:将cv2.warpPerspective(...)的每个结果应用到拼接到那个点的图像上。目前,我正在使用alpha通道,并在这就是答案的启发下将这两幅图像混合起来。,正是这种混合使缝纫变慢了。

(伪)代码:

代码语言:javascript
复制
def blend_transparent(background, foreground):
    overlay_img = foreground[:, :, :3]  # Grab the BRG planes 
    overlay_mask = foreground[:, :, 3:]  # And the alpha plane

    # Again calculate the inverse mask
    background_mask = 255 - overlay_mask

    # Turn the masks into three channel, so we can use them as weights
    overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR)
    background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR)

    # Create a masked out background image, and masked out overlay
    # We convert the images to floating point in range 0.0 - 1.0
    background_part = (background * (1 / 255.0)) * (background_mask * (1 / 255.0))
    overlay_part = (overlay_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0))

    # And finally just add them together, and rescale it back to an 8bit integer image
    return np.uint8(
        cv2.addWeighted(background_part, 255.0, overlay_part, 255.0, 0.0)
    )


for image in right_images:
    warped_image = cv2.warpPerspective(image, ...)
    mask = np.zeros(
        (warped_image.shape[0], warped_image.shape[1], 4), 
        dtype="uint8"
    )
    mask[0 : previously_stitched.shape[0], 0 : previously_stitched.shape[1]] = previously_stitched
    mask_rgb = mask[:, :, :3]  # Grab the BRG planes
    previously_stitched = blend_transparent(mask_rgb, warped_image)

,所以我的问题是:有没有一种更有效的方法将扭曲的图像应用于现有的全景图?

我的完整工作代码是用这个储存库编写的。

免责声明:我是一个网页开发人员,我对计算机视觉的知识是非常有限的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-10 13:08:46

当图像具有透明度时,Alpha通道非常有用,但是在这里,您可以通过转换手动添加alpha通道。这个通道可以用来存储计算,但我认为你会失去性能。我建议blend_transparent使用以下函数:

代码语言:javascript
复制
def blend_transparent(self, background, foreground):
    # Split out the transparency mask from the colour info
    overlay_img = foreground[:, :, :3]  # Grab the BRG planes

    res = background

    only_right = np.nonzero((np.sum(overlay_img, 2) != 0) * (np.sum(background,2) == 0))
    left_and_right = np.nonzero((np.sum(overlay_img, 2) != 0) * (np.sum(background,2) != 0))

    res[only_right] = overlay_img[only_right]
    res[left_and_right] = res[left_and_right]*0.5 + overlay_img[left_and_right]*0.5
    return res

在这里,如果当前没有设置任何值,则在结果中设置正确图像像素的值。如果已经设置了一个值,则从左和右计算值的平均值。计算时间除以1.6倍。

由于您的投影是冻结的,所以不需要每次计算索引only_right和left_and_right,我们可以计算它们一次并存储它们。这样做,您应该将计算时间除以4倍。

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

https://stackoverflow.com/questions/54126186

复制
相关文章

相似问题

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