【问题标题】:OpenCV multiple frames into videoOpenCV多帧成视频
【发布时间】:2019-08-03 07:34:49
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

  • 看看这个tutorial 写/保存视频
  • 是的,这是可能的。 CV 具有在视频文件中写入帧的功能。视频必须使用与帧相同的大小(宽度、高度),否则您必须在保存之前调整帧大小。但是使用ffmpeg 会更容易——比如ffmpeg *.png output.mv4

标签: python opencv


【解决方案1】:

您正在寻找的是VideoWriter。请参阅 保存视频this page 上的官方教程。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 2015-05-20
    • 2011-09-09
    • 2016-12-22
    • 2014-02-20
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多