3

I am capturing an avi file and processing it. My code has worked for sometime without problem but now it does not seem to stop after the last frame of the video is captured. Instead it keeps looping back to the beginning of the video. I do not understand why this is happening and I can not think of anything changing with regards to Eclipse or OpenCV. I have tried the same code on my Ubuntu pc with the same video and it works without problems. I have even tried as much as reinstalling the OS and apps without success.

Sample code:

#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;



int main(int argc, char** argv)
{
Mat frame;
VideoCapture capture;
const string inputVideo = argv[1];


char buff[PATH_MAX];
getcwd( buff, PATH_MAX );
std::string fileName( buff );
fileName.append("/");
fileName.append(inputVideo);


capture.open(inputVideo);

while(true)
{
    capture >> frame;

    if(!frame.empty())
    {
        imshow("frame", frame);
    }
    else
    {
        printf(" --(!) No captured frame -- Break!");
        break;
    }

    int key = waitKey(10);
    if((char)key == 'c')
    {
        break;
    }
}

return 0;
}

I am running this on a Mac OS X (10.8.2), Eclipse Juno, and OpenCV 2.4.3.

Any advice or comments are appreciated. Thanks in advance

jmo
  • 153
  • 2
  • 12
  • did you try the C style video reading with CvCapture ? – rotating_image Dec 10 '12 at 13:29
  • @rotating_image I have just tried that without success. I used the code from [here](http://stackoverflow.com/questions/4872383/how-to-write-a-video-file-with-opencv) and took out the code for writing the output to keep it simple. Still loops. – jmo Dec 10 '12 at 17:52
  • 1
    I am currently having the same problem. There is a workaround though. You can just get the number of frames in the video and use a for loop instead of a while loop. This will return the number of frames: `capture.get(CV_CAP_PROP_FRAME_COUNT)` – zackg Dec 12 '12 at 08:22
  • @zackg Thanks for the suggestion. I had thought about doing something like that to just get by. If at some time I come across the reason for this problem I will be sure to let you know as well. Thanks again. – jmo Dec 12 '12 at 13:18
  • 2
    This did not work for me, but I found another workaround: capture.get(CV_CAP_PROP_POS_FRAMES) before and after frame grabbing, if the value "after" is less than the value "before", then I've reached the end of the video. – G B Oct 11 '13 at 13:49
  • @GB Very logical approach. Thanks, it is a viable solution. – jmo Oct 12 '13 at 05:00

2 Answers2

2

The solution that I used was posted as a comment by @G B. I am creating a solution so that it may be marked as one.

I used capture.get(CV_CAP_PROP_POS_FRAMES) before and after frame grabbing, if the value "after" is less than the value "before", then I've reached the end of the video.

jmo
  • 153
  • 2
  • 12
  • I'm getting the value "after" as equal to the value "before" when I reach the end of the video fwiw (opencv 3.2.0) – lobotmcj Jan 25 '18 at 04:21
1

Get the frame count like below,

int frameCnt = capture.get(CV_CAP_PROP_FRAME_COUNT);

And check to exit the loop when the frame count exceeds..

zessx
  • 68,042
  • 28
  • 135
  • 158
Jegan
  • 139
  • 1
  • 8
  • I am sure that you mean to exit the loop when the current frame accumulator exceeds the frame count... – jmo Nov 05 '14 at 15:26