Open Computer Vision

September 28, 2008

Color Detection by using Color space basics

Filed under: Color Detection,Image Processing,Open Computer Vision,OpenCV — Abhijeet @ 6:42 pm

This site has moved to



  1. Found ur blogpost informative.M very much interested in this field.M doing final year Engg. Project on Image Fusion using VC++/OpenCV.Would like suggestions for it wen needed.

    Comment by Vikram — February 25, 2009 @ 6:37 am | Reply

  2. Now it’s working perfect!
    Is there a way to find the coordinates of the ‘center of gravity’ of the red object?

    Thanks again,

    Comment by rtluser — April 3, 2009 @ 2:07 pm | Reply

  3. and once more small detail, line 27:
    channelsmono = monoimgbot->Channels;
    would be:
    channelsmono = monoimgbot->nChannels;
    Kris 🙂

    Comment by Kris — April 4, 2009 @ 7:11 pm | Reply

  4. Hi guys help me out.

    I would like to detect other colors than red, specially blue and green
    Can anyone help me w/ the coding


    Comment by gerald — April 5, 2009 @ 6:18 am | Reply

  5. Thanks a lot, your algorithm should be implemented in the library. OpenCV is so low level that it is unusable for color filtering.

    Comment by Bobby — May 21, 2009 @ 9:54 pm | Reply

    • Thank you…:)

      Comment by colouredpages — May 22, 2009 @ 3:46 am | Reply

  6. Thanx for the code guys, its a great help. Could you please explain how you came up with sthreshold = 140? Is it a random (i.e. experimented) value or calculated one (like hupper and hlower)

    Comment by achu18 — May 26, 2009 @ 2:09 am | Reply

    • Its random for sure and it differs in different environments….

      Comment by colouredpages — May 30, 2009 @ 4:35 pm | Reply

  7. Is my understanding of color space correct?
    for the following IplImage(img)

    IplImage *img ;
    int width = img->width;
    int height = img->height;
    int nchannels = img->nChannels;
    int step = img->widthStep;
    uchar *data =( uchar*)img->imageData;
    img->channelSeq is BGR

    for( i =0; i < height ; i++)
    for( j =0; j < width ; j++)
    data[i*step + j*nchannels +0] is the blue channel
    data[i*step + j*nchannels +1] is the green channel
    data[i*step + j*nchannels +2] is the red channel

    once you convert the img to hsv using cvCopy(img,hsvImg,NULL) then

    data[i*step + j*nchannels +0] is the hue value (between 0 and 180)
    data[i*step + j*nchannels +1] is the saturation i.e. threshold
    data[i*step + j*nchannels +2] is the value i.e. brightness

    Is this correct?

    Comment by achu18 — June 3, 2009 @ 1:09 am | Reply

    • Hey,

      Right now I am out of town but i still make sure that I check for the comments on my site.But i think you are doing a serious mistake..First of all you use cvCvtColor to change between color spaces …friends I tell you once again a certain range of numbers are converted to another range of numbers to extract a feature in the color conversion…please read the tutorial about the HSV and the color space conversion and then you would not have any problems….

      and after you are done with the conversion please treat the array as a normal simple array as you were treating the same earlier…arrays would remain arrays but the numbers would change the dimensions of the numbers would obviously be the same ..Please do not expect the same numbers to be present…I hope this helps….!!!

      Comment by colouredpages — June 4, 2009 @ 4:35 pm | Reply

      • sorry colouredpages,
        cvCopy was a typo, i meant cvCvtColor!

        Comment by achu18 — June 5, 2009 @ 12:29 am

    • Yes after you convert the Image to HSV you would handle the iplimage as an ordinary Iplmage….so you are right…and also you do not have the values above 180 for are correct ..finally great everything is fine….

      Comment by colouredpages — June 13, 2009 @ 7:08 am | Reply

  8. hey,,great work!

    just curios,,what if you want to detect other colors?

    and how did you arrive with the value of sthreshold?

    Comment by martin — June 27, 2009 @ 12:23 pm | Reply

    • Its really dyanmic and you have to guess,…

      Comment by colouredpages — June 27, 2009 @ 5:29 pm | Reply

  9. hey guys,

    does anyone knows the hupper and hlower for green or blue??


    Comment by john — October 21, 2009 @ 2:29 am | Reply

    • now lemme see how many people give approximately correct answers to this question..!!

      Comment by colouredpages — October 21, 2009 @ 6:14 am | Reply

      • so do u know the answer??

        Comment by john — October 22, 2009 @ 1:21 am

      • Ok then i do not know the answer..Have fun….I do not give answers to people who do not work by themselves…!!

        Comment by colouredpages — November 5, 2009 @ 6:25 pm

  10. Please the values of thresholds: sthreshold, hlower and hupper for detecting the blue ball, please it is urgent.thank you very much

    Comment by Amel — October 26, 2009 @ 4:37 pm | Reply

    • Could you please atleast experiment…? take the lower value to be 5 and the upper to be 10 and the next time take the lower value to be 10 and upper to be 15..proceed till you get the result…!! I am sure you would get the answer yourself by this method…please do not ask the next time for the values of the other colors..!!

      Comment by colouredpages — November 5, 2009 @ 6:23 pm | Reply

  11. Hi,

    I am new to opencv, still learning procedures and all the functions. I am trying to run your program but getting this error:

    Bad number of channels (Incorrect number of channels for this conversion code) in function cvCvtColor, . \cvcolor.cpp

    Can you advice whats the best way to tackle this?

    Jas Singh

    Comment by Jas Singh — November 4, 2009 @ 10:18 pm | Reply

    • Dude,

      I really do not think you should be getting an error like this…really if you look at the previous comments they were able to compile the code ..Please send me the screen shot and the complete error details a

      Comment by colouredpages — November 5, 2009 @ 6:21 pm | Reply

  12. I am student of engineering.I have tested this code to detect red ball from the ground(green area).But result is not good. what changes I should make?
    Thank you in advance.

    Comment by NK — November 10, 2009 @ 9:05 am | Reply

  13. I am student from eng. I have used this code to find red ball on ground image but it does not give good result. can not detect ball

    Comment by NK — November 10, 2009 @ 9:28 am | Reply

    • Dear,
      If you observe closely, i have always told that the program which I have provided would require some changes in the environment which you use the program in…please try to change some values… I have got many mails saying that the program is working fine..!!

      Comment by colouredpages — November 13, 2009 @ 10:04 am | Reply

  14. Just wanted to say thank you. This article was very helpful. keep it up! 🙂

    Comment by h — November 23, 2009 @ 10:03 pm | Reply

  15. Wow… thanks for all the information! All the information related with HSV and how to work with it in OpenCV has been very useful for me!

    Comment by Oscar — April 18, 2010 @ 1:54 pm | Reply

  16. The link you provide for HSV tutorial ( does not work). Can you give any other, or the correct path? I could not understand the hlower and hupper values. Can you please explain?
    Best regards,

    Comment by Paulo Trigueiros — May 6, 2010 @ 4:19 pm | Reply

    • The best link i have found is . Try to understand the third paragraph and do not complicate the issue….’h’, ‘s’,’v’, hue means color, saturation means the amount of color and value means the amount of brightness. simple do not think much….check the opencv documentation and find out the maximum and the minimum values those three components can take and tweak it a bit to make it work for you…(This way I am asking you to learn to drive the car and then get to know the internal details of the car(how it works))…:)

      Comment by colouredpages — May 7, 2010 @ 5:28 am | Reply

  17. Greate post. Keep writing such kind of info on your page. Im
    really impressed by your blog.
    Hello there, You have done an excellent job. I will definitely digg it and in
    my view recommend to my friends. I’m sure they’ll be benefited from this web

    Comment by web design company — April 6, 2013 @ 5:47 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

Create a free website or blog at

%d bloggers like this: