OpenCV 将帧写入文件 Python

3 投票
3 回答
8804 浏览
提问于 2025-04-28 10:05

嘿,我开始尝试使用OpenCV,但我无法把我的摄像头输出保存到文件里。这里是我写的代码。这个程序运行得很好,能启动摄像头并创建一个叫“output.avi”的文件。问题是这个output.avi文件非常小(只有414字节),而且每次运行程序时内容都是一样的。

我猜问题出在四字符编码(fourcc)上,但我找不到适合我情况的设置。我是在Mac OS X上运行的。如果你需要更多信息,请告诉我。

import numpy as np
import cv2

path = ('/full/path/Directory/output.avi')

cap = cv2.VideoCapture(0)
cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height

fourcc = cv2.cv.CV_FOURCC(*'XVID')
video_writer = cv2.VideoWriter(path,fourcc, 20.0, (640,480))

while(cap.isOpened()):
    #read the frame
    ret, frame = cap.read()
    if ret==True:
        #show the frame
        cv2.imshow('frame',frame)
        #Write the frame
        video_writer.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()
暂无标签

3 个回答

1

主要问题是你在编程时没有安全性考虑:

path = ('/full/path/Directory/output.avi')

cap = cv2.VideoCapture(0)
if not cap:
    print "!!! Failed VideoCapture: invalid parameter!"
    sys.exit(1)

cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')

video_writer = cv2.VideoWriter(path, fourcc, 20.0, (640,480))
if not video_writer :
    print "!!! Failed VideoWriter: invalid parameters"
    sys.exit(1)

# ...

所以当 VideoCapture()VideoWriter() 失败时,程序会立刻知道不能继续执行了。

另外,注意到旧版的 cv2.cv.CV_FOURCC()cv2.VideoWriter_fourcc() 替代了。我这样做是因为这个页面提供了最新的示例,教你如何用Python做这些事情。你也可以尝试所有的FourCC编码,直到找到一个在你的系统上能用的。

另一个重要的事情是,设置捕获界面的帧大小可能不奏效,因为相机可能不支持那个分辨率。FPS也是如此。为什么这是个问题?因为我们需要在 VideoWriter 的构造函数中定义这些设置,所有发送给这个对象的帧必须具有完全相同的尺寸,否则写入器就无法将帧写入文件。

你应该这样做:

path = ('/full/path/Directory/output.avi')

cap = cv2.VideoCapture(0)
if not cap:
    print "!!! Failed VideoCapture: invalid parameter!"
    sys.exit(1)

# The following might fail if the device doesn't support these values
cap.set(1, 20.0) #Match fps
cap.set(3,640)   #Match width
cap.set(4,480)   #Match height

# So it's always safer to retrieve it afterwards
fps = cap.get(CV_CAP_PROP_FPS)
w = cap.get(CV_CAP_PROP_FRAME_WIDTH);
h = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')

video_writer = cv2.VideoWriter(path, fourcc, fps, (w, h))
if not video_writer :
    print "!!! Failed VideoWriter: invalid parameters"
    sys.exit(1)

while (cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
         break

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

cap.release()
video_writer.release()
1

把你的代码整理成类,并分成清晰的函数。找一些函数来保存你在OpenCV中的结果,试试其他格式,并在多个操作系统上运行你的代码。

你也可以尝试使用C++或者Java/C#来配合OpenCV。

我想在计算机视觉的书里有关于你问题的章节,你可以在这里找到相关书籍

这就是我能帮你的全部了。

4

只需要把

fourcc = cv2.cv.CV_FOURCC(*'XVID')

改成

fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')

答案可以在这里找到:

opencv VideoWriter在OSX下没有输出

撰写回答