android比较2个图像并突出显示差异

时间:2016-06-22 20:37:03

标签: java android opencv

将提供2张图像。我们需要找出它们之间的差异并突出它们。

到目前为止,我在JAVA中看到了这个solution,但由于android不支持BufferedImage,我无法继续进行。我已接近比较2个位图的像素,但面临着未来的问题。

我也尝试比较两个位图的像素,但它会突出显示所有非白色的颜色

void findDifference(Bitmap firstImage, Bitmap secondImage)
{
    if (firstImage.getHeight() != secondImage.getHeight() && firstImage.getWidth() != secondImage.getWidth())
        Toast.makeText(this, "Images size are not same", Toast.LENGTH_LONG).show();

    boolean isSame = true;

    for (int i = 0; i < firstImage.getWidth(); i++)
    {
        for (int j = 0; j < firstImage.getHeight(); j++)
        {
            if (firstImage.getPixel(i,j) == secondImage.getPixel(i,j))
            {
            }
            else
            {
                differentPixels.add(new Pixel(i,j));
                secondImage.setPixel(i,j, Color.YELLOW); //for now just changing difference to yello color
                isSame = false;
            }
        }
    }
    imgOutput.setImageBitmap(secondImage);
}

提前致谢。

2 个答案:

答案 0 :(得分:6)

在数字色度计的帮助下,我发现在视觉上看起来相似的图像的颜色之间存在非常微小的差异,这与@taarraas建议的非常相似。所以我拿了一个阈值并像这样解决了它。

private static final int threashold = 10;

void findDifference(Bitmap firstImage, Bitmap secondImage)
{
    if (firstImage.getHeight() != secondImage.getHeight() || firstImage.getWidth() != secondImage.getWidth())
        Toast.makeText(this, "Images size are not same", Toast.LENGTH_LONG).show();

    boolean isSame = true;

    for (int i = 0; i < firstImage.getWidth(); i++)
    {
        for (int j = 0; j < firstImage.getHeight(); j++)
        {
            int pixel = firstImage.getPixel(i,j);
            int redValue = Color.red(pixel);
            int blueValue = Color.blue(pixel);
            int greenValue = Color.green(pixel);

            int pixel2 = secondImage.getPixel(i,j);
            int redValue2 = Color.red(pixel2);
            int blueValue2 = Color.blue(pixel2);
            int greenValue2 = Color.green(pixel2);

            if (Math.abs(redValue2 - redValue) + Math.abs(blueValue2 - blueValue) + Math.abs(greenValue2 - greenValue) <= threashold)
//                if (firstImage.getPixel(i,j) == secondImage.getPixel(i,j))
            {
            }
            else
            {
                differentPixels.add(new Pixel(i,j));
                secondImage.setPixel(i,j, Color.YELLOW); //for now just changing difference to yello color
                isSame = false;
            }
        }
    }
    imgOutput.setImageBitmap(secondImage);
}

答案 1 :(得分:1)

逐像素地比较图像,您可以这样做:

private void findDifference(Bitmap firstImage, Bitmap secondImage) {
    Bitmap bmp = secondImage.copy(secondImage.getConfig(), true);

    if (firstImage.getWidth() != secondImage.getWidth()
            || firstImage.getHeight() != secondImage.getHeight()) {
        return;
    }

    for (int i = 0; i < firstImage.getWidth(); i++) {
        for (int j = 0; j < firstImage.getHeight(); j++) {
            if (firstImage.getPixel(i, j) != secondImage.getPixel(i, j)) {
                bmp.setPixel(i, j, Color.YELLOW);
            }
        }
    }

    imgOutput.setImageBitmap(bmp);
}