簡體   English   中英

使用 Opencv 將單個幀合並到視頻文件

[英]Merge individual frame to video file using Opencv

我正在嘗試使用 Opencv 將單個幀堆疊到視頻文件中。 我想將兩個不同的代碼組合在一起來制作單獨的框架。 以下代碼幫助我提取單個幀,

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('file_data.mp4',fourcc,20 (1920,1080),False)
while True:
    ret, frame=cap.read()
    mask = object_detector.apply(frame)
    _, mask  = cv2.threshold(mask,254,255,cv2.THRESH_BINARY)       
    contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    res = cv2.bitwise_and(frame,frame,mask=mask)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>1000:   
            #print("Area of contour:", area)
            cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
            cv2.imwrite("file%d.jpg"%count, frame)
            out.write(frame)
    if cv2.waitKey(1) and 0xFF == ord('q'):
        break

我嘗試將單個幀存儲在數組中,但是沒有用。 它沒有顯示任何錯誤,但電腦崩潰。

fps = 20, , 寬度 = 1920, 高度 = 1080

感謝羅特姆。 該問題已使用 Opencv 的 VideoWriter 解決。 工作代碼如下。

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('file_data.mp4',fourcc,20,(1920,1080), True)
while True:
ret, frame=cap.read()
if ret == True:
    # frame[frame<=thresholds]=0
    mask = object_detector.apply(frame)
    _, mask  = cv2.threshold(mask,254,255,cv2.THRESH_BINARY)       
    contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    res = cv2.bitwise_and(frame,frame,mask=mask)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>1000:    
            cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
            out.write(frame)
    break
if cv2.waitKey(20) == ord('q'):
    break

暫無
暫無

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

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