Ask Your Question

Revision history [back]

try this after reading @berak's explaining answer

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int, char**)
{
    Mat frame;
    VideoCapture cap;
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    cap.open(deviceID + apiID);

    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }

    //--- GRAB AND WRITE LOOP
    cout << "Start grabbing" << endl
        << "Press any key to terminate" << endl;

    int cnt = 0;
    Mat frm_test[3];

    for (;;)
    {
        // wait for a new frame from camera and store it into 'frm_test[circular_value]'
        cap.read(frm_test[cnt % 3]);
        // check if we succeeded
        if (frm_test[cnt % 3].empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }

        // show live and wait for a key with timeout long enough to show images
        imshow("Live", frm_test[cnt % 3]);
        cnt++;
        if (waitKey(5) >= 0) {
            cout << "Exit requested" << endl;
            destroyAllWindows();
            break;
        }

    }

    imshow("frm 1", frm_test[(cnt-3) % 3]);
    imshow("frm 2", frm_test[(cnt-2) % 3]);
    imshow("frm 3", frm_test[(cnt-1) % 3]);

    waitKey(0);
    return 0;
}