Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Well, I don't believe there's a seek. You need to do it. For example I do it like this :

#include "opencv2/opencv.hpp"
#include "iostream"

using namespace cv;
using namespace std;
int main(int, char**)
{
    VideoCapture cap ( "tree.avi" ); // open the default camera
    if( ! cap.isOpened () )  // check if we succeeded
        return -1;

    /* Mat edges; */
    namedWindow ( "tree" , 1 );
    double frnb ( cap.get ( CV_CAP_PROP_FRAME_COUNT ) );
    std::cout << "frame count = " << frnb << endl;

    for(;;) {
      Mat frame;
      double fIdx;

      std::cout << "frame index ? ";
      std::cin >> fIdx;
      if ( fIdx < 0 || fIdx >= frnb ) break;
      cap.set ( CV_CAP_PROP_POS_FRAMES , fIdx );
      bool success = cap.read(frame); 
      if ( ! success ) {
    cout << "Cannot read  frame " << endl;
    break;
      }
      /* cap >> frame; // get a new frame from camera */
      imshow("tree", frame);
      if ( waitKey (0) == 27 ) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

But, there's some errors I can't explain. The execution is OK 'till you start it with an index at most 100 though the FRAME_COUNT says 451 !? For example, let's tell the first frame to read is #100, next you can read any frame indexed before or after 100. I'd like to fix it. I also try the Haris' code but I met the same bug, i.e. the last frame (#450) isn't read... If you could help me to understand I'll be grateful.

click to hide/show revision 2
There isn't problem

Well, I don't believe there's a seek. You need to do it. For example I do it like this :

#include "opencv2/opencv.hpp"
#include "iostream"

using namespace cv;
using namespace std;
int main(int, char**)
{
    VideoCapture cap ( "tree.avi" ); // open the default camera
    if( ! cap.isOpened () )  // check if we succeeded
        return -1;

    /* Mat edges; */
    namedWindow ( "tree" , 1 );
    double frnb ( cap.get ( CV_CAP_PROP_FRAME_COUNT ) );
    std::cout << "frame count = " << frnb << endl;

    for(;;) {
      Mat frame;
      double fIdx;

      std::cout << "frame index ? ";
      std::cin >> fIdx;
      if ( fIdx < 0 || fIdx >= frnb ) break;
      cap.set ( CV_CAP_PROP_POS_FRAMES , fIdx );
      bool success = cap.read(frame); 
      if ( ! success ) {
    cout << "Cannot read  frame " << endl;
    break;
      }
      /* cap >> frame; // get a new frame from camera */
      imshow("tree", frame);
      if ( waitKey (0) == 27 ) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

But, there's some errors I can't explain. The execution is OK 'till you start it with an index at most 100 though the FRAME_COUNT says 451 !? For example, let's tell the first frame to read is #100, next you can read any frame indexed before or after 100. I'd like to fix it. I also try the Haris' code but I met the same bug, i.e. the last frame (#450) isn't read... If you could help me to understand I'll be grateful.grateful....

Well, there's no pb ! The test video file was corrupted !!! Thus, you can use the code without trouble.