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;
}

Adding OpenCV to Eclipse

Adding OpenCV to Eclipse:


  1. Download eclipse indigo or the latest C++ IDE version
  2. Create a project
  3. Properties > C/C++ Build > Settings. 
  • Under Libraries add "opencv_core", "opencv_cv" and all OpenCV libraries you will need
  • Under "Library Search Path" add the location of the built libraries, usually: "/usr/local/lib" on Mac
  • Under Includes add the path to the include header files (.h), usually: "usr/local/include/opencv"