Java颜色检测 [英] Java colour detection

查看:462
本文介绍了Java颜色检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在Java中实现一个功能,它读取一个图像,并能够检测红色,蓝色,绿色,黄色等色调在卫星图像分析程序的一部分。因此,例如在标准卫星图像中,蓝色将是水,所以我想程序读取多少像素是蓝色,然后可以说x%的图像是水。



我知道通过读取每个像素的RGB值可以使用整个逻辑语句,但是有更简单的方法吗?否则会有数百个if语句需要花费很长时间写入,但也需要很长时间来执行。理想的id像这样的东西:

  if(pixelValue = red){
redCounter ++;
}

这显然很简单,但它会节省



提前感谢。

解决方案

你必须读取每个像素。您可以使用

  int rgb = bufferedImage.getRGB(x,y) 

或者,您可以从图像获取数据缓冲区,并直接获取像素为 int [] 数组(如注释中所述),但请注意,这可能会对后来渲染此图像时的性能有一些影响。



下一步是检测像素是否具有某种颜色。当涉及到照片,比较像

  if(rgb == Color.BLUE.getRGB()){... } 

没有意义:像素不会完全大多数情况。相反,您可以通过将RGB值转换为HSB颜色空间来分析RGB值的色相。所以你可以做一些像

  float hsb [] = new float [3]; 
int r =(rgb>> 16)& 0xFF;
int g =(rgb>> 8)& 0xFF;
int b =(rgb)& 0xFF;
Color.RGBtoHSB(r,g,b,hsb);

然后 hsb 数组将包含色相,饱和度和亮度的颜色。这些值将介于0和1之间。色调乘以360将得到颜色的色调:





饱和度和亮度可用于检测近黑近白。所以你的最终分析可能大致(!)看起来像这样:

  if(hsb [1]< 0.1& hsb [2]> 0.9)近白(); 
else if(hsb [2] <0.1)almostBlack();
else {
float deg = hsb [0] * 360;
if(deg> = 0&& deg< 30)red();
else if(deg> = 30&& deg< 90)yellow();
else if(deg> = 90&& deg< 150)green();
else if(deg> = 150&& deg< 210)cyan
else if(deg> = 210&& deg< 270)blue();
else if(deg> = 270&& deg< 330)
else red();
}

这些嵌套的if语句也可以通过使用 NavigableMap 及其 floorEntry ceilingEntry 方法,但if语句可能在这里更容易理解。


Im looking to implement a feature in Java which reads an image and is able to detect where there are shades of red, blue, green, yellow, etc. as part of a satellite image analysis program. So for example in a standard satellite image, blue would be water so I would like the program to read how many pixels are blue and then it could say x% of the image is water.

I know it would be possible using a whole load of logic statements by reading the RGB value of each pixel but is there an easier way to do this? Otherwise there will be hundreds of if statements which is going to take a long time to write but also a long time to perform. Ideally id like something like this:

if (pixelValue = red) {
   redCounter++;
}

Which is obviously very simple but it would save having to go through every single possible RGB combination for red, blue, green, yellow, purple, etc. which are all colours present in some of the coloured images.

Thanks in advance.

解决方案

You have to read every pixel anyhow. You can do this with

int rgb = bufferedImage.getRGB(x,y);

Alternatively, you can obtain the data buffer from the image and directly obtain the pixels as an int[] array (as described in the comments), but note that this may have some impact on the performance when rendering this image afterwards.

The next step would be to detect whether the pixel has a certain color. When it comes to photos, a comparison like

if (rgb == Color.BLUE.getRGB()) { ... }

does not make sense: The pixels will not be perfectly blue in most cases. Instead, you could analyze the hue of the RGB value, by converting it into the HSB color space. So you could do something like

float hsb[] = new float[3];
int r = (rgb >> 16) & 0xFF;
int g = (rgb >>  8) & 0xFF;
int b = (rgb      ) & 0xFF;
Color.RGBtoHSB(r, g, b, hsb);

Then the hsb array will contain the hue, saturation and brightness of the color. These will be values between 0 and 1. The hue multiplied with 360 will give you the "tone" of the color:

The saturation and brightness can be used to detect pixels that are "nearly black" or "nearly white". So your final analysis could roughly (!) look like this:

if      (hsb[1] < 0.1 && hsb[2] > 0.9) nearlyWhite();
else if (hsb[2] < 0.1) nearlyBlack();
else {
    float deg = hsb[0]*360;
    if      (deg >=   0 && deg <  30) red();
    else if (deg >=  30 && deg <  90) yellow();
    else if (deg >=  90 && deg < 150) green();
    else if (deg >= 150 && deg < 210) cyan();
    else if (deg >= 210 && deg < 270) blue();
    else if (deg >= 270 && deg < 330) magenta();
    else red();
}

These nested if-statements could also be avoided by using a NavigableMap and its floorEntry and ceilingEntry methods, but the if-statements are probably easier to understand here.

这篇关于Java颜色检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆