2

I have a problem to open many video files (e.g., 200) in a loop by using the OpenCV class VideoCapture. Below you can find my code.

More specifically, my program succeeds to open a certain number of videos (usually 171-173) but then it fails to open the others. I even tried to open always the same file (like in the example below) but the behaviour is the same.

In my opinion it should not be a memory leak problem (actually there is memory leak, but consumes only about 50MB in total). I think it is related to the fact that, when each video is opened, several threads are also opened and never closed, so they accumulate. But I don't know if this is the real reason or, if it is, how to solve it.

I'm using Visual Studio to compile, and Windows 7 OS.

Please let me know if you have any 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, as the code is very simple.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1343279
  • 21
  • 1
  • 4
  • first question...how big are the video files? – TomP89 Apr 19 '12 at 08:02
  • Quite small, between 1 and 20 MB – user1343279 Apr 19 '12 at 08:15
  • well I imagine you are potentially running out of memory. Are your running 32bit or 64bit windows ? and how much RAM do you have? Worst case scenario, 200 x 20mb video = 4GB – TomP89 Apr 19 '12 at 08:42
  • - I have 16GB of RAM. By checking on the Task Manager of Windows (64bit), the process of my program takes only up to 50 MB. That's why I don't think the problem is related to memory. – user1343279 Apr 19 '12 at 08:51
  • Sorry, I read your post as though your memory leak only takes up 50mb. – TomP89 Apr 19 '12 at 09:13
  • I cannot understand why for each video there are several threads being created. Also, they are not closed afterwards. Is this happening for other people too? – user1343279 Apr 19 '12 at 10:19
  • Try to find out why it fails to open by calling GetLastError() and printing the result when it could not open the video. – rve Apr 19 '12 at 10:32
  • @rve - I've tried using GetLastError() but the message box shows this: myprogram faild with error 0: The operation completed successfully. – user1343279 Apr 19 '12 at 10:49
  • I'm using OpenCV 2.3. It would be helpful if someone could try these few lines of code and check if the problem appears or not. Thanks! – user1343279 Apr 19 '12 at 13:16

1 Answers1

1

I used OpenCV 2.3.0 on Mac OS X and had no problems running your code.

You might want to upgrade your version to 2.3.1 and try again. If the problem persists, it might an issue specific to the Windows implementation or even maybe just specific to Windows 7.

Another wild guess is to implement the program above using the C interface of OpenCV instead of the C++ interface you are using right now. I've had problems in the past (not related to video) that were fixed using this trick. I don't recommend mixing the interfaces, so if you are going to do something with the C interface, don't use the C++ interface of OpenCV in your program:

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 it's cvCaptureFromAVI() or cvCreateFileCapture(). Please verify!

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks @karlphillip . I will try what you said -- Until now I've been using the precompiled OpenCV 2.3 library for MS Visual Studio. I will also try the same code on Linux and will post what will happen. – user1343279 Apr 22 '12 at 10:07