5
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv)
{
CvCapture* capture=0;
IplImage* frame=0;

capture = cvCaptureFromAVI("C:\\boy walking back.avi"); // read AVI video
if( !capture )
    throw "Error when reading steam_avi";

cvNamedWindow( "w", 1);

for( ; ; )
{
/*  int cvGrabFrame (CvCapture* capture);
    IplImage* cvRetrieveFrame (CvCapture* capture)*/
    frame = cvQueryFrame( capture );
if(!frame)
        break;
    cvShowImage("w", frame);

}
cvWaitKey(0); // key press to close window
cvDestroyWindow("w");
cvReleaseImage(&frame);
}

I am using openCV with VS2008. I have read in a video file and used CV_CAP_PROP_FRAME_COUNT to obtain the number of frames which was approximately 130 for a 4 second long video clip. I am doing a motion recognition of walking so I need to get every other 5 frames since between 5 frames, there is little change in the motion of the body. I have a program so far which allows me to obtain one frame of the video clip. However, I am unable to obtain different frames and also, I am not sure how to go about getting every other 5 frames. The above is the code used to get one frame of the video.

sue-ling
  • 399
  • 2
  • 11
  • 21

1 Answers1

6

You should be able to skip 4 frames, and then keep the 5th frame. Below is a small example I wrote to demonstrate this:

IplImage* skipNFrames(CvCapture* capture, int n)
{
    for(int i = 0; i < n; ++i)
    {
        if(cvQueryFrame(capture) == NULL)
        {
            return NULL;
        }
    }

    return cvQueryFrame(capture);
}


int main(int argc, char* argv[])
{
    CvCapture* capture = cvCaptureFromFile("../opencv-root/samples/c/tree.avi");

    IplImage* frame = NULL;
    do
    {
        frame = skipNFrames(capture, 4);
        cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
        cvShowImage("frame", frame);
        cvWaitKey(100);
    } while( frame != NULL );

    cvReleaseCapture(&capture);
    cvDestroyWindow("frame");
    cvReleaseImage(&frame);

    return 0;
}

Hope that helps :)

mevatron
  • 13,911
  • 4
  • 55
  • 72
  • Thanks for your help. Forgive me if I appear silly, but I am fairly new to OpenCV. When your code was run, the video clip ran at a faster speed, which of course, could be altered by adjusting the time of the delay from 100 etc. However, is there a way to obtain simply 1 in 5 frames of the video? For example, the output of the code will be a frame showing the person in mid-movement. But thank you so much for your assistance thus far. – sue-ling Feb 12 '12 at 06:46
  • 1
    So, are you asking if you can seek to frame X, where X can be any frame from 1 to 5? – mevatron Feb 20 '12 at 21:02
  • i was able to extract very 1 in 5 frames. Thank you for your help:) I'm now trying to create a mask image or copied image of a frame in the video. I want to upload the 2 images to show you what I am trying to accomplish but I don't have a 10 reputation yet:/ Would you still be able to advise me as to which openCV functions to use? – sue-ling Feb 21 '12 at 00:10
  • 1
    I up-voted your question, so now you have 10 points :) I will definitely try to help you. But, since this question is answered, just ask a new one, and everyone gets more points :) – mevatron Feb 21 '12 at 04:08
  • thank you so much. I just tried posting my new question when I got the notification that I need more than a 10 reputation to post images arghh. So I still posted the question but I don't know how in the world you all will be able to see the images now sigh. Still, can you go and look for the qu please? – sue-ling Feb 21 '12 at 05:32
  • @mevatron any idea on ho to do the same thing using python and opencv? – praxmon Mar 14 '14 at 04:55