OpenCV VideoCapture automatically updates the frame

I am implementing an algorithm that requires video frames from time t, and the other from time t + 1. Seeing a few examples, this seemed very simple. I thought this would work just fine:

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

But this is not so, the images were the same because the displayed image (img1 == img2) was completely white, indicating a value of 255 for each pixel.

I thought that perhaps I did not give enough time for the camera to capture the second frame, and I used the same one as 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. 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 again displayed two images after capturing img1, both images changed! I tried to use different VideoCapture objects for different images, but this had no effect ...

Can anyone advise me what I'm doing wrong?

Thanks,

Renan

+4
source share
4 answers

I fixed the problem by copying img1 and img2 to the auxiliary matrices so that they remain unchanged. Does anyone know of a better solution?

+3
source

When calling grabber (using the >> operator in your case), OpenCV sends a link only to the current frame. Thus, img1 will point to the frame buffer, and when you call cap >> img2 both images will point to the last frame. The only way to save individual images is to store them in separate matrices (for example, img1.copyTo(myFirstImg) , myFirstImg = img1.clone() , etc.).

+4
source

You can work with cap.grab () and cap.retrieve (img1,) twice. Read the documentation in .grab and retrieve VideoCapture carefully. here

+2
source

Captures the frame from system camera using OpenCV

Grab () function used to capture frames from a video camera in OpenCV

+1
source

All Articles