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

Reading and playing a video file

A video deals with moving images rather than still images, that is, display of a frame sequence at a proper rate (FPS or frames per second). The following showVideo example illustrates how to read and play a video file with OpenCV:

//… (omitted for simplicity)
int main(int argc, char *argv[])
{
    Mat frame; // Container for each frame

    VideoCapture vid(argv[1]); // Open original video file
    if (!vid.isOpened()) // Check whether the file was opened
        return -1;
    int fps = (int)vid.get(CV_CAP_PROP_FPS);
    namedWindow(argv[1]); // Creates a window
    while (1) {
        if (!vid.read(frame)) // Check end of the video file
            break;
        imshow(argv[1], frame); // Show current frame on window
        if (waitKey(1000/fps) >= 0)
            break;
    }
    return 0;
}

The code explanation is given as follows:

  • VideoCapture::VideoCapture(const string& filename) – This class constructor provides a C++ API to grab a video from the files and cameras. The constructor can have one argument, either a filename or a device index for a camera. In our code example, it is used with a filename obtained from the command-line arguments as follows:
    VideoCapture vid(argv[1]);
  • double VideoCapture::get(int propId) – This method returns the specified VideoCapture property. If a property is not supported by the backend used by the VideoCapture class, the value returned is 0. In the following example, this method is used to get the frames per second of the video file:
    int fps = (int)vid.get(CV_CAP_PROP_FPS);

    Since the method returns a double value, an explicit cast to int is done.

  • bool VideoCapture::read(Mat& image) – This method grabs, decodes, and returns a video frame from the VideoCapture object. The frame is stored in a Mat variable. If it fails (for example, when the end of the file is reached), it returns false. In the code example, this method is used as follows, also checking the end of file condition:
    if (!vid.read(frame)) // Check end of the video file
    break;

In the preceding example, the waitKey function is used with a computed number of milliseconds (1000/fps) trying to play the video file at the same rate it was originally recorded. Playing a video at a faster/slower rate (more/less fps) than that will produce a faster/slower playback.

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