Skip to content

Instantly share code, notes, and snippets.

@shaheenkdr
Created July 10, 2015 08:57
Show Gist options
  • Save shaheenkdr/8c0158bf010ed800bd16 to your computer and use it in GitHub Desktop.
Save shaheenkdr/8c0158bf010ed800bd16 to your computer and use it in GitHub Desktop.
Image comparison | To compare two images given their URL
//SIMPLE PROGRAM CREATED BY TEAM BLOX AS PART OF STARTUP BOX PROGRAM
//USER CAN GIVE TWO IMAGE URLS AND THE PROGRAM COMPARES RGB VALUES OF PIXELS AND RETURNS THE SIMILARITY ON A SCALE OF 0-100
//ANY QUERIES CONTACT: shaheenkader@outlook.com
// : syameradi@gmail.com
// : afrahnv@gmail.com
package imager;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URL;
public class Imagecompare
{
public static void main(String args[])
{
String s1;
BufferedImage img1 = null;
BufferedImage img2 = null;
try {
URL url1 = new URL("http://www.lac.inpe.br/JIPCookbook/Resources/ImageSimilarity/d02.jpg");
URL url2 = new URL("http://www.lac.inpe.br/JIPCookbook/Resources/ImageSimilarity/s02.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;
double percnt = 100.0-(p*100.0);
System.out.println("PERCENT: " +percnt);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment