Java BufferedImage Compare compareImage(BufferedImage img1, BufferedImage img2)

Here you can find the source of compareImage(BufferedImage img1, BufferedImage img2)

Description

compare Image

License

LGPL

Declaration

public static double compareImage(BufferedImage img1, BufferedImage img2) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import javax.imageio.ImageIO;

public class Main {
    public static double compareImage(Path img1File, Path img2File) throws IOException {
        BufferedImage img1 = ImageIO.read(img1File.toFile());
        BufferedImage img2 = ImageIO.read(img2File.toFile());
        return compareImage(img1, img2);
    }/*w  w w. j a  va 2 s .c  om*/

    public static double compareImage(BufferedImage img1, BufferedImage img2) {
        int height = Math.max(img1.getHeight(), img2.getHeight());
        int width = Math.max(img1.getWidth(), img2.getWidth());
        long matching = 0;
        long pixels = 0;
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int col1 = -1;
                if (x < img1.getWidth() && y < img1.getHeight()) {
                    col1 = img1.getRGB(x, y);
                }
                int col2 = -1;
                if (x < img2.getWidth() && y < img2.getHeight()) {
                    col2 = img2.getRGB(x, y);
                }
                if (Integer.valueOf(col1).equals(col2)) {
                    matching++;
                }
                pixels++;
            }
        }
        return (double) matching / pixels;
    }
}

Related

  1. compare(BufferedImage a, BufferedImage b)
  2. compare(BufferedImage p1, BufferedImage p2)
  3. compareBufferedImages(BufferedImage bufferedImage1, BufferedImage bufferedImage2)
  4. compareImage(BufferedImage biA, BufferedImage biB)
  5. compareImage(BufferedImage img1, BufferedImage img2)
  6. compareImages(BufferedImage imageOne, BufferedImage imageTwo)
  7. compareImages(BufferedImage imgA, BufferedImage imgB)
  8. compareImages(final BufferedImage firstImage, final BufferedImage secondImage)
  9. compareImages(final BufferedImage img1, final BufferedImage img2)