Opencv2:Python:cv2.VideoWriter [英] Opencv2: Python: cv2.VideoWriter

查看:99
本文介绍了Opencv2:Python:cv2.VideoWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的代码没有保存视频?网络摄像头的帧速率是否必须与 VideoWriter 帧大小完全匹配?

 import numpy as np`enter code here导入 cv2导入时间def videoaufzeichnung(video_wdth,video_hight,video_fps,seconds):帽 = cv2.VideoCapture(6)cap.set(3,video_wdth) # wdthcap.set(4,video_hight) #hightcap.set(5,video_fps) #hight# 定义编解码器并创建 VideoWriter 对象fps = cap.get(5)Fourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter('output.avi',fourcc,video_fps,(video_wdth,video_hight))#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))开始=时间.时间()时间=0而(zeitdauer<秒):结束=时间.时间()zeitdauer=结束开始ret, frame = cap.read()如果 ret==True:框架 = cv2.flip(frame,180)# 写入翻转的帧写出(帧)cv2.imshow('frame',frame)如果 cv2.waitKey(1) &0xFF == ord('q'):休息别的:休息# 如果作业完成,则释放所有内容cap.release()out.release()cv2.destroyAllWindows()`videoaufzeichnung.videoaufzeichnung(1024,720,10,30)

预先感谢您的帮助.

解决方案

我怀疑您正在使用 OpenCV 的 libv4l 版本进行视频 I/O.OpenCV 的 libv4l API 中存在一个错误,该错误会阻止 VideoCapture::set 方法更改视频分辨率.请参阅链接 123.如果您执行以下操作:

<预><代码>...框架 = cv2.flip(frame,180)打印(frame.shape[:2] # 检查以查看帧大小写出(帧)...

您会注意到帧大小没有被修改以匹配函数参数中提供的分辨率.克服此限制的一种方法是手动调整框架大小以匹配分辨率参数.

<预><代码>...框架 = cv2.flip(frame,180)frame = cv2.resize(frame,(video_wdth,video_hight)) # 手动调整帧大小打印(frame.shape[:2] # 检查以查看帧大小写出(帧)...

Why does the following code not save the video? Also is it mandatory that the frame rate of the webcam matches exactly with the VideoWriter frame size?

    import numpy as np`enter code here
import cv2
import time

def videoaufzeichnung(video_wdth,video_hight,video_fps,seconds):
    cap = cv2.VideoCapture(6)
    cap.set(3,video_wdth) # wdth
    cap.set(4,video_hight) #hight 
    cap.set(5,video_fps) #hight 

    # Define the codec and create VideoWriter object
    fps = cap.get(5)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc,video_fps, (video_wdth,video_hight))
    #out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

    start=time.time()
    zeitdauer=0
    while(zeitdauer<seconds):
        end=time.time()
        zeitdauer=end-start
        ret, frame = cap.read()
        if ret==True:
            frame = cv2.flip(frame,180)
            # write the flipped frame
            out.write(frame)

            cv2.imshow('frame',frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()`

videoaufzeichnung.videoaufzeichnung(1024,720,10,30)

Thanks in advance for your help.

解决方案

I suspect you're using the libv4l version of OpenCV for video I/O. There's a bug in OpenCV's libv4l API that prevents VideoCapture::set method from changing the video resolution. See links 1, 2 and 3. If you do the following:

...
frame = cv2.flip(frame,180)
print(frame.shape[:2] # check to see frame size
out.write(frame)
...

You'll notice that the frame size has not been modified to match the resolution provided in the function arguments. One way to overcome this limitation is to manually resize the frame to match resolution arguments.

...
frame = cv2.flip(frame,180)
frame = cv2.resize(frame,(video_wdth,video_hight)) # manually resize frame
print(frame.shape[:2] # check to see frame size
out.write(frame)
...

这篇关于Opencv2:Python:cv2.VideoWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆