What I'm trying to do is to test if a pixel is blue or not.
For example: The blue color is in RGB defined as rgb(0,0,255). Typical color depths are 8 bit (256 colours), 16 bit (about 65 thousand), 24 bit (about 16 million) and 32 bit (over 4 billion different colours). So there is clearly more than 1 shade of blue.
How do I define the range of the blue color and test for each pixel if it's blue or not? And what do i need to bear in mind regarding the different depths?
My code so far is:
BufferedImage image = ImageIO.read(file);
// Getting pixel color by position x and y
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int clr = image.getRGB(i, j);
Note 1:
http://www.workwithcolor.com/cyan-blue-color-hue-range-01.htm
The problem here is, what is in between the color steps?
An example would be great.
Note 2:
I just found a presentation about the topic i'm interested in:
http://cstwiki.wtb.tue.nl/images/06-opencv.pdf
On page 13 we can see the definition of red. But how can you define the other colors?
Use the ColorSync Utility calculator to get the color values of a pixel on your screen. In the ColorSync Utility app on your Mac, click Calculator in the toolbar of the ColorSync Utility window. Click the magnifying glass , then move the pointer over an area on the screen that you want to examine.
Line art pixels are represented by only one binary bit with values 0 or 1, used to denote Black or White (2 colors, no gray).
RGB - Three Numbers So essentially, any color can be encoded as three numbers .. one each for red, green, and blue. In RGB, a color is defined as a mixture of pure red, green, and blue lights of various strengths.
Each pixel on a computer screen is composed of three small dots of compounds called phosphors surrounded by a black mask. The phosphors emit light when struck by the electron beams produced by the electron guns at the rear of the tube. The three separate phosphors produce red, green, and blue light, respectively.
In my opinion the easiest way is to convert color format to HSV/HSB and then pick blue range for Hue. The exact range may be a problem, but I mostly use from 165 (below is green) to 240 (above is purple/violet). Saturation and Value/Brightness controls only shades, but be aware that Saturation near 0 would be rather gray with some blueish shade, and Value near 0 would be almost black. Because of that you can check also Saturation to be >10 and Value >15, but of course you can pick your own values to suit your needs.
Sample code (not tested):
boolean isBlue(int rgb){
Color color = new Color(rgb);
float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
// now [0] is hue, [1] saturation and [2] value/brightness
// all values are between 0 and 1 so you must divide hue by 360 and others by 100
if(hsb[0] > 165/360 && hsb[0] < 240/360 && hsb[1] > 0.1 && hsb[2] > 0.15)
return true;
return false;
}
Wiki about HSV
RGBtoHSB javadoc
Color picker to check colors in different systems
In the RGB color space this is not easy to do. For one, the way that colors are displayed is device-dependent, that is, what ever RGB-value you choose to display might look quite different on different screens. But even when this can somehow be controlled for, the colors that have been assigned specific names in English do not necessarily occupy the same volume within the RGB color cube, neither in terms of size nor shape. Because of this, there are a few color models that try to approximate the human perception of colors.
One contender for this is the CIELab color space. The "Lab" refers to how colors are represented in this model: via lightness L
of the color, the color's position on the red/magenta and green axis (a
), and its position on the yellow and blue axis (b
). Unfortunately though, there are no simple formulas for conversion between RGB or CMYK values and Lab*, because the RGB and CMYK color models are device-dependent. The RGB or CMYK values first must be transformed to a specific absolute color space, such as sRGB or Adobe RGB. This adjustment will be device-dependent, but the resulting data from the transform will be device-independent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With