Open Computer Vision

June 13, 2009

Detecting Colors using RGB Color Space

Filed under: OpenCV — Abhijeet @ 10:21 am

This is a post which is based on Color detection Using the RGB Basics and It would be really useful in certain situations.This method uses the RGB Color Space to detect the Colors…Read on..

So friends, if you have used Matlab you would know, or if you have been working in Image Processing from some time now you would know that even if a pixel is more RED in Color you would have significant amount of green and the Blue Component.This is can be said as a property.

If you see a Red Apple on a tree and if you take a photograph of the same then if you analyse that picture you would find a strong Red Component.If we consider that Image to be an 8 bit Image then you would find that the Red part of the Picture would have the Red pixel value around 245-255 and the blue to be definitely lesser (in many occasions ) and same is the case with blue but in many occasions I have seen that both, the Green and the blue Components have a value more than 200..but still we can filter them out…

Lets analyse this Image..



Now we have a Red ball in the Middle  of the Image.

We also know that the Red channel is the last or the third channel.Which means we have to make sure we select pixels which have more red color than any other component.

We want to make sure that ( red  > 20+ green or red >20+ blue).
Which means i want to select the red pixels whose value is atleast 20 units greater than any other component So lets Analyse the program now..

We should also make a note that white is R=G=B=255 so we do not include and equal sign in the condition loop which we are going to analyse in the program below..

/*ALL the necessary header files*/
int main()
int i,j,k;
int height,width,step,channels;
int stepr, channelsr;
int temp=0;
uchar *data,*datar;
IplImage *frame=cvLoadImage("3d-09-5balls.jpg",1);
IplImage *result=cvCreateImage( cvGetSize(frame), 8, 1 );
if(frame==NULL ) {
puts("unable to load the frame");exit(0);}
printf("frame loaded");

height = frame->height;
width = frame->width;
step =frame->widthStep;
channels = frame->nChannels;
data = (uchar *)frame->imageData;
/*Here I use the Suffix r to diffenrentiate the result data and the frame data
Example :stepr denotes widthStep of the result IplImage and step is for frame IplImage*/

datar = (uchar *)result->imageData;

for(i=0;i < (height);i++) for(j=0;j <(width);j++)

/*As I told you previously you need to select pixels which are
more red than any other color
Hence i select a difference of 29(which again depends on the scene).
(you need to select randomly and test*/
if(((data[i*step+j*channels+2]) > (29+data[i*step+j*channels]))
&& ((data[i*step+j*channels+2]) > (29+data[i*step+j*channels+1])))
/*If you want to use cvErode you may...*/
/*Destroying all the Windows*/
return 0;

If you observe Carefully only the red parts of the Image have got Highlighted…wherever there is Red it is white in the Monochrome image and I did not select the image according to the Program but Vice versa.
You can also use this program on frames from a camera by
replacing the frame from the Image by the frame from the
You can also use this program for detecting colors and specially objects which are known to be of a particular color in combination with some more Operations.

The result is below..result

About these ads


  1. for(i=0;i < (height);i++) for(j=0;j < (width);j++)

    Comment by Nam — June 22, 2009 @ 6:34 am | Reply

    • thank you……………..!!!!!!!! I am really really grateful to you…

      Comment by colouredpages — June 22, 2009 @ 12:23 pm | Reply

  2. is it possible to use this code to process images larger than the monitor resolution?

    Comment by jin guan — August 11, 2009 @ 4:03 pm | Reply

  3. why does it depend on the monitor resolution..processing is different and showing the same image is different alll depends on the Iplimage only…so you can process any size image but loooking at the same depends on the monitor….

    Comment by colouredpages — August 13, 2009 @ 1:02 pm | Reply

  4. Hello,

    As I see this program detect red color can you please help how to detect the Blue one and what can I change in the code below?
    Thanks for your reply soon

    Best regards.

    Comment by Amel — August 21, 2009 @ 9:34 pm | Reply

  5. Please send me the code if possible at this mail

    Comment by Amel — August 21, 2009 @ 9:43 pm | Reply

  6. It is real easy…whatever i have done for the red thing please do the same for the blue thing …it is real easy please …and can but i do not want to send the code because it would dissolve the real purpose of ths blog..!

    Abhijeet AKA Colouredpages.

    Comment by colouredpages — August 22, 2009 @ 11:27 pm | Reply

  7. Thanks, I did it

    Comment by amel — August 22, 2009 @ 11:31 pm | Reply

  8. sorry for deleting someone’s comment by mistake…but lemme tell youu that i have done this just for educational purpose…I did not do this after reading a journal or a paper..!!

    Comment by colouredpages — September 4, 2009 @ 7:54 am | Reply

  9. i understood how to find the red color in a given image.can u tell how to find different colors in a given bitmap image using c sharp?pl fwd to mail address..

    Comment by nisha — October 3, 2009 @ 5:25 am | Reply

  10. thanks for the very useful source codes, however i have problems applying it into my visual C++6.0 program. any useful tips for me?
    note: i do not have the required header files i think.

    Comment by alvin — November 9, 2009 @ 11:04 am | Reply

  11. Hello everyone,

    I want to extract 2×2 white pixels from an image. I have tried cvGet2D function and taken pixel value. But this is very time consuming process.

    Can any one suggest suitable solution for this problem?


    Comment by NK — January 5, 2010 @ 9:30 am | Reply

  12. Great buddy thanks

    Comment by Usman Ahmad — March 31, 2010 @ 4:48 pm | Reply

  13. Yo my man whats up?

    Nice program! I just implemented it too, and it works fine, but I have a peculiar question and observation.

    I have noticed that the program works fine for only square .jpg images, such as 640 x 640, or 800 x 800. It doesn’t however work for rectangular .jpg images. It’s really strange and I can’t understand why this happens. I noticed that the modification I have to do to make the program work relies on the data element selected in the fors, as well as the IplImage * img->imageDataOrigin value. Can you help me out on this one guys ? =D


    Comment by Mecasickle — March 31, 2010 @ 10:45 pm | Reply

  14. Hello,

    it’s a very clear implementation, thanks.

    conio.h can’t be found on Mac OS X. But it works without it too.

    Comment by Mustafa — December 3, 2010 @ 11:58 am | Reply

    • Yeah I realize that conio.h is not needed… but i have a habit of typing all the includes ..kinda obsessed..!! ;)

      Comment by colouredpages — December 3, 2010 @ 12:38 pm | Reply

  15. Dear Sir,

    Thank you for the code. I would like to know the code to detect the centre of gravity of each ball and show gravity position on each.

    It will be highly appreciate if someone help me regarding this.

    Thanks in advance.

    Comment by Momin — August 1, 2011 @ 8:33 am | Reply


    Comment by tomilee67 — October 30, 2011 @ 12:58 pm | Reply

  17. Excellent work! Is there any way that we can compute the probability of detection of a certain color?

    Comment by alhakika — January 17, 2012 @ 8:06 pm | Reply

  18. thnx a lot, it worked very nicely!! Kindly tell me how to use the code for colours other then RGB.

    Comment by siddharth — January 25, 2012 @ 9:30 am | Reply

  19. plz how can i use this code”detect color” on android project

    Comment by emmma — February 17, 2012 @ 9:49 pm | Reply

  20. nice to change the code if i want to detect red and green balls,,pls help..

    Comment by eve36 — February 22, 2012 @ 3:27 pm | Reply

  21. how to detect blue color

    Comment by qurban — February 29, 2012 @ 5:41 pm | Reply

  22. Excellent work and thx for the code;) but I would like to know how can i improved this code to detect just red circle in the image and eliminated the other shape .
    Thanks in advance.

    Comment by Islem Gharbi — March 16, 2012 @ 10:32 am | Reply

  23. HI, thanks for the code! it helps me a lot in my project! :D but i have encounter this problem that there seems to be some leak memory when i run with video. is there any suggestion you can guide me?

    Thanks a lot in advance!

    Best Regards

    Comment by colin — April 17, 2012 @ 1:43 am | Reply

  24. i’m very thank you for ur help!!
    this article is very useful for me :) and source code is easy to catch up.

    Comment by de_doob — June 15, 2012 @ 2:19 am | Reply

  25. Thanks for this tutorial!
    Im interested where you got the pictures from. Could you tell me the source where you adopted the pictures? I would really appriciate your help :-) Thanks in advance

    Comment by Anonymous — June 17, 2012 @ 4:10 pm | Reply

  26. It is really very helpful! Thanks :-)

    Comment by Abhijeet Pise — June 20, 2013 @ 9:48 am | Reply

  27. […] This is a post which is based on Color detection Using the RGB Basics and It would be really useful in certain situations.This method uses the RGB Color Space to detect the Colors…Read on.. So friends, if you have used Matlab you would know, or if you have been working in Image Processing from some […]   Related Article: Open Computer Vision […]

    Pingback by Detecting Colors using RGB Color Space | Ragnarok Connection — July 16, 2014 @ 3:22 am | Reply

  28. owh its okay, I already found the solution, your code by the way thank you very much

    Comment by collolo — May 11, 2013 @ 3:25 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: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

The Rubric Theme. Blog at


Get every new post delivered to your Inbox.

Join 29 other followers

%d bloggers like this: