Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting frames from .avi video using OpenCV

#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.

like image 967
sue-ling Avatar asked Feb 12 '12 05:02

sue-ling


People also ask

What is ret frame in OpenCV?

Basically, ret is a boolean regarding whether or not there was a return at all, at the frame is each frame that is returned. If there is no frame, you wont get an error, you will get None. gray = cv2. cvtColor(frame, cv2. COLOR_BGR2GRAY)

Does OpenCV work on videos?

To start working with videos using OpenCV, we use the following functions: Cv2. VideoCapture() : It establishes a connection to a Video.It takes a parameter that indicates whether to use the built-in camera or an add-on camera. The value '0' denotes the built-in camera.


1 Answers

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 :)

like image 113
mevatron Avatar answered Oct 12 '22 01:10

mevatron