Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you could try to set a timer:

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat frame; // keep it out of the loop, so we can save latest img.
    int64 t0 = getTickCount(); // start time
    for(;;)
    {
        cap >> frame; // get a new frame from camera
        int64 t1 = getTickCount(); // time now
        double timeRunning = double(t1-t0)/getTickFrequency();
        if (timeRunning >= 2.0) // 2 seconds
              break;
    }
    // write last frame to  disk:
    imwrite("/home/sachin/Desktop/a.jpg", frame);
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

sidenote: waitKey() won't work without a gui window