Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 547
Haris Avatar asked Jun 22 '16 20:06

Haris


People also ask

How can I compare two photos?

Click on the File > New > Image option. Go to the File > Open option and select two or three images that you want to compare. Press the Compare button to view differences between images.

How can I compare two files in Android?

Go to file name in project then press control then select Compare File and select another file you wish to compare. Seperate window will open up showing differences by colour contrast. Welcome to SO !

How do I compare two pixels in a picture?

You can compare two images using functions from PIL. The diff object is an image in which every pixel is the result of the subtraction of the color values of that pixel in the second image from the first image. Using the diff image you can do several things. The simplest one is the diff.


2 Answers

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);
}
like image 55
Haris Avatar answered Sep 27 '22 18:09

Haris


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);
}
like image 27
antonio Avatar answered Sep 27 '22 17:09

antonio