blImageAPI — separate an opencv image into its individual channels

Introduction

I have created the blImageAPI to make life easier when it comes to programming images and videos. In this article I present a couple of functions that make it a snap to play with image channels. In the end, you’ll be able to do the following:

  1. How do I get an individual channel from my image?
  2. How do I combine multiple grayscale images to form a colored image in the way I want?
  3. How could I apply different algorithms to different channels from the same image?

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. cvSplit
    1. Used to actually split the colored image into multiple grayscale images.
  3. cvMerge
    1. Used to actually combine the multiple grayscale images into a single colored image.

Channel Functions List

The following lists the image channel 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 and that’s taken care in the functions. (Having the same size images will be faster)

  1. QueryChannel
    1. (Updated Dec/31/2010) — Grab a single channel from a two-channel complex image
    2. Grab a single channel from a three-channel colored image
    3. (Updated Dec/31/2010) — Grab a single channel from a four-channel colored image
  2. QueryChannels
    1. (Updated Dec/31/2010) — Grab all two channels from a two-channel complex image
    2. Grab all three channels from a three-channel colored image
    3. (Updated Dec/31/2010) — Grab all four channels from a fourr-channel colored image
  3. InsertChannel
    1. (Updated Dec/31/2010) — Insert a channel into a two-channel complex image
    2. Insert a channel into a three-channel colored image
    3. (Updated Dec/31/2010) — Insert a channel into a four-channel complex image
  4. CombineChannels
    1. (Updated Dec/31/2010) — Combine two single channel images into a two-channel complex image
    2. Combine three single channel images into a three-channel colored image
    3. (Updated Dec/31/2010) — Combine four single channel images into a four-channel colored image

The code

As it is the case with most of OpenCV’s functions, cvSplit and cvMerge make it very easy to work with image channels in many ways. 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, number of channels, 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 blImageChannels.hpp, to which I will be adding more functions dealing with image channels as I develop them.

blImageChannels.hpp (Click to see code…)

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. Hi, i think that i saw you visited my blog so here i am!.I am looking for ways to add things to my website!Is it ok if i use some of the things i saw here?!

    • Sure, feel free to use it as you like. If you want, let me know what you would like to see next. Have fun and make sure to check out the other APIs

Leave a Reply

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