Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell visible color from RGB values

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?

like image 990
Jayjey Avatar asked May 20 '15 23:05

Jayjey


1 Answers

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

like image 154
copeg Avatar answered Nov 09 '22 07:11

copeg