簡體   English   中英

OpenCV將多個幀轉換成視頻

[英]OpenCV multiple frames into video

我正在嘗試使用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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM