Skip to content

OpenCV – Extracting RGB from a color image

September 8, 2010

In this post I would like to go into details of a color image. In opencv, an image is represented as a 1 dimensional entity. From the top left hand corner of the image, each pixel is represented as BGR in an array form. That is. each row of pixels is represented one after another where each pixel information is given completely. An RGB image is a 3 channel image where each pixel is represented by three 8 bit values corresponding to blue, green and red components. Here, I would give an example how to extract only the blue components of an input image.


int main(int argc, char **argv)

 const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
 IplImage* img = 0;
 int h, w, step, channels, i, j, k;
 uchar *data, *trans;
 img = cvLoadImage(imagename, CV_LOAD_IMAGE_UNCHANGED);
 printf("Could not load image file: %s\n", imagename);
 h = img->height;
 w = img->width;
 step = img->widthStep;
 channels = img->nChannels;
 data = (uchar *)img->imageData;

 IplImage* RGB = cvCreateImage(cvGetSize(img), 8, 1);
 trans = (uchar *)RGB->imageData;
 int trans_step;
 trans_step = RGB->widthStep;

//extracting necessary components
 int col = 0;
 for(i=0; i<h; i++)
 for(j=0; j<w; j++)
 trans[i*trans_step+j] = data[i*step+j*channels+col];

 cvNamedWindow("OpenCV", CV_WINDOW_AUTOSIZE);
 cvShowImage("OpenCV", RGB);

 return 0;

In this example code, we read an image into img and extract required component into a single channel image RGB. Here, in the code section mentioning extraction features takes the blue components into the image data array trans as col is set as 0. That is we are copying every one first value representing an image, i.e. the blue component into our new image. If we set col = 1 we can obtain the green sections of the image and similarly col = 2 gives red components of the image.


From → OpenCV

  1. Kishore A permalink

    Good going. Keep writing- I’m sure it would be useful for someone who wants to get started with OpenCV.

    In any case, it’s good to put down your understanding in words- it would be great to look back after some time.

  2. navyenzo permalink

    If you want a much easier, faster and safer way to access image pixels, with automatic garbage collection, automatic type conversion, and more, check blImageAPI, an Image structure that wraps IplImage and provides all the good stuff.

    It is also fully compatible with opencv algorithms.

    You’ll find blImage here:

  3. Swagatika permalink

    Thank you very much for the code..It was a great help for me being a beginner. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: