Capturing Moving Pictures
The simplest way to get moving pictures into OpenCV is to use a webcam. This works without ffmpeg being installed, and as such provides a useful starting point.
Before pulling the frames from a camera (or a file), you will need to set up an object from which you can capture the frames. To do this, use the following:
CvCapture *capture = cvCaptureFromCAM(int index);
index refers to the number assigned to your camera by your operating system, usually 0. If that doesn't work, try 1.
ffmpeg users can also load video files using the same line but substituting AVI instead of CAM. In this case the parameter will be a char[ ] filename.
Once this is done you can query the camera (or the file) at any point in your program for a frame using:
IplImage *frame = cvQueryFrame(CvCapture* capture); where capture is the object above.
When you have finished using the capture, it should be released using
Saving Movies (ffmpeg only)
Create a video writer object, as follows:
CvVideoWriter *writer = cvCreateVideoWriter(char[ ] filename, int fourcc, double fps, CvSize frame_size, int type);
filename, as always, can include a path. fourcc specifies the 'four character code', or the video format: CV_FOURCC('P', 'I', 'M', '1') for MPEG-1,
CV_FOURCC('M', 'J', 'P', 'G') for motion-JPEG,
CV_FOURCC('D', 'I', 'B', ' ') for RGB avi files, or
CV_FOURCC('I', 'Y', 'U', 'V') for uncompressed YUV, 4:2:0 chroma subsampled.
fps gives the number of frames per second, frame_size is the size of the frames used (and should match those of the IplImages written), and type can be 0 (greyscale) or 1 (colour). Currently these only work in Windows.
Frames are then written with cvWriteFrame(CvVideoWriter* writer, IplImage* img) and the writer is released with cvReleaseVideoWriter(CvVideoWriter** writer).