Question

I deal with a project in Java, which loads an image and find color information of each pixel. I need to detect the sky on a color definition, which may seem like the sky - using the blue color range. Originally it seemed best to use RGB, but this necessitates a definition of all shades of blue (uncountable). I found the analysis method, where are used thresholds of HSV for each color, but have no idea how I could load some libraries using color ranges, respectively, how it could be solved. Thanks for any help.

Was it helpful?

Solution

You can convert a java.awt.Color to it's HSV values using the static method RGBtoHSB(...). You could then get the hue, and compare it to the boundaries of a range that - for your purposes - constitutes 'blue' - e.g.

private static final float MIN_BLUE_HUE = 0.5f; // CYAN
private static final float MAX_BLUE_HUE = 0.8333333f; // MAGENTA

public static boolean isBlue(Color c) {
  float[] hsv = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
  float hue = hsv[0];
  if (hue >= MIN_BLUE_HUE && hue <= MAX_BLUE_HUE){
    return true;
  } 
  return false;
}

I've offered some starting values for MIN_BLUE_HUE and MAX_BLUE_HUE, but what you set MIN_BLUE_HUE and MAX_BLUE_HUE to will depend upon on you're willing to accept as 'blue' - i.e. how much green or purple could there be, and it still be acceptable as sky?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top