blImageAPI — Convert an opencv image from RGB to HSV color space no matter what the depth is

Introduction

When we start playing with images, dealing with colors becomes an inevitibility. Over the past hundreds of years, different ways to look at colors have evolved in the form of color spaces. These color spaces help us conceptualize colors in advantageous ways. In this article I present a couple of functions that make it a snap to convert images from the RGB or BGR color spaces to the HSV color space and back again, no matter what the depth of the images are. In the end, you’ll be able to do the following:

  1. How do I convert an image from BGR to HSV?
  2. How do I convert an image from RGB to HSV?
  3. How do I convert an image from HSV to BGR?
  4. How do I convert an image from HSV to RGB?
  5. How do I do this for images of different depths?

The functions I present here also take into account possible errors and eliminate the “horrific” error messages coming from opencv, sometimes at speeds of hundreds per seconds if working with videos.

Dependencies

  1. blImage
    1. Used as the generic matrix
  2. cvCvtColor
    1. Used to actually split the colored image into multiple grayscale images.

HSV Functions List

The following lists the HSV functions I present through this article. The list will be updated as I create and insert more functions here. Note that with the following functions, the user does not have to pay attention to image size or image depth. The number of channels does have to be three. (Having the same size images will be faster)

  1. FromBGRtoHSVColor
    1. Takes a BGR color and turns it into an HSV color
  2. FromHSVtoBGRColor
    1. Takes an HSV color and turns it into a BGR color
  3. FromBGRtoHSV
    1. Takes a three color BGR image of any depth and size, and turns it into a three color HSV image of any other depth. The depths nor the sizes of the two images have to match.

The code

As it is the case with most of OpenCV’s functions, cvCvtColor makes it very easy to convert images from various color spaces to other ones. The problem, just like most of OpenCV’s functions is the use of pointers and the unpredictability that comes from using them.

Also, the requirements on image sizes and image depths, are rarely stated anywhere, and with opencv’s horrific error message system, it can cause major headaches.

The blImageAPI makes it easy to get around that problem. I’m saving the functions in a file called blHSV.hpp, to which I will be adding more functions dealing with the HSV color space conversions.

blImageChannels.hpp (Click to see code…)

Usage

This collection of functions makes it really easy to convert images from the BGR color space to the HSV one and back again. The following snippet shows just how easy and readable the code is. It connects to the webcam and grabs frames, which it then converts to HSV and back again (note that the images can be of any depth or size, unlike when using opencv’s cvCvtColor function):

#include "blImageAPI/blImageAPI.hpp"
using namespace blImageAPI;

int main(int argc, char *argv[])
{
    // Connect to the webcam
    blCaptureDevice Webcam;
    Webcam.ConnectToWebcam(0);

    // Create a frame to grab images from webcam
    blImage< blColor3<double> > Frame;

    // Create separate images for each channel
    blImage< blColor3<double> > FrameNegative;

    int key = 'a';

    while(key != 'q')
    {
        // Query a frame from the webcam
        Webcam.QueryFrame(Frame);

        // Show frame
        cvShowImage("WebcamColor",Frame);

        // Take the negative
        ImageNegative(Frame,FrameNegative);

        // Show the frame negative
        cvShowImage("Blue Channel",FrameNegative);

        // Wait for a key
        key = cvWaitKey(1);
    }
}

The resulting output is:

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.hpp"
using namespace blImageAPI;

Note: Everything is declared in the 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"

One Comment

Leave a Reply

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