1

我需要从 OpenCV 获取视频的帧馈送。我的代码运行良好,但我需要在每毫秒获取它正在处理的帧。

我在 Linux 上使用 cmake。

我的代码:

#include "cv.h"
#include "highgui.h"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera

Mat frame;
    namedWindow("feed",1);
    for(;;)
{
    Mat frame;
    cap >> frame;   // get a new frame from camera
    imshow("feed", frame);
    if(waitKey(1) >= 0) break;
}
    return 0;
}
4

1 回答 1

4

我假设你想存储框架。我会推荐std::vector(正如GPPK 推荐的那样)。std::vector允许您动态创建数组。该push_back(Mat())函数将一个空Mat对象添加到向量的末尾,并且该back()函数返回数组中的最后一个元素(允许cap对其进行写入)。

代码如下所示:

#include "cv.h"
#include "highgui.h"

using namespace cv;

#include <vector>
using namespace std; //Usually not recommended, but you're doing this with cv anyway

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera

    vector<Mat> frame;
    namedWindow("feed",1);
    for(;;)
    {
        frame.push_back(Mat());
        cap >> frame.back();   // get a new frame from camera
        imshow("feed", frame);
        // Usually recommended to wait for 30ms
        if(waitKey(30) >= 0) break;
    }
    return 0;
}

请注意,您可以像这样快速填充 RAM。例如,如果您每 30 毫秒抓取 640x480 RGB 帧,您将在大约 70 秒内达到 2GB 的 RAM。

std::vector是一个非常有用的容器,如果您不熟悉,我建议您查看有关它的教程。

于 2013-08-14T15:12:59.117 回答