I have a program that uses the OpenCV library (version 2.4.1) to capture video from my laptop's webcam (or any other connected camera) and save it to an .avi file. When I debug in Visual Studio 2010, I get an unhandled exception at the very end of the program, when either the CvCapture or the IplImage are being released. Here is the code:
// 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;
}
I found that I have to have tbb.dll and tbb_debug.dll in the same directory as the source code (.cpp files) for the program to work. These dll's can be downloaded from Intel.
The video capture works, that is, the window appears and displays the video, but the exception occurs no matter how I rearrange the release statements. If I remove the release statements (except for the VideoWriter), I don't get the exception, but then the .avi file produced cannot be opened. The program exits the while loop when the user presses the Esc key.
From openCV docu:
cvQueryFrame
Grabs and returns a frame from camera or file
IplImage* cvQueryFrame( CvCapture* capture );
capture video capturing structure.
The function cvQueryFrame grabs a frame from camera or video file, decompresses and returns it. This function is just a combination of cvGrabFrame and cvRetrieveFrame in one call. The returned image should not be released or modified by user.
So you don't have to allocate or release the "frame"
delete:
IplImage* frame = cvCreateImage( cvSize( width,height ), IPL_DEPTH_8U, 1);
and
cvReleaseImage( &frame );
and replace
frame = cvQueryFrame( capture );
with
IplImage* frame = cvQueryFrame( capture );