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

Har*_*ris 5 java android opencv

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

到目前为止,我已经在JAVA中看到了这个解决方案,但由于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);
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Har*_*ris 7

在数字色度计的帮助下,我发现在视觉上看起来相似的图像的颜色之间存在非常微小的差异,这与@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);
}
Run Code Online (Sandbox Code Playgroud)