Last active
January 30, 2017 22:52
-
-
Save kaliatech/cfc4f68d0eb896b495679c572dbcfb7d to your computer and use it in GitHub Desktop.
Percentage Image Difference - Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://rosettacode.org/wiki/Percentage_difference_between_images#Java | |
import java.awt.image.BufferedImage; | |
import javax.imageio.ImageIO; | |
import java.io.IOException; | |
import java.net.URL; | |
public class ImgDiffPercent | |
{ | |
public static void main(String args[]) | |
{ | |
BufferedImage img1 = null; | |
BufferedImage img2 = null; | |
try { | |
URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg"); | |
URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg"); | |
img1 = ImageIO.read(url1); | |
img2 = ImageIO.read(url2); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
int width1 = img1.getWidth(null); | |
int width2 = img2.getWidth(null); | |
int height1 = img1.getHeight(null); | |
int height2 = img2.getHeight(null); | |
if ((width1 != width2) || (height1 != height2)) { | |
System.err.println("Error: Images dimensions mismatch"); | |
System.exit(1); | |
} | |
long diff = 0; | |
for (int y = 0; y < height1; y++) { | |
for (int x = 0; x < width1; x++) { | |
int rgb1 = img1.getRGB(x, y); | |
int rgb2 = img2.getRGB(x, y); | |
int r1 = (rgb1 >> 16) & 0xff; | |
int g1 = (rgb1 >> 8) & 0xff; | |
int b1 = (rgb1 ) & 0xff; | |
int r2 = (rgb2 >> 16) & 0xff; | |
int g2 = (rgb2 >> 8) & 0xff; | |
int b2 = (rgb2 ) & 0xff; | |
diff += Math.abs(r1 - r2); | |
diff += Math.abs(g1 - g2); | |
diff += Math.abs(b1 - b2); | |
} | |
} | |
double n = width1 * height1 * 3; | |
double p = diff / n / 255.0; | |
System.out.println("diff percent: " + (p * 100.0)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment