blVideoThread2 — Another way to capture video using opencv in a parallel thread

This entry is part 15 of 17 in the series blImageAPI -- BarbatoLabs Image API

Introduction

Just as I mentioned in my previous blVideoThread article, when capturing video, our programs can be limited by the slow speeds of the capturing hardware.  One way around this, is to obviously separate the capturing process from the main program flow.

In this very short article I derive from the blVideoThread class I presented last time, and add a couple of funtions to further speed up the capturing process.

The idea

The idea behind this class is simple, as shown in the following figure.  Opencv provides us with a new image everytime we query an image from the video source.  In the previous article, we would let the parallel thread query a frame, pause until the main thread grabbed the frame and told it to go ahead and get another one, and the whole process would start over again.

I couldn’t help but feel that there were still road blocks in the capturing process.

The class in this article has an additional frame used as an intermediate buffer between the worker thread and the main thread.  The concept is very simple:

  1. Start the capturing thread and let the program continuously query frames.
    1. Every time the worker thread queries a frame, we shove the new frame into our buffer frame, one pixel at a time.
  2. The main program can read the buffer frame at anytime, without being afraid of the buffer frame being deleted.
    1. This is because the buffer frame never changes size, never gets deleted.  The worker thread feeds information to it, and the main thread reads that information anytime it wants to.

The code

The code to implement this is very simple, and I created the class blVideoThread2 and saved it in a file called blVideoThread2.hpp

blVideoThread2.hpp (Click to see…)

Usage

Using this class is super simple. You create an instance, connect it to the webcam and start the capturing thread. Its destructor automatically stops the thread, so no need to explicitly stop the thread in the main program, although functions have been provided to start, pause and stop the capturing thread.

The following snippet shows how one might use this in a program.

// We have to include the blImageAPI
// library and define the following macro
// to use the video thread
#include "blImageAPI/blImageAPI"
using namespace blImageAPI;

#define USE_BL_VIDEOTHREAD

// This code should go somewhere at the
// beginning of the program, when first creating
// the video thread
// Here we initialize the video thread, connect it
// to a webcam and start the parallel capturing
blVideoThread2 MyVideoThread;
MyVideoThread.ConnectToWebcam(0);
MyVideoThread.StartCapturingThread();

// We then somewhere in a loop, capture
// the frame and do whatever we want with
// it, here we create a texture with it using
// blTexture
// Notice how we're not checking whether
// we have a frame available to read like
// we were doing with the previous blVideoThread class
m_RollerTexture->LoadImageToTexture(m_VideoThread.GetFrame(),true,false);

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

Note2: Also, to use the video thread, you have to define the macro USE_BL_VIDEOTHREAD (I did this so that in case you don’t have the sfml library, if you don’t define the macro, the compiler won’t yell at you).

blImageAPI.zip -- Ver Jun/06/2011 1:43pm (5060)

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"

One Comment

  1. Pingback: blTexture — Load an IplImage into an opengl texture and create webcam and video textures using the blImageAPI | BarbatoLabs

Leave a Reply

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