首页 > 解决方案 > 如何使用 python 在视频中添加虚拟广告牌之类的东西

问题描述

虚拟广告牌常用于体育转播。这些广告牌不是真实的,它们会被它们面前的移动物体所覆盖,就像它就在那里一样。我想知道是否可以通过python做到这一点?以这个视频为例。(你可以通过这个下载它。)我们可以在车道上方但在车辆下方添加一些东西吗?让我们说这个简单的黄色矩形:

在此处输入图像描述

将其添加到车道将是这样的:

在此处输入图像描述

当车辆经过这个黄色区域时,该区域应该被车辆覆盖而不是覆盖车辆。

在处理视频之前,我正在尝试首先使用单应性和一些包装将矩形添加到图片中并使用以下代码拟合图片中的车道区域:

import cv2
import numpy as np

# source image
source_img = cv2.imread('yellow_rec.jpg')
size = source_img.shape
# get four corners of the source (clock wise)
pts_source = np.array(
                    [
                    [0,0],
                    [size[1] - 1, 0],
                    [size[1] - 1, size[0] -1],
                    [0, size[0] - 1 ]
                    ],dtype=float
                    )
#pts_source = np.array([[310,0], [440,0], [589,151],[383,151]])

# destination image
dst_img = cv2.imread('video_screen_shot.png')
# four corners in destination image (also clock wise):
pts_dst = np.array([[400,115], [531,108], [647,186],[533,244]])

# calculate homography
h, status = cv2.findHomography(pts_source, pts_dst)

# warp source image to destination based on homography
temp = cv2.warpPerspective(source_img, h, (dst_img.shape[1], dst_img.shape[0]))

# Black out polygonal area in destination image.
cv2.fillConvexPoly(dst_img, pts_dst.astype(int), 0, 16)

# Add warped source image to destination image.
dst_img = dst_img + temp

cv2.imshow('warpped', dst_img)
cv2.waitKey(0)

这给了我:在此处输入图像描述

但是,如果我在每个视频帧上都这样做,黄色区域只会覆盖经过它的车辆,而不是被车辆覆盖。我怎样才能让它像一个虚拟广告牌,被视频中的移动物体覆盖?

标签: pythonopencvmaskhomography

解决方案


推荐阅读