'Compare images for similarity using java

I want to check similarity in between 2 images: enter image description here to enter image description here

Using code below, I get Difference Percentage-->8.132336061764388. First I resize images to be on the same size and then use the compare method.

I was expect to have small if not none similarity. What is not correct in the similarity check? Is there other precise method?

public static void compare(BufferedImage imgA, BufferedImage imgB) {
// Assigning dimensions to image
int width1 = imgA.getWidth();
int width2 = imgB.getWidth();
int height1 = imgA.getHeight();
int height2 = imgB.getHeight();

// Checking whether the images are of same size or
// not
if ((width1 != width2) || (height1 != height2))

  // Display message straightaway
  System.out.println("Error: Images dimensions mismatch");
else {

  long difference = 0;

  // treating images likely 2D matrix

  // Outer loop for rows(height)
  for (int y = 0; y < height1; y++) {

    // Inner loop for columns(width)
    for (int x = 0; x < width1; x++) {

      int rgbA = imgA.getRGB(x, y);
      int rgbB = imgB.getRGB(x, y);
      int redA = (rgbA >> 16) & 0xff;
      int greenA = (rgbA >> 8) & 0xff;
      int blueA = (rgbA) & 0xff;
      int redB = (rgbB >> 16) & 0xff;
      int greenB = (rgbB >> 8) & 0xff;
      int blueB = (rgbB) & 0xff;

      difference += Math.abs(redA - redB);
      difference += Math.abs(greenA - greenB);
      difference += Math.abs(blueA - blueB);
    }
  }

  double total_pixels = width1 * height1 * 3;

  double avg_different_pixels
          = difference / total_pixels;

  double percentage
          = (avg_different_pixels / 255) * 100;

  // Lastly print the difference percentage
  System.out.println("Difference Percentage-->"
          + percentage);
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source