Open Computer Vision

April 26, 2009

Understanding Color Space Basics

Filed under: OpenCV — colouredpages @ 9:40 am

This post once again concentrates on understanding “What is Color Space” and how can it be used for Different projects you do and how well can you use the basics to finish the same.I still see a lot of users asking questions about Color Spaces and how to use them and this post satisfies your Thirst…Look at this picture.

main1

This picture (from Wikipedia) shows a man wearing a blue dress with a green background.Now lets split this image channels into the constituent channels and then see what are the constituents of the image with the help of this program.

/*Program to remove the components and show them apart..
.make the users realize the importance
of color space in OpenCV*/

/*All the includes*/
#include "cv.h"
#include "highgui.h"
#include "stdio.h"
#include "conio.h"

int main() {

int i,j,k;
/*Load the image*/
IplImage* frame=cvLoadImage("Man Color Image.jpg",1);
cvNamedWindow("main", CV_WINDOW_AUTOSIZE);
cvNamedWindow("red", CV_WINDOW_AUTOSIZE);
cvNamedWindow("green", CV_WINDOW_AUTOSIZE);
cvNamedWindow("blue", CV_WINDOW_AUTOSIZE);
if(frame==NULL){ /*If image not found then exit.
This is the function of this loop */
printf("the file doesnot exist");
exit(0);
}/*creating 3 images all one channel*/
IplImage* blue=cvCreateImage( cvGetSize(frame), 8, 1 );
IplImage* green=cvCreateImage( cvGetSize(frame), 8, 1 );
IplImage* red=cvCreateImage( cvGetSize(frame), 8, 1 );

//-----------------------------------------
/*Setting the Image Channel of interest(COI)*/
cvSetImageCOI( frame, 1 );
/*Here CvCopy sees the COI and ROI and then copies the channels to be copied
Here we set the first channel as the channel of interest*/
cvCopy( frame, blue, NULL );/*Copy the first channel */

cvSetImageCOI( frame, 2 );/*Set the COI to second channel*/
cvCopy( frame, green, NULL );/*Copy the second channel*/
cvSetImageCOI( frame, 3 );/*Set the COI to third channel*/
cvCopy( frame, red, NULL );/*Copy the third channel*/

cvShowImage("main",frame);
cvShowImage("blue",blue);
cvShowImage("green",green);
cvShowImage("red",red);
cvSaveImage("main.jpg",frame);
cvSaveImage("blue.jpg",blue);
cvSaveImage("green.jpg",green);
cvSaveImage("red.jpg",red);

cvWaitKey();
cvDestroyWindow( "main" );
cvDestroyWindow( "red" );
cvDestroyWindow( "green" );
cvDestroyWindow( "blue" );

return 0;/*This statement is compiler specific and might change.
As i already said that I am using DEV C++ as my compiler*/
}

Blue Channel

Blue Channel

green1

Green Channel

Red Channel

Red Channel

Now why is the Dress in the Red Channel low in Intensity..? (Do not worry about the sizes of the Image I have changed them to suit the looks and spacing).Its because the Red color is less in the blue area “but it is not zero(for most of the pixels in real time imaging)”.

Just look at the upper left corner in the green Image behind the head of the man.Don’t you see the leaves of the tree clearly with more intensity.Its because the leaves are green and which means they have more green color and less of the other colors.Because they are green.A matter of Fact (the leaves are green because they do not absorb the green color they reflect the green color).

If  you look in the blue channel, the background is very dark which means that there is less “blue” intensity….think for some points like this from the image.

So any area in an image no matter what color it is has RGB values .
Now 8*8*8=(2^8)*(2^8)*(2^8)=2^24 which roughly comes to 16 million colors.So its is very difficult to detect specific colors using RGB hence we do a color space conversion.I have already posted a topic describing the importance of the HSV color space.

Now if you observe the higher the ‘R’ ,’G', ‘B’ value higher is the intensity and when R=G=B=255 which means that its pure white light, which again means that the intensity is contained in a combination of the RGB values…similarly some more arithmetic done on the RGB gives the HUE nd the saturation values.

Now look at the upper left corner of the green image …? Dont you think its brighter.The plants are green Just look at the thick green leaves they are brighter.If a part in the image is brighter then it means that it has higher intensity and higher intensity means that the higher is the number.

Just try to satisfy yourself with what you think and what is present.Ponder over this post for sometime it would be a good time investment.
Remember->brighter Region signifies higher intensity which again means higher numbers
Darker regions means the vice versa…please give me some feedback about my explanation
I will try to Improve the same.

5 Comments »

  1. Hi, good post. I have been pondering this topic,so thanks for sharing. I’ll definitely be coming back to your site.

    Comment by How I Lost Thirty Pounds in Thirty Days — May 4, 2009 @ 4:25 am | Reply

  2. Hi, good post. I have been thinking about this topic,so thanks for writing. I will definitely be coming back to your site. Keep up the good work

    Comment by How I Shed T h i r t y P o u n d s in Thirty Days — May 6, 2009 @ 1:33 pm | Reply

  3. Hi,

    Nice post..I liked your explanation. I am a newbie in OpenCV and I think your blog is a good way to go about it..

    thanks

    Comment by Aditya — April 13, 2010 @ 4:56 pm | Reply

  4. hi,

    this is a good way to illustrate what an RGB color space is. now i already know most of what you explained. the only thing that made it kind of hard to read towards the end is the way it is written in english and the fact of some spaces missing after punctuation marks.

    please bear in mind that i haven’t read your read you HSV topic which is my next target to figure out why HSV is favored against RGB. but i do know that HSV parameters don’t go quite up to 255, well at least not all.

    in any case this is quite good. and i was just a bit picky with some of the comments so please don’t take it personally. good job!

    regards,
    ivan

    Comment by Ivan — June 29, 2010 @ 12:04 am | Reply

  5. hi, thx to your effort, currently I am working on color filters. It really helped.

    Comment by RITESH RANJAN — March 5, 2011 @ 4:17 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.