OpenCV上的VideoCapture自动更新帧

时间:2011-05-03 08:47:08

标签: opencv video-capture video-processing

我正在实施一种算法,该算法需要来自时刻t的视频帧和来自时刻t + 1的另一个视频帧。看到几个例子,它似乎非常简单。我认为这样可以完美地运作:

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

但它没有,图像是相同的,因为显示的图像(img1 == img2)是完全白色的,表示每个像素的值为255.

我想也许我没有给相机足够的时间来捕获第二帧,而我正在使用仍然在缓冲区中的那个帧。我做的很简单:

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);
}

它仍然无效。可以肯定的是,我添加了以下几行代码:

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);
}

当我再次捕获img1后显示两个图像时,两个图像都发生了变化!我尝试过为不同的图像使用不同的VideoCapture对象,但这没有任何影响...

有人可以告诉我我做错了吗?

谢谢,

Renan的

4 个答案:

答案 0 :(得分:4)

调用抓取器时(在您的情况下使用>>运算符),OpenCV仅发送对当前帧的引用。因此,img1将指向帧缓冲区,当您调用cap >> img2时,两个图像都将指向最新帧。保留单独图像的唯一方法是将它们存储在单独的矩阵中(即img1.copyTo(myFirstImg)myFirstImg = img1.clone()等)。

答案 1 :(得分:3)

我通过将img1和img2复制到辅助矩阵来解决问题,这样它们肯定会保持不变。谁知道更好的解决方案?

答案 2 :(得分:2)

您可以使用cap.grab()和cap.retrieve(img1,)两次。 仔细查看.grab文档并检索VideoCapture。 here

答案 3 :(得分:1)

Captures the frame from system camera using OpenCV

grab()函数用于从OpenCV中的Camera Video中抓取帧

相关问题