我有一个程序使用 OpenCV 库(版本 2.4.1)从笔记本电脑的网络摄像头(或任何其他连接的摄像头)捕获视频并将其保存到 .avi 文件。当我在 Visual Studio 2010 中调试时,在程序的最后,当 CvCapture 或 IplImage 被释放时,我得到一个未处理的异常。这是代码:
// WriteRealTimeCapturedVideo.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
int main()
{
CvCapture* capture = cvCaptureFromCAM( 1 ); //CV_CAP_ANY
if ( !capture )
{
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
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));
#ifndef NOWRITE
CvVideoWriter* writer = cvCreateVideoWriter("Capture.avi", CV_FOURCC('M','J','P','G'), fps, size); //CV_FOURCC('M','J','P','G')
#endif
int width = (int)(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH));
int height = (int)(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 1);
while ( 1 )
{
// Get one frame
frame = cvQueryFrame( capture );
if ( !frame )
{
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
#ifndef NOWRITE
cvWriteToAVI( writer, frame );
#endif
char c = cvWaitKey(33);
if( c == 27 ) break;
}
#ifndef NOWRITE
cvReleaseVideoWriter( &writer );
#endif
cvDestroyWindow( "mywindow" );
cvReleaseImage( &frame );
cvReleaseCapture( &capture );
return 0;
}
我发现我必须将 tbb.dll 和 tbb_debug.dll 放在与源代码(.cpp 文件)相同的目录中,程序才能运行。这些 dll 可以从 Intel 下载。
视频捕获工作,即出现窗口并显示视频,但无论我如何重新排列发布声明都会出现异常。如果我删除了发布声明(除了 VideoWriter),我不会得到异常,但是生成的 .avi 文件无法打开。当用户按下 Esc 键时,程序退出 while 循环。