繁体   English   中英

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

[英]Python OpenCV real time image stitching (n = 5) performance optimization

我需要即时缝合五个视频流。 记录视频的摄像机并排安装在机架上,并且永远不会改变它们的相对位置。 因此,单应性矩阵是静态的。

我正在遵循这个github repo的方法:

从中心图像开始,您首先向左缝合,然后将其余图像向右缝合。

该仓库中的代码可以正常工作,但是速度很慢。 我已经能够大幅提高其性能(系数300),但拼接五张图片的全景图像仍然需要0.25秒(在2015年的Macbook Pro上)。

较慢的部分:将cv2.warpPerspective(...)每个结果cv2.warpPerspective(...)缝合的图像。 我目前正在通过使用Alpha通道并混合两个图像来实现这一点受此SO Answer的启发。 正是这种混合使缝合变慢。

(伪)代码:

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)

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

我完整的工作代码在此存储库中

免责声明:我是一名Web开发人员,对计算机视觉的了解非常有限。

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

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。

由于您的投影是冻结的,因此不需要每次都只计算index_right和left_and_right,我们可以计算一次并存储它们。 这样做,您应该将计算时间除以4。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM