简体   繁体   English

如何在OpenCV中获取视频的帧馈送?

[英]How to get frame feed of a video in OpenCV?

I need to get the frame feed of the video from OpenCV. 我需要从OpenCV获取视频的帧馈送。 My code runs well but I need to get the frames it is processing at each ms. 我的代码运行良好,但我需要获得它在每ms处理的帧。

I am using cmake on Linux. 我在Linux上使用cmake。

My code: 我的代码:

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

I'm assuming you want to store the frames. 我假设你想存储帧。 I would recommend std::vector (as GPPK recommends ). 我建议使用std :: vector (如GPPK 推荐的那样 )。 std::vector allows you to dynamically create an array. std::vector允许您动态创建数组。 The push_back(Mat()) function adds an empty Mat object to the end of the vector and the back() function returns the last element in the array (which allows cap to write to it). push_back(Mat())函数将一个空的Mat对象添加到向量的末尾,而back()函数返回数组中的最后一个元素(允许cap写入它)。

The code would look like this: 代码如下所示:

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

Note that you can fill your RAM very quickly like this. 请注意,您可以像这样快速填充RAM。 For example, if you're grabbing 640x480 RGB frames every 30ms, you will hit 2GB of RAM in around 70s. 例如,如果你每30ms抓取640x480 RGB帧,你将在70s左右达到2GB的RAM。

std::vector is a very useful container to know, and I would recommend checking out a tutorial on it if it is unfamiliar to you. std::vector是一个非常有用的知识容器,如果您不熟悉,我建议您查看一个教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM