Reading video sequences

To process a video sequence, we should be able to read each frame. OpenCV has developed an easy-to-use framework that can work with video files and camera input.

The following code is a videoCamera example that works with a video captured from a video camera. This example is a modification of an example in Chapter 1, Getting Started, and we will use it as the basic structure for other examples in this chapter:

#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int videoCamera()
{
    //1-Open the video camera
    VideoCapture capture(0);

    //Check if video camera is opened
    if(!capture.isOpened()) return 1;

    bool finish = false;
    Mat frame;
    Mat prev_frame;
    namedWindow("Video Camera");

    if(!capture.read(prev_frame)) return 1;

    //Convert to gray image
    cvtColor(prev_frame,prev_frame,COLOR_BGR2GRAY);

    while(!finish)
    {
        //2-Read each frame, if possible
        if(!capture.read(frame)) return 1;

        //Convert to gray image
        cvtColor(frame ,frame, COLOR_BGR2GRAY);

        //Here, we will put other functions 

        imshow("Video Camera", prev_frame);

        //Press Esc to finish
        if(waitKey(1)==27) finish = true;

        prev_frame = frame;
    }
    //Release the video camera
    capture.release();
    return 0;
}

int main( )
{
    videoCamera();
}

The preceding code example creates a window that shows you the grayscale video's camera capture. To initiate the capture, an instance of the VideoCapture class has been created with the zero-based camera index. Then, we check whether the video capture can be successfully initiated. Each frame is then read from the video sequence using the read method. This video sequence is converted to grayscale using the cvtColor method with the COLOR_BGR2GRAY parameter and is displayed on the screen until the user presses the Esc key. Then, the video sequence is finally released. The previous frame is stored because it will be used for some examples that follow.

Note

The COLOR_BGR2GRAY parameter can be used in OpenCV 3.0. In the previous versions, we also had CV_BGR2GRAY.

In the summary, we have shown you a simple method that works with video sequences using a video camera. Most importantly, we have learned how to access each video frame and can now make any type of frame processing.

Note

With regard to video and audio formats supported by OpenCV, more information can be found at the ffmpeg.org website, which presents a complete open source and cross-platform solution for audio and video reading, recording, converting, and streaming. The OpenCV classes that work with video files are built on top of this library. The Xvid.org website offers you an open source video codec library based on the MPEG-4 standard for video compression. This codec library has a competitor called DivX, which offers you proprietary but free codec and software tools.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset