blCaptureDevice — A simple data structure to handle video sources in OpenCV

Introduction

When it comes to OpenCV, dealing with images and videos means dealing with finicky pointers.  This, as could be attested by all who have played with OpenCV before, can quickly lead to “out of memory” and other obscure error messages that the library loves so much to spit at you. As part of a series, I’m designing simple structures that make dealing with the library a breeze.

In this short article, I design a simple data structure used to handle video sources such as webcams and avi files.

Dependencies

  1. boost::shared_ptr from the boost c++ libraries
  2. CvCapture from the OpenCV library (obviously)
  3. blImage from my blImageAPI library

Clean Up Resources Functor

Since we’re going to use boost::shared_ptr to hold our capture devices, we’re going to need a simple functor for shared_ptr to properly release our capture devices, just like we did with blImage. We’re adding the functor inside the file blCleanResource.hpp, and the code looks as follows:

//-------------------------------------------------------
// Functor used to release an OpenCV Capture Device
//-------------------------------------------------------
class ReleaseCaptureDevice
{
public:

    // Overloaded operator used to
    // release a capture device
    void operator()(CvCapture* CaptureDevice)
    {
        // Check if we have a capture device
        if(CaptureDevice == NULL)
            return;

        // Release the image
        cvReleaseCapture(&CaptureDevice);

        // Nullify the pointer
        CaptureDevice = NULL;
    }
};
//-------------------------------------------------------

Design

Our capture device structure adheres to the same design philosophy as our blImage structure. It is a class called blCaptureDevice and it is captured in a header file called blCaptureDevice.hpp, which uses inline documentation.

blCaptureDevice.hpp (Click to see code…)

Usage

The following example shows how to use the blCaptureDevice structure to connect to a webcam and stream it to an OpenCV window. Notice how readable the code has become compared to standard OpenCV functions.

int main(int argc, char *argv[])
{
    // key used to quit program
    int key = 'a';

    // We're going to read from the
    // webcam and save to grayscale
    blImage<unsigned char> Frame;

    // Create the webcam capture device
    // and connect it to the webcam
    blCaptureDevice Webcam;
    Webcam.ConnectToWebcam(0);

    cvNamedWindow("Webcam",CV_WINDOW_AUTOSIZE);

    while( key != 'q' )
    {
        // Get a frame
        Webcam.QueryFrame(Frame);

        // Display current frame
        cvShowImage("Webcam",Frame);

        // exit if user press 'q'
        key = cvWaitKey(1);
    }

    return 0;
}

Downloads

I have put all the files into a zip file which can be downloaded here.  All you have to do is extract it somewhere, let’s say in a directory called blImageAPI, and then include the blImageAPI.hpp file as follows:

#include "blImageAPI/blImageAPI.hpp"

using namespace blImageAPI;

Note: Everything is declared in a namespace blImageAPI, and such you would use it as: blImageAPI::blImage

[download#1]

About Vincenzo Barbato

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few.

Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes.

Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration.

His outlook on life:

"Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

About Enzo

Known to his friends as Enzo, he's an outside-the-box engineer/researcher whose interests and expertise span many fields, including controls systems, multi-physics simulations, mechatronics, oil technologies, data analysis and machine vision just to name a few. Refusing to grow up, he's on a continuous journey to develop simple and creative solutions that have the power of disrupting industries by optimizing systems and processes. Married to a beautiful wife, with two beautiful daughters and two identical twin boys, his home is a never ending chaotic fountain of inspiration. His outlook on life: "Never blindly accept what you're told, listen, but then question, with curiosity, creativity and collaboration we can change the world"

2 Comments

  1. Pingback: blMemStorage — A simple data structure to wrap OpenCV’s CvMemStorage with shared_ptr | BarbatoLabs

  2. Pingback: blVideoThread — A simple class to capture video using opencv in a parallel thread | BarbatoLabs

Leave a Reply

Your email address will not be published. Required fields are marked *