How to define clear range for pixel color
Asked Answered
E

3

15

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?

Emergence answered 12/8, 2015 at 10:53 Comment(5)
At first i thought you could just set range for RGB Colours but this wont work. as you can see on the colour table: 2.bp.blogspot.com/_DmvFk3aIxDI/S9Kbb5BOjEI/AAAAAAAAADk/… blue starts at #000066 and #66FFFF is still blue. But not everything in between is blue.Cranford
I found this: #30362286 should help.Cranford
No, it doesn't! If you pick the value of blue in HUE, you can still change the parameters of Saturation and Value/Brightness so you won't see any blue anymore. Try here: colorizer.orgKlenk
How do you define what colors are "blue"? Is #000001 blue? How about #7F7F80 or #FEFEFF? Do you just want colors whose blue component is greater than the other components, or maybe whose hue is within a certain range (what range?) and saturation is above a certain threshold (what threshold?) in the HSV model?Enzymolysis
I want to get all the variation of the clear blue sky. And the sky has pretty much of different blue in it, but i'm not interessted in any clouds. Now to your answer, the proposed colors are not blue to me. So the blue component beeing higher then the others is not a sufficient requirement. How to set the range, the saturation and the threshold is my question!Klenk
C
8

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

Canaster answered 26/8, 2016 at 9:10 Comment(4)
Yes this is probably an ordinary approximation. Still I was looking for a way to bypass the "picking own values to suit the own needs".Klenk
Well, the hard question here is what exactly you need. If you want to obey dark and 'cloudy' tones just set both saturation and value to be more than 50 (or 0.5 in code). In previous comment you suggested that you're looking for 'clear sky' it is probably anywhere from navy to low saturated cyan, so i would try with saturation > 25 (below is too 'cloudy') and value > 50 (below is rather night sky tones). In the answer I provided rather universal blue definition from dark to light tones, and from tuquoise up to indigo.Canaster
@JürgenK. If you have your own definition of blue, please add it to your question. If you ask people to define a color for you, you get their own approximate definition. And in some situations, people see a blue dress as white.Tramline
@gre_gor. Well, I don't have an own definition of blue. Even if i had one, it would not be useful. That's why I'm looking for a universal solution.Klenk
C
4

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.

Cyndi answered 31/8, 2016 at 12:49 Comment(0)
N
0

Technically, below should be the implementation for blue shades.

boolean isBlueShade(int r, int g, int b)
{
    return (r == g) && (r != b);
}

We can argue that CYAN is also blue. But, technically that is not blue. If your application requires some related colors to be blue, then the above method implementation to include that range check.

I am just editing the above method just to communicate what I thought clearly, even though it is not the expected solution.

Niles answered 30/8, 2016 at 12:4 Comment(2)
this is not an answer to the question at all. the question is how to define the range check for blue colorKlenk
Basing on this definition white and black or even grayscale is also blue. RGB is not the intuitive model, what can be easily used to distinguish colors.Canaster

© 2022 - 2024 — McMap. All rights reserved.