Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am not familiar with Python you can refer this piece of c++ code might be helpful.

    IplImage* frame, *lastframe;
    CvCapture* capture = cvCreateFileCapture("Video.avi");

    //Query for getting image size and depth for creating lastframe Image pointer.
    frame = cvQueryFrame( capture ); 
     if ( !frame ) return;
     lastframe=cvCreateImage(cvGetSize(frame),8,3);

     while (1)
     {
       frame = cvQueryFrame( capture );
        if ( !frame ) break;
        cvCopy(frame,lastframe, NULL);
     }

  if(lastframe)
    cvSaveImage("lastframe.jpeg",lastframe);
    cvReleaseImage(&lastframe);
    cvReleaseCapture( &capture );

Hope these helpful.....

I am not familiar with Python you Hi, You can refer this piece of c++ code might OpenCV documentation about Reading and Writing Images and Video

  • Get total frame count using VideoCapture::get by passing the id CV_CAP_PROP_FRAME_COUNT.
  • Then using VideoCapture::set set the index to next frame to be helpful.captured, note that the index is 0 based.

See below code

    IplImage* frame, *lastframe;
    CvCapture* capture VideoCapture cap("video1.mp4"); 
    if( !cap.isOpened()){
         cout << "Cannot open the video file" << endl;
         return -1;
    }

    double count = cvCreateFileCapture("Video.avi");

    //Query for getting image size and depth for creating lastframe Image pointer.
    frame cap.get(CV_CAP_PROP_FRAME_COUNT); //get the frame count
    cap.set(CV_CAP_PROP_POS_FRAMES,count-1); //Set index to last frame
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);

    while(1)
    {
        Mat frame;
        bool success = cvQueryFrame( capture ); 
cap.read(frame); 
        if ( !frame ) return;
     lastframe=cvCreateImage(cvGetSize(frame),8,3);

     while (1)
     {
       frame = cvQueryFrame( capture );
        if ( !frame ) (!success){
          cout << "Cannot read  frame " << endl;
          break;
        cvCopy(frame,lastframe, NULL);
     }

  if(lastframe)
    cvSaveImage("lastframe.jpeg",lastframe);
    cvReleaseImage(&lastframe);
    cvReleaseCapture( &capture );
        imshow("MyVideo", frame);
        if(waitKey(0) == 27) break;
    }

Hope these helpful.....

Hi, You can refer OpenCV documentation about Reading and Writing Images and Video

  • Get total frame count using using VideoCapture::get by passing the id id CV_CAP_PROP_FRAME_COUNT.

  • Then using using VideoCapture::set set the index to next frame to be be captured, note that the index is 0 0 based.

See below code

 VideoCapture cap("video1.mp4"); 
    if( !cap.isOpened()){
         cout << "Cannot open the video file" << endl;
         return -1;
    }

    double count = cap.get(CV_CAP_PROP_FRAME_COUNT); //get the frame count
    cap.set(CV_CAP_PROP_POS_FRAMES,count-1); //Set index to last frame
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);

    while(1)
    {
        Mat frame;
        bool success = cap.read(frame); 
        if (!success){
          cout << "Cannot read  frame " << endl;
          break;
        }
        imshow("MyVideo", frame);
        if(waitKey(0) == 27) break;
    }

Hi, You can refer OpenCV documentation about Reading and Writing Images and Video

  • Get total frame count using VideoCapture::get by passing the id CV_CAP_PROP_FRAME_COUNT.

  • Then using VideoCapture::set set the index to next frame to be captured, note that the index is 0 based.

See below code

 VideoCapture cap("video1.mp4"); 
    if( !cap.isOpened()){
         cout << "Cannot open the video file" << endl;
         return -1;
    }

    double count = cap.get(CV_CAP_PROP_FRAME_COUNT); //get the frame count
    cap.set(CV_CAP_PROP_POS_FRAMES,count-1); //Set index to last frame
    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE);

    while(1)
    {
        Mat frame;
        bool success = cap.read(frame); 
        if (!success){
          cout << "Cannot read  frame " << endl;
          break;
        }
        imshow("MyVideo", frame);
        if(waitKey(0) == 27) break;
    }