【问题标题】:Overlay transparent video to camera feed OpenCV将透明视频叠加到相机馈送 OpenCV
【发布时间】:2020-05-01 21:33:39
【问题描述】:

我一直在寻找如何用 Python(或实际上用任何语言)将透明视频叠加到相机源上,我见过的最接近的方法是使用 opencv

我按照教程here 做了一些实验。一个是在while 循环中添加一个新的VideoCapture,以便在从相机捕获视频时播放文件中的视频;但视频不会显示。

我遇到的其他事情是混合视频和摄像头,但并没有真正做叠加。

我迷失了方向,非常感谢任何有关如何以编程方式进行操作的教程或链接。

更新:这是关于同时逐帧和逐时间加载摄像机源和透明视频。

【问题讨论】:

  • 您的问题是关于如何逐帧/逐时间同步加载两个视频,或者关于如何根据一些透明度信息合并两个图像?
  • 它是关于同时逐帧和逐时间加载两个视频(相机馈送和透明视频)。谢谢@Micka,会更新问题
  • 请分享您的代码。
  • 所以两个视频的帧率完全相同?否则逐帧和逐时间是不可能的。只需使用 2x VideoCapture 并在每次迭代中从两个 videoCapture 中读取 => 加载已解决。
  • @Micka 同步是可能的。完成后我将发布代码。此外,相机通常没有帧率,而是取决于特定时间的 CPU 使用率。

标签: python opencv video-streaming video-processing video-capture


【解决方案1】:
import cv2
import time
import numpy as np

current_milli_time = lambda: int(round(time.time() * 1000))

# Camera feed
cap_cam = cv2.VideoCapture(0)
if not cap_cam.isOpened():
    print('Cannot open camera')
    exit()
ret, frame_cam = cap_cam.read()
if not ret:
    print('Cannot open camera stream')
    cap_cam.release()
    exit()

# Video feed
filename = 'myvideo.mp4'
cap_vid = cv2.VideoCapture(filename)
if not cap_cam.isOpened():
    print('Cannot open video: ' + filename)
    cap_cam.release()
    exit()
ret, frame_vid = cap_vid.read()
if not ret:
    print('Cannot open video stream: ' + filename)
    cap_cam.release()
    cap_vid.release()
    exit()

# Specify maximum video time in milliseconds
max_time = 1000 * cap_vid.get(cv2.CAP_PROP_FRAME_COUNT) / cap_vid.get(cv2.CAP_PROP_FPS)

# Resize the camera frame to the size of the video
height = int(cap_vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap_vid.get(cv2.CAP_PROP_FRAME_WIDTH))

# Starting from now, syncronize the videos
start = current_milli_time()

while True:
    # Capture the next frame from camera
    ret, frame_cam = cap_cam.read()
    if not ret:
        print('Cannot receive frame from camera')
        break
    frame_cam = cv2.resize(frame_cam, (width, height), interpolation = cv2.INTER_AREA)

    # Capture the frame at the current time point
    time_passed = current_milli_time() - start
    if time_passed > max_time:
        print('Video time exceeded. Quitting...')
        break
    ret = cap_vid.set(cv2.CAP_PROP_POS_MSEC, time_passed)
    if not ret:
        print('An error occured while setting video time')
        break
    ret, frame_vid = cap_vid.read()
    if not ret:
        print('Cannot read from video stream')
        break

    # Blend the two images and show the result
    tr = 0.3 # transparency between 0-1, show camera if 0
    frame = ((1-tr) * frame_cam.astype(np.float) + tr * frame_vid.astype(np.float)).astype(np.uint8)
    cv2.imshow('Transparent result', frame)
    if cv2.waitKey(1) == 27: # ESC is pressed
        break

cap_cam.release()
cap_vid.release()
cv2.destroyAllWindows()

【讨论】:

  • 这种方法在一个循环的时间间隔内跳过视频的帧。由于相机没有恒定的帧速率,因此您需要标记视频帧并在实时操作之后重复相机帧,以免造成流程负担过重,如果您想要逐帧-帧结果。
猜你喜欢
  • 1970-01-01
  • 2021-07-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多