Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV, captured video runs faster than original camera video!

Tags:

c++

opencv

I am using openCV to capture video from camera and store to and avi file, the problem is that when i finish capturing and run the avi file, the video stream looks awkwardly fast...

here is the code

void main( )
{
CvCapture *capture = cvCaptureFromCAM( 0 );

int width = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH );
int height = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
CvVideoWriter *writer = cvCreateVideoWriter( "myCamCapture.avi",
-1,30, cvSize(  width, height ) );
cvNamedWindow("d", CV_WINDOW_AUTOSIZE);
IplImage *frame = 0;


while( 1 )
{
    frame = cvQueryFrame( capture );

    cvShowImage("d",frame);
    cvWriteFrame( writer, frame );
    char c = cvWaitKey( 33 );
    if( c == 27 ) break;
}

cvReleaseCapture( &capture );
cvReleaseVideoWriter( &writer );
cvDestroyWindow( "d" );


    }

please help

like image 840
Alan_AI Avatar asked Nov 29 '25 08:11

Alan_AI


2 Answers

You're telling the writer that it should play at 30 frames per second. So if you're actually capturing, say, 15 frames per second, those frames are going to be played back faster than than real time.

Showing the captured image, waiting for a keypress, and writing it to the file all take time. You need to account for that. You might try capturing the video up-front, measuring the actual FPS while it happens, and then writing your AVI using that value.

like image 197
kwatford Avatar answered Nov 30 '25 22:11

kwatford


You can also use cvGetCaptureProperty(CV_CAP_PROP_FPS ) to ask the camera what frame rate it is generating frames at then use 1000/fps instead of 33 in the delay loop.

like image 43
Martin Beckett Avatar answered Nov 30 '25 21:11

Martin Beckett