OpenCV ライブラリ (バージョン 2.4.1) を使用してラップトップの Web カメラ (またはその他の接続されたカメラ) からビデオをキャプチャし、.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;
}
プログラムを動作させるには、ソース コード (.cpp ファイル) と同じディレクトリに tbb.dll と tbb_debug.dll を配置する必要があることがわかりました。これらの dll は Intel からダウンロードできます。
ビデオ キャプチャは機能します。つまり、ウィンドウが表示されてビデオが表示されますが、リリース ステートメントをどのように並べ替えても例外が発生します。リリース ステートメント (VideoWriter を除く) を削除すると、例外は発生しませんが、生成された .avi ファイルを開くことができません。ユーザーが Esc キーを押すと、プログラムは while ループを終了します。