Haris
Haris

Reputation: 1862

android compare 2 images and highlight difference

2 Images will be provided. We need to find differences between them and highlight them.

So far I have seen this solution in JAVA but as BufferedImage is not supported in android, I am unable to proceed further. I have come close to comparing pixels of 2 Bitmaps but facing issue ahead.

I have also tried comparing pixels of two bitmap, but it highlights all the non-white colors

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);
}

Thanks in advance.

Upvotes: 5

Views: 7531

Answers (2)

Haris
Haris

Reputation: 1862

With the help of digital color meter I detected there is very slight difference between colors of visually looking similar images quite similar what @taarraas also suggested. so I took a threshold value and solved it like this.

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);
}

Upvotes: 7

antonio
antonio

Reputation: 18262

Comparing the images pixel by pixel you can do it like this:

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);
}

Upvotes: 1

Related Questions