【发布时间】:2015-04-04 20:42:43
【问题描述】:
我目前正在使用以下代码循环遍历图像中的像素,并返回像素的坐标,其 RGB 值与 if 语句中定义的相同:
outerloop:
for (int y = 0; y < image.getHeight(); y = y + 1) {
for (int x = 0; x < image.getWidth(); x = x + 1) {
Color mycolor = new Color(image.getRGB(x, y));
int red = mycolor.getRed();
int green = mycolor.getGreen();
int blue = mycolor.getBlue();
if (red == 183 & green == 86 & blue == 182){
System.out.println(x,y);
break outerloop;
}
}
}
现在的问题是 RGB 值在应用程序中每次变化都非常轻微,所以我试图为当前恒定的 RGB 值添加一种“容差”。例如,在一种情况下,红色可能是 185,绿色可能是 89,而蓝色可能是相同的 (182)。
我知道我可以在 if 语句中使用 OR (||) 函数定义所有条件,但由于这需要大量代码,有没有更简单的解决方案?例如,将正公差定义为常数并循环遍历此公差内的所有 RGB 值组合?
【问题讨论】:
-
if (Math.abs(red - 183) < tolerance && Math.abs(green - 86) < tolerance && ...等? -
转换为 HSV 是否更有意义?饱和度大致相同,或者至少可以忽略不计。我猜转换会很慢。
标签: java for-loop colors combinations rgb