如何将输出video保存到OpenCV中的文件中

我想将输出video保存到文件而不是显示它并尝试使用cvcaptureimage但仍无法获得结果

#include  int main(int argc, char *argv[]) { // Create a "Capture" object from 1st command line argument CvCapture *video = cvCaptureFromFile(argv[1]); // this is the structure that will store the frames IplImage *frame = NULL; // create a window to display the video cvNamedWindow("Video", CV_WINDOW_AUTOSIZE); // get the next frame while (frame = cvQueryFrame(video) ) { // display it in the window we created cvShowImage("Video", frame); // wait 1000/fps milliseconds cvWaitKey((int)(1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS))); //reading into .avi file CvCapture*capture=0 capture=cvcreateFileCapture(arg[1]); if(!capture) { // intiate the video read IpLImage *bgr_frame=cvQueryFrame(capture); double fps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS); } } } 

我可以知道我在代码中重复的错误吗?

我知道这已经很老了,但我不同意有关OSX的声明。

您可以在OSX上轻松构建和编译FFMPEG。 无需使用QTKIT / Quicktime。 我实际上是在OSX上写video而没有任何问题。

另一个建议是使用新的OpenCV 2.0+结构,而不是使用那些已弃用的结构。 (使用Mat而不是IplImage / cvMat ,使用VideoWriter和VideoCapture。)

 Mat image; VideoCapture capture; VideoWriter out; capture = VideoCapture(-1); image << capture; out.write(image); 

如果您需要参考代码,请检查此项 ,以便开始使用。

无论如何 ,下面的代码显示来自网络摄像头的video。 如果仔细观察,您会看到从彩色框架到灰度版本的转换,并且窗口上会显示灰度。

灰度帧也记录在磁盘上的文件中,名为out.avi 。 你必须知道这个代码不适用于使用OpenCV 2.1的Mac OS X,因为需要使用ffmpeg编译OpenCV以允许将video添加到文件中。

在Windows上,cvCreateVideoWriter()将显示一个对话框,供您选择保存video时要使用的相应编解码器。 可以通过设置一个参数来更改此函数调用,该参数将对您的首选项的编解码器进行硬编码。

我匆匆写了它,但我知道它有效。

 #include  #include  #include  #include  #include  #include  #include  int main() { int camera_index = 0; IplImage *color_frame = NULL; int exit_key_press = 0; CvCapture *capture = NULL; capture = cvCaptureFromCAM(camera_index); if (!capture) { printf("!!! ERROR: cvCaptureFromCAM\n"); return -1; } double cam_w = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); double cam_h = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); printf("* Capture properties: %fx %f - %f fps\n", cam_w, cam_h, fps); cvNamedWindow("Grayscale video", CV_WINDOW_AUTOSIZE); CvVideoWriter* writer = NULL; writer = cvCreateVideoWriter("out.avi", -1, fps, cvSize((int)cam_w,(int)cam_h), 1); if (writer == NULL) { printf("!!! ERROR: cvCreateVideoWriter\n"); return -1; } while (exit_key_press != 'q') { color_frame = cvQueryFrame(capture); if (color_frame == NULL) { printf("!!! ERROR: cvQueryFrame\n"); break; } IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1); if (gray_frame == NULL) { printf("!!! ERROR: cvCreateImage\n"); continue; } cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY); cvShowImage("Grayscale video", gray_frame); cvWriteFrame(writer, gray_frame); cvReleaseImage(&gray_frame); exit_key_press = cvWaitKey(1); } cvReleaseVideoWriter(&writer); cvDestroyWindow("Grayscale video"); cvReleaseCapture(&capture); return 0; } 

您似乎为循环内的每个帧创建一次video文件。

在开头创建一次avi文件,然后使用cvWriteFrame()添加每个新帧

因为您还没有发布图像。 它会导致内存中断。 你需要在循环中释放图像。