cuber
cuber

Reputation: 371

How to determine colors in an Array in Java

I have a program which takes a picture and it then determines the RGB values of a portion of pixels I am interested in. I take the average RGB of the regions and put those in an array. What I want to do is go through that array and say based on its RGB value that it is for example "Orange". I want to do this without constraining the RGB values like this. if(r > 10 && g < 200 && b < 200) color is green. The reason being that lighting in the image can change a lot. So it could fall out of range if I give it hard constraints. I am trying to think of how to do it so that it is dynamic and works with no matter the lighting in the photo.

This isn't really a code problem I am trying to think of ways of how I can solve this. Can't seem to think of other ways of doing it without using constraints as I mentioned above which is why I came here.

Here is an example of a color array. (There are 24 colors in it)

Color Array: [java.awt.Color[r=6,g=115,b=77], java.awt.Color[r=6,g=115,b=77], java.awt.Color[r=6,g=115,b=77], java.awt.Color[r=6,g=115,b=77], java.awt.Color[r=248,g=184,b=40], java.awt.Color[r=241,g=26,b=27], java.awt.Color[r=0,g=38,b=183], java.awt.Color[r=0,g=38,b=183], java.awt.Color[r=178,g=168,b=204], java.awt.Color[r=198,g=148,b=22], java.awt.Color[r=185,g=140,b=6], java.awt.Color[r=0,g=38,b=183], java.awt.Color[r=241,g=26,b=27], java.awt.Color[r=236,g=212,b=255], java.awt.Color[r=237,g=70,b=20], java.awt.Color[r=237,g=70,b=20], java.awt.Color[r=237,g=70,b=20], java.awt.Color[r=237,g=70,b=20], java.awt.Color[r=0,g=38,b=183], java.awt.Color[r=236,g=212,b=255], java.awt.Color[r=241,g=26,b=27], java.awt.Color[r=255,g=189,b=71], java.awt.Color[r=241,g=26,b=27], java.awt.Color[r=236,g=212,b=255]]

So the first 4 indexes in the array would map to ["Green","Green","Green","Green"]

Upvotes: 1

Views: 665

Answers (1)

user7859067
user7859067

Reputation:

answer cuz cannot comment.

If you think checking all r, g, b, and alpha will need too many process for processing and would take time. So you you are right. But you are wrong as this work will be as easy as ABC for a OpenCL/CUDA (or generally GPGPU) program for example.

One solution could be converting the RGB form to HSL form, and simply change the L to 100% and again convert back it to RGB
For example, given color [3,50,2] in rgb form, which could be a dark green, the HSL value is [119,96,20]
now change the L value to 100%, and convert back the value to RGB which is [14,255,10]. So now it's simply ready for your constraint check as you mentioned.

Another solution could be change the constraint value into something relative to min and max values of given color.
for example, don't assume the g must be over 200 to mark the color as green, instead finds the max and min of all r, g, and b values, and now check if the g is more than 70% of max value.
Sample(EDIT):
Given color value as [3,50,2]. Now instead of assume the min value for checking a color (here the const 200), finds the max value of given color (which is 50 here) and assume the min value 70%(or anything) of max val. check the sample below.

int given_color=0x00033202;//3,50,2 in ARGB form in hex
int relative_max=(given_color&0xff);//not 200, default assume blue is max
int relative_min=(given_color&0xff);//not 200, default assume blue is min
int tmp;
for(int a=8;a<=16;a+=8){//check for red and green too
 tmp=(given_color>>a)&0xff;
 if(tmp<relative_min){relative_min=tmp;}
 if(tmp>relative_max){relative_max=tmp;}
}//now you have max and min of given color, here 50 and 2
//now assume the 74% of real max value is const val 200
//and the value you want to check for color
relative_max*=0.74;
relative_min*=0.74;
//now check over real amx and rel min over const values
if( (r<relative_min) && (g>relative_max) && (b>relative_max) ){}//could be cyan
//...etc

Upvotes: 2

Related Questions