Java BufferedImage Compare compareImage(BufferedImage img1, BufferedImage img2)

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

Description

Assume that img1, img2 sizes are equals

License

Apache License

Parameter

Parameter Description
img1 a parameter
img2 a parameter

Declaration

public static double compareImage(BufferedImage img1, BufferedImage img2) 

Method Source Code

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

import java.awt.image.BufferedImage;
import java.awt.image.Raster;

public class Main {
    /**// ww  w.  j av a  2  s . c om
     * Assume that img1, img2 sizes are equals
     * @param img1
     * @param img2
     * @return
     */
    public static double compareImage(BufferedImage img1, BufferedImage img2) {
        Raster data1 = img1.getRaster();
        Raster data2 = img2.getRaster();
        int width = data1.getWidth();
        int height = data2.getHeight();
        double[] p1 = new double[3];
        double[] p2 = new double[3];
        double result = 0;
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                data1.getPixel(x, y, p1);
                data2.getPixel(x, y, p2);

                result += Math.abs(comparePixel(p1, p2));
            }
        }
        return result;
    }

    public static double comparePixel(double[] p1, double[] p2) {
        double result = 0;
        for (int i = 0; i < p1.length; i++) {
            result += Math.abs(p2[i] - p1[i]);
        }
        return result;
    }

    public static int comparePixel(int[] p1, int[] p2) {
        int result = 0;
        for (int i = 0; i < p1.length; i++) {
            result += Math.abs(p2[i] - p1[i]);
        }
        return result;
    }
}

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)