Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

try to read different frame by videocapture but failed

Hi all, this is my way i try to read and save video's different frame. below is my code, but i found that i got the same images.(i'm sure i keep moving in front of the camera lol)

pls help me to find what i need to do. thanks in advance!!

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <time.h>

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];
clock_t start, finish;
start = clock();

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

    finish = clock();
    if ((cnt < 3) && ((finish - start)/CLOCKS_PER_SEC > 1.0)) {
        frm_test[cnt] = frame;
        start = clock();
        cnt++;
    }

        // covert color to gray
        //cvtColor(frame, frame, cv::COLOR_RGB2GRAY);

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

    }
    // the camera will be deinitialized automatically in VideoCapture destructor

    imshow("frm 1", frm_test[0]);
    imshow("frm 2", frm_test[1]);
    imshow("frm 3", frm_test[2]);

    waitKey(0);
    return 0;
}