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
I fixed the problem by copying img1 and img2 to auxiliary matrices so that they would remain the same for sure. Anyone knows of a better solution?