Open Computer Vision

June 13, 2009

Detecting Colors using RGB Color Space

Filed under: OpenCV — Abhijeet @ 10:21 am

This site has moved to



  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! 😀 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

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 )

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

Blog at

%d bloggers like this: