Thursday, August 9, 2012

Play Video in OpenCV

This is with the older OpenCV versions. I had problems using the new OpenCV 2.4.0 so I resorted to this older way of reading videos. It can also be used in the later OpenCV versions (i.e. they are backward compatible)


/* play a video file */

#include <highgui.h>

int main(int argc, char** argv) {
    /* Create a window */
    cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
    /* capture frame from video file */
    CvCapture* capture = cvCreateFileCapture( argv[1]);
    /* Create IplImage to point to each frame */
    IplImage* frame;
    /* Loop until frame ended or ESC is pressed */
    while(1) {
        /* grab frame image, and retrieve */
        frame = cvQueryFrame(capture);
        /* exit loop if fram is null / movie end */
        if(!frame) break;
        /* display frame into window */
        cvShowImage("Example2", frame);
        /* if ESC is pressed then exit loop */
        char c = cvWaitKey(33);
        if(c==27) break;
    }
    /* destroy pointer to video */
    cvReleaseCapture(&capture);
    /* delete window */
    cvDestroyWindow("Example2");

    return EXIT_SUCCESS;
}

No comments: