Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV Essentials

You're reading from   OpenCV Essentials Acquire, process, and analyze visual content to build full-fledged imaging applications using OpenCV

Arrow left icon
Product type Paperback
Published in Aug 2014
Publisher
ISBN-13 9781783984244
Length 214 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (10) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Something We Look At – Graphical User Interfaces 3. First Things First – Image Processing 4. What's in the Image? Segmentation 5. Focusing on the Interesting 2D Features 6. Where's Wally? Object Detection 7. What Is He Doing? Motion 8. Advanced Topics Index

Live input from a camera

Usually, the computer vision problems we face are related with processing live video input from one or several cameras. In this section, we will describe the recLiveVid example, which grabs a video stream from a webcam (connected to our computer), displays the stream in a window, and records it in a file (recorded.avi). By default, in the following example, the video capture is taken from the camera with cam_id=0. However, it is possible to handle a second camera (cam_id=1) and grab the video from it, setting an argument at the command line:

//… (omitted for brevity)
int main(int argc, char *argv[])
{
    Mat frame;
    const char win_name[]="Live Video...";
    const char file_out[]="recorded.avi";
    int cam_id=0; // Webcam connected to the USB port
    double fps=20;

    if (argc == 2)
        sscanf(argv[1], "%d", &cam_id);

    VideoCapture inVid(cam_id); // Open camera with cam_id
    if (!inVid.isOpened())
        return -1;

    int width = (int)inVid.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)inVid.get(CV_CAP_PROP_FRAME_HEIGHT);
    VideoWriter recVid(file_out, CV_FOURCC('F','F','D','S'), fps, Size(width, height));
    if (!recVid.isOpened()) 
        return -1;

    namedWindow(win_name);
    while (1) {
        inVid >> frame; // Read frame from camera
        recVid << frame; // Write frame to video file
        imshow(win_name, frame); // Show frame
        if (waitKey(1000/fps) >= 0)
            break;
    }
    inVid.release(); // Close camera
    return 0;
}

The code explanation is given as follows:

  • VideoCapture::VideoCapture(int device) – This class constructor initializes a VideoCapture object to receive a video from a camera rather than a file. In the following code example, it is used with a camera identifier:
    VideoCapture inVid(cam_id); // Open camera with cam_id
  • VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true) – This class constructor creates an object to write a video stream to a file with the name passed as the first argument. The second argument identifies the video codec with a code of four single characters (for example, in the previous sample code, FFDS stands for ffdshow). Obviously, only codecs actually installed in the local system can be used. The third argument indicates the frames per second of the recording. This property can be obtained from the VideoCapture object with the VideoCapture::get method, although it may return 0 if the property is not supported by the backend. The frameSize argument indicates the total size for each frame of the video that is going to be written. This size should be the same as the input video grabbed. Finally, the last argument allows writing the frame in color (default) or in grayscale. In the example code, the constructor is used with the ffdshow codec and the size of the video capture is as follows:
    int width = (int)inVid.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)inVid.get(CV_CAP_PROP_FRAME_HEIGHT);
    VideoWriter recVid(file_out, CV_FOURCC('F','F','D','S'), fps,Size(width, height));
    
  • void VideoCapture::release() – This method closes the capturing device (webcam) or the video file. This method is always called implicitly at the end of the program. However, in the preceding example, it is called explicitly to avoid wrong termination of the output file (only noticeable when playing the recorded video).
You have been reading a chapter from
OpenCV Essentials
Published in: Aug 2014
Publisher:
ISBN-13: 9781783984244
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image