python - OpenCV将多个帧转换成视频

标签 python opencv

我正在尝试使用opencv将多个帧组合到一个视频中。
这可能吗?

我的程序可以保存多张图像,但不能将它们合并为一个视频。

import time
import cv2
import mss
import numpy

frame = 1
with mss.mss() as sct:
    # Part of the screen to capture
    monitor = {'top': 40, 'left': 0, 'width': 800, 'height': 640}

    while 'Screen capturing':
        last_time = time.time()

        # Get raw pixels from the screen, save it to a Numpy array
        img = numpy.array(sct.grab(monitor))

        # Display the picture
        frame += 1    
        name = 'C:/Users/samih/SublimePython/Game_Play_Recorder/Imgs/img' + str(frame) + '.png'
        cv2.imwrite(name, img)





        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

它应该能够将所有这些图像转换为一个视频。

最佳答案

您正在寻找的是VideoWriter。请参见的官方教程,位于保存视频下。

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,  480))
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    frame = cv.flip(frame, 0)
    # write the flipped frame
    out.write(frame)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('q'):
        break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()

关于python - OpenCV将多个帧转换成视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57334225/

相关文章:

c++ - OpenCV 中 1 平面图像的位平面仅适用于图像的 1/3

python - 在 Pandas 中,选择标签包含指定级别的给定字符串的所有列

python - 如何检查 django_tables2 对象为空/null?

python - 无法在 PyCharm 中设置 PYTHONPATH

Python Django 测试两个异常之一

c++ - 无法使用 findcontours 和 opencv 找到所有轮廓

c++ - 从源代码 : *** No targets specified and no makefile found. 停止编译 openCV

python - 在 Cassandra 中创建现有 key 空间的副本(使用新名称)

opencv - 如何将 k4a_image_t 转换为 opencv 矩阵? (Azure Kinect 传感器 SDK)

python - 改进在前景蒙版中遇到分割对象时的背景减除