我是opencv的新手,写入视频文件时遇到问题。基本上我正在从高清网络摄像头读取并写入avi。可运行的代码:
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv){
CvCapture* capture=NULL;
capture=cvCreateCameraCapture(0);
if(!capture){
return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);
double fps=cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
CvSize size=cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter* writer=cvCreateVideoWriter(argv[1],
CV_FOURCC('M','J','P','G'),
fps,
size);
cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);
while((bgr_frame=cvQueryFrame(capture))){
cvWriteFrame(writer, bgr_frame);
cvShowImage("Video", bgr_frame);
char c=cvWaitKey(60);
if(c==27){
break;
}
}
cvReleaseVideoWriter(&writer);
cvReleaseImage(&bgr_frame);
cvReleaseCapture(&capture);
return 0;
}
运行时,我收到错误
Output #0, avi, to 'test.avi':
Stream #0.0: Video: mjpeg, yuvj420p, 1280x720, q=2-31, 117964 kb/s, 90k tbn
[mjpeg @ 0x7fd55b805600] bitrate tolerance too small for bitrate
WARNING: Could not create empty movie file container.
OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.3.1/modules/imgproc/src/color.cpp, line 3175
terminate called throwing an exceptionAbort trap: 6
相机是Macbook上的高清网络摄像头。这个相机会导致问题吗?如果是这样,我可以将比特率容差设置得更低吗?我是opencv的新手。谢谢!
顺便说一下,当argv [1] .avi不存在时,CvVideoWriter可以创建一个新文件吗?
答案 0 :(得分:2)
是的,cvCreateVideoWriter会在尚不存在时创建新文件。
因为每个网络摄像头驱动程序都没有实现FPS检测代码,所以会出现问题。在Mac上尝试使用一个断言添加代码:
double fps=(double)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
assert(fps>0.0);
运行它:
./w x.avi
Assertion failed: (fps>0.0), function main, file w.cpp, line 11.
Abort trap: 6
带有FPS的AVI文件无法运行,这就是它未创建的原因。