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

2024-10-03 02:44:36 发布

您现在位置:Python中文网/ 问答频道 /正文

虚拟广告牌是体育广播中常用的一种广告形式。这些广告牌不是真的,会被面前移动的物体覆盖,就像它就在那里一样。我想知道有没有可能通过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

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


Tags: image区域sourceimgsize视频npcv2