Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VideoCapture on OpenCV updating automatically the frame

I'm implementing an algorithm that requires a video frame from instant t and another from instant t+1. Seeing a few examples it seemed really straightforward. I thought that this would work perfectly:

VideoCapture cap(0);
Mat img1, img2;
while(1) {
    cap >> img1; // instant t
    cap >> img2; // instant t+1
    imshow(img1 == img2);
}

But it didn't, the images were the same, because the image displayed (img1 == img2) was completely white, indicating value 255 for every pixel.

I thought that maybe I wasn't giving enough time for the camera to capture a second frame and I was using the same one that was still in the buffer. What I did was simple:

VideoCapture cap(0);
Mat img1, img2;
while(1) {
    cap >> img1; // instant t

    // I added 2.5 seconds between the acquisition of each frame
    waitKey(2500); 

    cap >> img2; // instant t+1
    waitKey(2500);
    imshow(img1 == img2);
}

It still didn't work. Just to be sure, I added the following lines of code:

VideoCapture cap(0);
Mat img1, img2;
while(1) {
    cap >> img1; // instant t
    imshow("img1", img1);
    waitKey(2500); // I added 2.5 seconds between the acquisition of each frame

    cap >> img2; // instant t+1

    // Here I display both images after img2 is captured
    imshow("img2", img2);
    imshow("img1", img1);
    waitKey(2500);
    imshow(img1 == img2);
}

When I displayed the two images after capturing img1 again, both images had changed! I've tried using different VideoCapture objects for the different images, but that didn't have any effect...

Can anyone advise me on what I'm doing wrong?

Thanks,

Renan

like image 709
Renan Avatar asked Dec 27 '22 21:12

Renan


1 Answers

When calling the grabber (By using the >> operator in your case), OpenCV does only send a reference to the current frame. So, img1 will point to the frame buffer, and when you call cap >> img2, both images will point to the latest frame. The only way to keep separate images is to store them in separate matrices (i.e img1.copyTo(myFirstImg), myFirstImg = img1.clone(), etc).

like image 197
vas Avatar answered Jan 16 '23 00:01

vas