How to tell visible color from RGB values
Asked Answered
E

1

6

I'm working an image analyzation project that checks the rgb values at set locations in a host of images, and need to be able to know if a certain area is green or blue. Originally I thought I could do this by testing if g>b in the rgb, but I've come to realize that often there can be more blue than green in a green image, due to the mixture with red. How can I tell- possibly a formula or an algorithm, what a color visibly appears to be based on the rgb?

Elishaelision answered 20/5, 2015 at 23:0 Comment(5)
Just a guess, but transforming it to Luv or Lab color space might help.Pituri
@Pituri I'm not familiar with either one of those. Mind providing a link?Elishaelision
In addition, HSL color spaceInternalcombustion
@Elishaelision I don't know the details of those color spaces, either. What I know, is that they are supposed to be closer to human perception. L only describes the luminance and the other two values the color on two axis. As a starting point for further research, the wiki link of luv space: en.m.wikipedia.org/wiki/CIELUVPituri
There is a really good talk from Microsoft's MIX09 conference called Design Fundamentals for Developers (which I can't find a copy of anymore) where Robby Ingebretsen says "every programer should switch to HSV, because its the way a programer's brain sees color. The H represents the frequency of the color (apart from it's Saturation or Value)Guayaquil
I
5

You can convert RGB values to HSB values using the Color classes RGBtoHSB method. The resulting hue value falls between 0-1, with the hue value of green (0,255,0) at 0.33 and blue (0,0,255) at 0.66

float[] hsb = Color.RGBtoHSB(0, 255, 0, null);//green
System.out.println(hsb[0]);
hsb = Color.RGBtoHSB(0, 0, 255, null);//blue
System.out.println(hsb[0]);

From this you could create a metric for hue values 'closer' to green, for instance any hue value < 0.5 is more green than blue.

Below is an image depicting how the colors change in this color space, with hue on the X axis (note in this picture hue varies from 0-360 degrees, whereas the RGBtoHSB returns values 0-1)

enter image description here

Internalcombustion answered 20/5, 2015 at 23:21 Comment(3)
Worked like a charm! For anyone looking on, I found my green values were closer to .6 and the blue values were closer to .9, so the >.5 test might not always get the right results.Elishaelision
Not everyone sees colors the same way, or even if they do, assign them the same name. Especially the boundary between green and blue. Try arguing about it if you find someone who thinks something is blue when you think it's green or vice versa ;-)Containment
How did you find out those values, Jayjey?Digiovanni

© 2022 - 2024 — McMap. All rights reserved.