【发布时间】:2012-03-12 12:07:08
【问题描述】:
我是新手,开始在 ubuntu 10.4 上使用 OpenCV。我正在尝试从相机捕获视频并将该视频写入 avi 文件,代码如下:
#include "opencv2/opencv.hpp"
#include <iostream>
#include <vector>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("cam1",CV_WINDOW_AUTOSIZE);
Mat frame;
VideoWriter outputVideo("lalala.avi", CV_FOURCC('D','I','V','X') , 30, Size(720, 480) , true); // or frame.size()
outputVideo.open("lalala.avi", CV_FOURCC('D','I','V','X') , 30, Size(720, 480), true) ; // or frame.size()
if( !outputVideo.isOpened() ) {
printf("VideoWriter failed to open!\n");
}
for(;;)
{
cap >> frame; // get a new frame from camera
imshow("cam1", frame);
outputVideo.write(frame); // or outputVideo << frame;
if(waitKey(30) >= 0) break;
}
return 0;
}
我的问题是我无法打开 VideoWriter 类,但我可以很好地从相机中获取帧。我尝试了不同的编解码器值和大小,但没有任何变化。
知道程序为什么不初始化 VideoWriter 类吗?
【问题讨论】:
-
你做了两次同样的事情,
VideoWriter outputVideo(...)和outputVideo.open(...)。 =[ 并尝试使用编解码器MJPG,我不知道OpenCV是否支持DIVX。