亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 opencv 調整視頻大小并保存

使用 opencv 調整視頻大小并保存

LEATH 2023-07-18 10:39:24
我正在嘗試使用 opencv 重新調整視頻大小,然后將其保存回我的系統。代碼有效并且不會給出任何錯誤,但輸出視頻文件已損壞。我使用的 fourcc 是 mp4v,與 .mp4 配合良好,但輸出視頻仍然損壞。需要幫忙。import numpy as np    import cv2    import sys    import re    vid=""        if len(sys.argv)==3:        vid=sys.argv[1]        compress=int(sys.argv[2])    else:        print("File not mentioned or compression not given")        exit()        if re.search('.mp4',vid):        print("Loading")    else:        exit()        cap = cv2.VideoCapture(0)    ret, frame = cap.read()        def rescale_frame(frame, percent=75):        width = int(frame.shape[1] * percent/ 100)        height = int(frame.shape[0] * percent/ 100)        dim = (width, height)        return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)        FPS= 15.0    FrameSize=(frame.shape[1], frame.shape[0])    fourcc = cv2.VideoWriter_fourcc(*'mp4v')        out = cv2.VideoWriter('Video_output.mp4', fourcc, FPS, FrameSize, 0)        while(cap.isOpened()):        ret, frame = cap.read()            # check for successfulness of cap.read()        if not ret: break                rescaled_frame=rescale_frame(frame,percent=compress)        # Save the video        out.write(rescaled_frame)            cv2.imshow('frame',rescaled_frame)        if cv2.waitKey(1) & 0xFF == ord('q'):             break        cap.release()    out.release()    cv2.destroyAllWindows()
查看完整描述

1 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

問題是VideoWriter初始化。


您初始化了:


out = cv2.VideoWriter('Video_output.mp4', fourcc, FPS, FrameSize, 0)

最后一個參數的0意思是,isColor = False。您告訴我們,您要將幀轉換為灰度然后保存。但是您的代碼中沒有轉換。


此外,您還根據compress參數調整代碼中每個框架的大小。


如果我使用默認的壓縮參數:


cap = cv2.VideoCapture(0)


if cap.isOpened():

    ret, frame = cap.read()

    rescaled_frame = rescale_frame(frame)

    (h, w) = rescaled_frame.shape[:2]

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')

    writer = cv2.VideoWriter('Video_output.mp4',

                             fourcc, 15.0,

                             (w, h), True)

else:

    print("Camera is not opened")

現在我們已經VideoWriter用所需的尺寸初始化了 。


完整代碼:


import time

import cv2



def rescale_frame(frame_input, percent=75):

    width = int(frame_input.shape[1] * percent / 100)

    height = int(frame_input.shape[0] * percent / 100)

    dim = (width, height)

    return cv2.resize(frame_input, dim, interpolation=cv2.INTER_AREA)



cap = cv2.VideoCapture(0)


if cap.isOpened():

    ret, frame = cap.read()

    rescaled_frame = rescale_frame(frame)

    (h, w) = rescaled_frame.shape[:2]

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')

    writer = cv2.VideoWriter('Video_output.mp4',

                             fourcc, 15.0,

                             (w, h), True)

else:

    print("Camera is not opened")


while cap.isOpened():

    ret, frame = cap.read()


    rescaled_frame = rescale_frame(frame)


    # write the output frame to file

    writer.write(rescaled_frame)


    cv2.imshow("Output", rescaled_frame)

    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):

        break



cv2.destroyAllWindows()

cap.release()

writer.release()

可能的問題:我不想更改VideoWriter參數,該怎么辦?


答案:那么你需要將框架更改為灰色圖像:


while cap.isOpened():

    # grab the frame from the video stream and resize it to have a

    # maximum width of 300 pixels

    ret, frame = cap.read()


    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)


查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 447 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號