7

I have a set of images of the sky, some with clouds, and some without.

Some are clear blue skies, some partly cloudy, some murky with white and grey clouds.

How can I determine that a particular RGB value is, more or less, blue?

Any shade of blue is 'blue' - light blue, dark blue, navy blue, sky blue - these are all blue.

I want to reduce the colors in the image down to 16. Then I want to make a histogram of White, Silver, Grey, Blue and 'everything else'.

How do I know from the RGB value what is a blue or a silver, a white or a grey, or something else?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Chris
  • 319
  • 1
  • 6
  • 17
  • 4
    Comparing colors in C# is easy. Determining "what is basically blue" is the hard part. – Jim Jun 19 '11 at 04:16

3 Answers3

13

RGB isn't the best color space to work in for this sort of thing. A color space such as HSV (also known as HSB) provides a more intuitive and descriptive model for a given color.

So, to get what you are after you can use the Hue of a given color. If you are dealing with a System.Drawing.Color structure you can use the GetHue() method which returns a hue value in degrees. Given a Hue wheel like the following you can define an upper and lower threshold (in degrees) for what Blue values you will accept.

Hue Wheel

Image processing and Color theory in general are far from trivial subjects. I would suggest that you find the simplest method that meets your requirements. This may be enough for you, or maybe not. If not then perhaps you can narrow the question down a bit for me.

Also realize that you will still need some threshold for the Brightness and Saturation components to ensure that you aren't actually dealing with black, white, or gray.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • I have a copy of Photoshop and I am getting different values from the photoshop HSB color picker dialog compared to c# Color.GetBrightness() and Color.GetSaturation. I guess I need to find a conversion routine from Photoshop to c# HSB – Chris Jun 19 '11 at 06:30
2

If the red and green values are close to the same, and if the blue value is greater than the red and green values, the color is going to be in the blue range. If all three values are about the same, you've got some version of grey, and if they're all close to their maximum the color will look white. It's impossible to be much more precise than that -- you'll need to just look at a color picker and figure out which region looks "blue" to you.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Thanks - I have been looking at the images and can now see the trend you are talking about. I appreciate the time you took to respond to me. cheers – Chris Jun 19 '11 at 04:21
  • "Impossible"? Use a different color space, i.e., use the Hue of the color instead of RGB. Far from impossible. – Ed S. Jun 19 '11 at 04:24
  • @Ed S.: Looking at hue certainly makes things easier, and it's a good suggestion for the OP (+1 for that), but what range for hue represents "basically blue" to the OP? I said it was "impossible to be more precise" simply because the question isn't more precise. I note that the answer you gave really doesn't get any closer to solving the question than I did. Specifically, the OP is looking to discern "blue" from "white," "silver," and "grey," so saturation and value come into play. Whether you choose RGB or HSV, you're still looking at all three components. – Caleb Jun 19 '11 at 04:49
  • That's true. If the problem requires more than a couple of simple thresholds then the OP will need to use a more sophisticated method of finding the "distance" between two colors, which pure blue as the reference color. – Ed S. Jun 19 '11 at 04:52
0

Maybe build a table with the RGB values for each of the 16 colors you want, then compare each pixel RGB value to each of the values in your table. To compare make a substraction like R1-R2 G1-G2 B1-B2 then sum up the differences and convert it to the color that is less different.

Related question: Compare RGB colors in c#

Community
  • 1
  • 1
zad
  • 3,355
  • 2
  • 24
  • 25
  • Apparently there is some sort of standard way of comparing colors its called Delta E check out this question http://stackoverflow.com/questions/3968179/compare-rgb-colors-in-c – zad Jun 19 '11 at 04:02
  • Thanks I will take a look at the Delta E and see what I find - cheers – Chris Jun 19 '11 at 04:26