如何使用python在视频中添加类似虚拟广告牌的内容

时间:2018-07-01 06:11:37

标签: python opencv mask homography

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

enter image description here

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

enter image description here

当车辆通过该黄色区域时,该区域应由车辆覆盖,而不是覆盖车辆。

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

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)

哪个给我:enter image description here

但是,如果我在每个视频帧上都这样做,黄色区域将仅覆盖经过它的车辆,而不是被车辆覆盖。如何使它像一个虚拟的广告牌,将被视频中的移动对象覆盖?

0 个答案:

没有答案