Java BufferedImage Compare imageDifference(BufferedImage imgA, BufferedImage imgB)

Here you can find the source of imageDifference(BufferedImage imgA, BufferedImage imgB)

Description

image Difference

License

Open Source License

Declaration

public static int imageDifference(BufferedImage imgA, BufferedImage imgB) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 ** ANGRYBIRDS AI AGENT FRAMEWORK/*from   w w w. j  av a 2  s. c o  m*/
 ** Copyright (c) 2013,XiaoYu (Gary) Ge, Stephen Gould,Jochen Renz
 **  Sahan Abeyasinghe, Jim Keys, Kar-Wai Lim, Zain Mubashir,  Andrew Wang, Peng Zhang
 ** All rights reserved.
 **This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. 
 **To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ 
 *or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
 *****************************************************************************/

import java.awt.image.*;

public class Main {
    public static int imageDifference(BufferedImage imgA, BufferedImage imgB) {

        int dHeight = Math.abs(imgA.getHeight() - imgB.getHeight());
        int dWidth = Math.abs(imgA.getWidth() - imgB.getWidth());

        int diff = 3 * 255 * dHeight * dWidth;
        for (int y = 0; y < Math.min(imgA.getHeight(), imgB.getHeight()); y++) {
            for (int x = 0; x < Math.min(imgA.getWidth(), imgB.getWidth()); x++) {
                final int colourA = imgA.getRGB(x, y);
                final int colourB = imgB.getRGB(x, y);

                diff += Math.abs((int) ((colourA & 0x00ff0000) >> 16)
                        - (int) ((colourB & 0x00ff0000) >> 16));
                diff += Math.abs((int) ((colourA & 0x0000ff00) >> 8)
                        - (int) ((colourB & 0x0000ff00) >> 8));
                diff += Math.abs((int) (colourA & 0x000000ff)
                        - (int) (colourB & 0x000000ff));
            }
        }

        return diff;
    }
}

Related

  1. compareImages(BufferedImage imageOne, BufferedImage imageTwo)
  2. compareImages(BufferedImage imgA, BufferedImage imgB)
  3. compareImages(final BufferedImage firstImage, final BufferedImage secondImage)
  4. compareImages(final BufferedImage img1, final BufferedImage img2)
  5. compareImgToFile(BufferedImage img, File file)