比较android中两个图像的RGB颜色值

时间:2012-12-11 11:44:22

标签: android image bitmap rgb

要获取一个图像的RGB值,我使用了以下代码片段

int[] pix = new int[picw * pich];
                 bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

                 int R, G, B,Y;

                 for (int y = 0; y < pich; y++){
                 for (int x = 0; x < picw; x++)
                     {
                     int index = y * picw + x;
                     int R = (pix[index] >> 16) & 0xff;     //bitwise shifting
                     int G = (pix[index] >> 8) & 0xff;
                     int B = pix[index] & 0xff;

                     //R,G.B - Red, Green, Blue
                      //to restore the values after RGB modification, use 
     //next statement
                     pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
             }}

我想比较两个图像,我知道比较像素值会更贵。我还分析了OpenCV库,但我不会  进入我的要求。

是否有任何算法可以在Android中使用RGB值比较图像?

还有其他比较RGB值的方法吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

我不确定您的要求是什么,但如果您只想比较两张图片的(RGB)调色板,则可能需要使用PaletteFactory中的Apache Commons Imaging (fka "Sanselan")方法:

PaletteFactory方法构建了集合(int[]List<>),然后可以对其进行迭代。我不确定你需要做什么样的比较,但是一个相当简单的例子,使用例如makeExactRgbPaletteSimple(),将是:

final File img1 = new File("path/to/image_1.ext")
final File img2 = new File("path/to/image_2.ext")
final PaletteFactory pf;
final int MAX_COLORS = 256;
final Palette p1 = pf.makeExactRgbPaletteSimple(img1, MAX_COLORS);
final Palette p2 = pf.makeExactRgbPaletteSimple(img2, MAX_COLORS);

final ArrayList<Int> matches = new ArrayList<Int>(Math.max(p1.length(), p2.length()));
int matchPercent;

// Palette objects are pre-sorted, afaik

if ( (p1 != null) && (p2 != null) ) {
  if (p1.length() > p2.length()) {
    for (int i = 0; i < p1.length(); i++) {
      final int c1 = p1.getEntry(i);
      final int c2 = p2.getPaletteIndex(c1);
      if (c2 != -1) {
        matches.add(c1);
      }
    }
    matchPercent = ( (int)( (float)matches.size()) / ((float)p1.length) * 100 ) )
  } else if (p2.length() >= p1.length()) {
    for (int i = 0; i < p1.length(); i++) {
      final int c1 = p2.getEntry(i);
      final int c2 = p1.getPaletteIndex(c1);
      if (c2 != -1) {
        matches.add(c1);
      }
    }
    matchPercent = ( (int)( (float)matches.size()) / ((float)p2.length) * 100 ) )
  }
}

这只是一个最小的例子,它可能编译也可能不编译,几乎肯定不是你在比较逻辑方面所需要的

它的基本操作是检查p1的每个成员是否也是p2的成员,如果是,则将其添加到matches。希望逻辑是正确的,没有保证。 matchPercentPalette s中存在的颜色百分比。

这可能是您想要的比较方法。这只是一个简单的例子。

你肯定需要使用第二个参数来makeExactRgbPaletteSimple()int max,因为我任意选择了256 - 请记住,如果(恼人地,imo)方法将返回null max太小了。

我建议从源代码构建,因为repos在相当长的一段时间内没有更新。该项目绝对不成熟,但对于中型图像和纯Java来说,它相当小,速度相当快。

希望这有帮助。

相关问题