OpenCV VideoCapture cannot open many video files

I have a problem opening many video files (200 for example) in a loop using the VideoCapture OpenCV class. You can find my code below.

In particular, my program manages to open a certain number of videos (usually 171-173), but then fails to open others. I even tried to open always the same file (as in the example below), but the behavior is the same.

In my opinion, this is not a memory leak problem (there is actually a memory leak, but it only consumes about 50 MB). I think this has to do with the fact that when every video is open, multiple streams are also open and never close, so they accumulate. But I don't know if this is the real cause or, if so, how to solve it.

I am using Visual Studio for compilation and Windows 7 OS.

Please let me know if you have a clue and / or solution.

string video_filename = "MyVideo.mp4";
for(int j=0; j<200; j++)
{
    VideoCapture video(video_filename);
    if(!video.isOpened())
    {
        cout << "Video #" << j << " could not be opened" << endl;
    }

    video.release(); // I've tried also to comment this out
}

      

I think you can easily try to reproduce this problem since the code is very simple.

+2


source to share


1 answer


I have used OpenCV 2.3.0 on Mac OS X and have had no problem getting your code running.

You might want to update to version 2.3.1 and try again. If the issue persists, it could be a Windows implementation specific issue, or even possibly Windows 7 only.

Another wild guess would be to implement the above program using the OpenCV C interface instead of the C ++ interface you are using. I have had issues in the past (not video related) that have been fixed using this trick. I do not recommend mixing interfaces, so if you are going to do something with the C interface, do not use the C ++ interface in OpenCV :



for (int j=0; j<200; j++)
{
    CvCapture* capture = cvCaptureFromAVI("MyVideo.mp4");
    if (!capture)
    {
        cout << "Video #" << j << " could not be opened" << endl;

        // Prevent calling cvReleaseCapture() on a capture that didn't succeeded
        continue; 
    }

    cvReleaseCapture(&capture);
}

      

I don't remember if he is cvCaptureFromAVI()

or cvCreateFileCapture()

. Please check!

+1


source







All Articles