python - 如何在 Python 中捕获的视频上插入图像

标签 python opencv image-processing cv2

我使用 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()

如果我的假设是错误的,请提供更多详细信息。

关于python - 如何在 Python 中捕获的视频上插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56371365/

相关文章:

python - 如何在数据框中查找其间带有下划线文本的字符串

python - 绘制分类器的决策边界,ValueError : X has 2 features per sample; expecting 908430"

c - 从图像中提取较小的图像

ios - 为什么 cv::resize 这么慢?

wpf - 在 WPF 中显示 CUDA 处理的图像

android - 如何将Android ARGB图像格式转换为opencv中使用的RGB图像格式?

python - 检测快速移动球的最佳 OpenCV 算法?

python - 如何使用线性插值法对 numpy 数组进行插值

python - 为什么 setup_requires 不能为 numpy 正常工作?

python - 使用 OpenCV 进行对象跟踪