【问题标题】:Java - Color recognition program never finds a pixelJava - 颜色识别程序永远找不到像素
【发布时间】:2018-07-14 05:33:50
【问题描述】:

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

我的班级目前看起来像这样:

  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。有预期的 8 位值 eregeb, for expected r, expected g, etc. and there are the leniency valuestr,tg,tb`,用于公差 r 等。

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

问题是,在调试时,我得到输入颜色的负值。这不是唯一的问题。我为我喂它的图像设置了一条完整的彩虹,即使有百分之十的宽大处理,我也永远不会得到匹配。

我不确定为什么会发生这种情况,因为我直接从 BufferedImage.getRGB(int x, int y) 获取 RGB 值。

该函数的 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 颜色空间中的整数像素。

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

我已经看到了与我的非常相似的颜色解码示例。虽然我自己没有尝试过,但它们看起来几乎相同。

这就是我掌握的所有线索,但如果你告诉我去哪里看,我相信我可以挖掘更多。

【问题讨论】:

  • 我会简化的。看看这里的代码:alvinalexander.com/blog/post/java/getting-rgb-values-for-each-pixel-in-image-using-java-bufferedi
  • @HemangRindani 这与我的代码相同,只是我不拉 alpha,也没有解释负值。
  • 还有其他想法吗?我还是很困惑

标签: java image image-recognition


【解决方案1】:

虽然这肯定不是问题的全部,但我发现使用自己的 ColorVector 值调用 match() 会返回 false,因为返回布尔条件仅检查 &lt; 而不是 @ 987654326@。所以:

return (rdiff&lt;tr)&amp;&amp;(gdiff&lt;tg)&amp;&amp;(bdiff&lt;tb);

应该是:

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

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

【讨论】:

  • 如果存在边缘情况,则 rdiff 为 0,并且始终小于 tr
  • 是的,但是当ColorVector使用第一个构造函数创建时,leniency没有明确设置,因此leniency默认为0。
猜你喜欢
  • 1970-01-01
  • 2018-08-06
  • 2016-02-09
  • 2014-01-09
  • 2013-03-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
相关资源
最近更新 更多