简体   繁体   中英

OpenCV, capturing from camera and saving to file

i want to build a simple application in openCV that captures video from camera and store it to a file. The problem is:

1- how to access the frame rate of my camera. cvGetCaptureProperty() always returns an FPS value of zero.

2- how to control the time in which the capture will take please, ie how to limit the capture time to 10 seconds for example...

here is my 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( 30 );
        if( c == 27 ) break;
    }
}

thank you in advance

1 (Edit): According to this site near the bottom of the page , it is necessary to call

cvQueryFrame(capture);

first in order to get correct properties. Retry calling

int fps       = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);

afterwards

2: You could use cvWaitKey(milliseconds) or simply a sleep. How are you capturing images, what does your loop look like?

1 - Not all cameras will supply the frame rate it depends on what their driver supports.

2 - To capture 10 seconds of video simply work out how many frames this is and instead of while(1) - which runs for ever, do something like.

// assumign you want 10seconds of 3fps = 300 frames total
for (int iframe=0;iframe<300;iframe++) {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM