Java - 颜色识别程序永远找不到像素

标签 java image image-recognition

我正在尝试编写一个程序来扫描图像中的某些结构和颜色,但是我识别颜色的类没有正确计算颜色 RGB 元素。

我的类(class)目前看起来像这样:

  package com.tuskiomi.image;

public class ColorVector {

    public static final int twoPctLeniency = 328965;
    public static final int fivePctLeniency = 855309;
    public static final int tenPctLeniency = 1644825;


    public int x, y;
    public int RGB;
    public int leniency;

    public ColorVector(int x, int y, int RGB){
        this.x = x;
        this.y = y;
        this.RGB = RGB;
        this.leniency = ColorVector.twoPctLeniency;
    }

    public ColorVector(int x, int y, int RGB, int leniency){
        this.x = x;
        this.y = y;
        this.RGB = RGB;
        this.leniency = leniency;
    }

    public static boolean match( ColorVector vec, int rgb){
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >> 8) & 0xFF;
        int b = (rgb >> 0) & 0xFF;
        //System.out.print(rgb);
        //System.out.print("-"+vec.RGB);

        int er = (vec.RGB >> 16) & 0xFF;
        int eg = (vec.RGB >> 8) & 0xFF;
        int eb = (vec.RGB >> 0) & 0xFF;

        int tr = (vec.leniency >> 16) & 0xFF;
        int tg = (vec.leniency >> 8) & 0xFF;
        int tb = (vec.leniency >> 0) & 0xFF;

        int rdiff = Math.abs(r-er);
        int gdiff = Math.abs(g-eg);
        int bdiff = Math.abs(b-eb);
        //System.out.println("-"+rdiff+"-"+gdiff+"-"+bdiff);

        return (rdiff<tr)||(gdiff<tg)||(bdiff<tb);
    }

    public boolean match(int rgb){
        int r = (rgb >> 16) & 0xFF;
        int g = (rgb >> 8) & 0xFF;
        int b = (rgb >> 0) & 0xFF;

        System.out.print(rgb);
        System.out.println("-"+this.RGB);

        int er = (this.RGB >> 16) & 0xFF;
        int eg = (this.RGB >> 8) & 0xFF;
        int eb = (this.RGB >> 0) & 0xFF;

        int tr = (this.leniency >> 16) & 0xFF;
        int tg = (this.leniency >> 8) & 0xFF;
        int tb = (this.leniency >> 0) & 0xFF;

        int rdiff = Math.abs(r-er);
        int gdiff = Math.abs(g-eg);
        int bdiff = Math.abs(b-eb);

        return (rdiff<tr)&&(gdiff<tg)&&(bdiff<tb);
    }


}

您可以在我的 match() 函数中看到我使用了三个主要数字。宽大数 leniency、应该匹配的 RGB 代码 RGB 和输入值 rgb

这个类的内部工作是将输入的整数分离成 8 位 RGB 值,rgb。对于预期的 r、预期的 g 等,有预期的 8 位值 eregeb,还有宽大值tr,tg,tb`,用于公差 r 等

我从预期的 r g b 值中减去 r g b 值,如果绝对值小于容差值,则测试通过。

问题是,在调试时,我得到输入颜色的负值。这不是唯一的问题。我为我提供的图像放了一条完整的彩虹,即使有 10% 的宽大处理,我也永远找不到匹配项。

我不确定为什么会这样,因为我的 RGB 值直接来自 BufferedImage.getRGB(int x, int y)。

该函数的 Javadoc 如下所示:

获取RGB

public int getRGB(int x, int y)

返回默认 RGB 颜色模型 (TYPE_INT_ARGB) 和默认 sRGB 颜色空间中的整数像素。如果此默认模型与图像 ColorModel 不匹配,则会发生颜色转换。使用此方法时,返回数据中每个颜色分量只有 8 位精度。 如果坐标不在边界内,则可能会抛出 ArrayOutOfBoundsException。但是,不能保证显式边界检查。

参数:
x - 像素的 X 坐标,从中获取默认 RGB 颜色模型和 sRGB 颜色空间中的像素
y - 像素的 Y 坐标,从中获取默认 RGB 颜色模型和 sRGB 颜色空间中的像素返回:默认 RGB 颜色模型和默认 sRGB 颜色空间中的整数像素。

我不确定为什么这会使整数变为负数,或破坏我当前的识别代码。

我看过解码颜色的示例,这些示例与我的非常相似。虽然我自己还没有尝试过,但它们看起来几乎一模一样。

这就是我的所有线索,但如果您告诉我在哪里寻找,我相信我可以挖掘出更多线索。

最佳答案

虽然这当然不是全部问题,但我发现调用 match()ColorVector 上有自己的rgb值(value)返回 false ,因为返回 boolean 条件只检查 <而不是 <= .所以:

return (rdiff<tr)&&(gdiff<tg)&&(bdiff<tb);

应该是:

return (rdiff<=tr)&&(gdiff<=tg)&&(bdiff<=tb);

涵盖预期 rgb 和实际 rgb 之间没有差异的边缘情况。

关于Java - 颜色识别程序永远找不到像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48604318/

相关文章:

javascript - 动态调整图像映射和图像的大小

.net - 如何从 .NET 中的图标文件中提取特定图像?

java - 从音符垫/图像中检测 OpenCV 符头

java - 以编程方式将新类添加到项目中

java - 在 for 循环中实例化数组以创建 JTextFields

java - 为什么有很多语言使用 JVM?

java - Mockito verify + any 行为不可预测

javascript - 如何在 JavaScript 中调整图像大小和显示图像

python - python中的实时OCR

iphone - 使用Iphone相机识别特定位置的文本