Book Image

OpenCV Essentials

Book Image

OpenCV Essentials

Overview of this book

Table of Contents (15 chapters)
OpenCV Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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;

     ...