Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

When should I use VideoCapture.release()?

This is a part of my code in C++, I'm using it to count live traffic and the idea is to keep running the program at least 20 hours, 7 days a week. I mean this is an example of how I'm catching the frames but I need to do it all the time (like a While(true) ) and I'm having some problems.

#include "opencv2/opencv.hpp"
#include <time.h>

using namespace cv;
using namespace std;

int main(int argc, char** argv)

    // Start default camera
    VideoCapture video("ip of my ipCam");

    // With webcam get(CV_CAP_PROP_FPS) does not work.
    // Let's see for ourselves.

    double fps = video.get(CAP_PROP_FPS);
    // If you do not care about backward compatibility
    // You can use the following instead for OpenCV 3
    // double fps = video.get(CAP_PROP_FPS);
    cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;

    // Number of frames to capture
    int num_frames = 120;

    // Start and end times
    time_t start, end;

    // Variable for storing video frames
    Mat frame;

    cout << "Capturing " << num_frames << " frames" << endl ;

    // Start time

    // Grab a few frames
    for(int i = 0; i < num_frames; i++)
        video >> frame;

    // End Time

    // Time elapsed
    double seconds = difftime (end, start);
    cout << "Time taken : " << seconds << " seconds" << endl;

    // Calculate frames per second
    fps  = num_frames / seconds;
    cout << "Estimated frames per second : " << fps << endl;

    // Release video
    video.release(); //----------------THIS------------------
    return 0;

Off course I take the frames and procces them to count the traffic... but I mean I'm running my proggram and after 5 minutes it crashes, so I was wondering if shoul I VideoCapture.release() every X minuto to prevent problems like

OpenCV Error: Insufficient memory (Failed to allocate 2764800 bytes) in OutOfMemoryError, file /home/pi/opencv/opencv-3.3.0/modules/core/src/alloc.cpp, line 55
OpenCV Error: Assertion failed (u != 0) in create, file /home/pi/opencv/opencv-3.3.0/modules/core/src/matrix.cpp, line 436
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/pi/opencv/opencv-3.3.0/modules/core/src/matrix.cpp:436: error: (-215) u != 0 in function create

In the image processing I use an array to store the frame so I can process them later, but I'm emptying the array at the same time, so I don't know why I get this errors.