0

I'm trying to make a video start at the beginning just after the video has reached frame number 50(e.i repeat the first 50 frames indefinitely). I read about a previous question were one of the responders says that the only practical way of doing it is to save the frames and then re display them. But, can I just set a counter to the beginning of the VideoCapture object and just keep grabbing frames with the >> operator? This is what I tried so far:

VideoCapture cap;//I select the video by passing it as an argument though the terminal.
while(1){

    if (FrameIndex == 50){  
    //cap.set(CV_CAP_PROP_POS_FRAMES, 0);
    cap.set(CV_CAP_PROP_POS_AVI_RATIO , 0);
    cap >> frame;
    }
    else{
    cap >> frame;
    }

  //#################################### 
  //# Rest of the code inside the loop # 
  //#################################### 


    FrameIndex++;
    FrameIndex = FrameIndex%50;

}
Community
  • 1
  • 1
jjf
  • 115
  • 3
  • 11
  • Setting CV_CAP_PROP_POS_FRAMES to 0, isn't working right? – chroman Dec 01 '13 at 02:22
  • Right, I get this error: "OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/jose/Desktop/UTA Research Stuff(Linux)/Augmented-Reality-Surgical-System-Linux/OpenCV-2.4.1/modules/core/s‌​rc/matrix.cpp, line 322 terminate called after throwing an instance of 'cv::Exception' what(): /home/jose/Desktop/UTA Research Stuff(Linux)/Augmented-Reality-Surgical-System-Linux/OpenCV-2.4.1/modules/core/s‌​rc/matrix.cpp:322: ...." – jjf Dec 01 '13 at 18:14

1 Answers1

0

CV_CAP_PROP_POS_AVI_RATIO is not a frame 'counter'. The doc of the get() method says it's "Relative position of the video file: 0 - start of the film, 1 - end of the film.". And in the doc of the set() method there's no such parameter at all.

So, you better just close and open the capture again to go back to the beginning.

This may seem to be a little too much for an operation which feels so simple and natural. But in reality, video decoding is not that straightforward. The frames are dependent on other frames - so it's not enough to know you want frame number N, in general case you need some of the previous ones as well as the following ones (e.g. B-frames). It's not that this cannot be done - but it's rather complicated or at least uneasy.

Sergei Nosov
  • 1,637
  • 11
  • 9