简体   繁体   中英

How to get frame feed of a video in OpenCV?

I need to get the frame feed of the video from OpenCV. My code runs well but I need to get the frames it is processing at each ms.

I am using cmake on Linux.

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 allows you to dynamically create an array. 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).

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. For example, if you're grabbing 640x480 RGB frames every 30ms, you will hit 2GB of RAM in around 70s.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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