开发者

OpenCV, captured video runs faster than original camera video!

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜