python - 如何在 Python 中的捕获视频上插入图像
问题描述
我使用 cv2.VideoCapured 捕获视频并显示。在同一时间捕获的视频显示未保存。如何在此捕获的视频上插入图像以同时显示。
解决方案
假设您想将图像直接添加到某个 x,y 位置的视频帧,而不进行任何颜色混合或图像透明度。您可以使用以下 python 代码:
#!/usr/bin/python3
import cv2
# load the overlay image. size should be smaller than video frame size
img = cv2.imread('logo.png')
# Get Image dimensions
img_height, img_width, _ = img.shape
# Start Capture
cap = cv2.VideoCapture(0)
# Get frame dimensions
frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH )
frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT )
# Print dimensions
print('image dimensions (HxW):',img_height,"x",img_width)
print('frame dimensions (HxW):',int(frame_height),"x",int(frame_width))
# Decide X,Y location of overlay image inside video frame.
# following should be valid:
# * image dimensions must be smaller than frame dimensions
# * x+img_width <= frame_width
# * y+img_height <= frame_height
# otherwise you can resize image as part of your code if required
x = 50
y = 50
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# add image to frame
frame[ y:y+img_height , x:x+img_width ] = img
# Display the resulting frame
cv2.imshow('frame',frame)
# Exit if ESC key is pressed
if cv2.waitKey(20) & 0xFF == 27:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
如果我的假设是错误的,请提供更多细节。
推荐阅读
- excel - 将 Excel 图表复制到 Word 文档的末尾
- python - 为什么 scipy.linalg.LU 重复求解 Ax = b 这么慢?
- c - 函数如何在不使用 malloc 的情况下返回结构?
- javascript - 为什么我的 content.js 脚本不能与我的 background.js 脚本(chrome 扩展)通信?
- html - 尝试从 C 服务器发送 HTML 但浏览器有问题
- javascript - Webpack 将 JavaScript 文件添加到包中,但文件不会在浏览器中运行
- php - PHP 致命错误:未捕获的 ArgumentCountError:函数 sendRegistrationErrorNotification() 的参数太少
- javascript - 在 Angular 中导入 umd 包
- jquery - 使用 optgroup 引导多选下拉菜单
- spring-webclient - 如何使用 Spring WebClient 传递错误正文和状态码