Comparing two Images in Java

There are many scenarios where i tried to compare images but failed to compare them. Image comparison is a very deep concept where there involved lot many complex algorithms . In brief for Two images to be same we need to compare the two images pixel by pixel so i came across PixelGrabber class in java and started using it which gave a positive result, but not accurate.
The sample code for image comparision as follows:

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class Compare {

static void processImage() {

String file1 = "img1.png";
String file2 = "img2.png";

Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
Image image2 = Toolkit.getDefaultToolkit().getImage(file2);

try {

PixelGrabber grab1 =new PixelGrabber(image1, 0, 0, -1, -1, false);
PixelGrabber grab2 =new PixelGrabber(image2, 0, 0, -1, -1, false);

int[] data1 = null;

if (grab1.grabPixels()) {
int width = grab1.getWidth();
int height = grab1.getHeight();
data1 = new int[width * height];
data1 = (int[]) grab1.getPixels();
}

int[] data2 = null;

if (grab2.grabPixels()) {
int width = grab2.getWidth();
int height = grab2.getHeight();
data2 = new int[width * height];
data2 = (int[]) grab2.getPixels();
}

System.out.println("Pixels equal: " + java.util.Arrays.equals(data1, data2));

} catch (InterruptedException e1) {
e1.printStackTrace();
}
}

public static void main(String args[]) {
processImage();
}
}

Happy Coding 🙂

Advertisements

40 thoughts on “Comparing two Images in Java

  1. Sir , I have to check one circuitpad’s back image with another circuitpad if any line is missing then it shows the pixels of missing lines .
    pls tell me

    1. Hello Midhuna,

      Comming to matlab, its very powerful tool for Image Processing techniques compared to java. so u have a different procedures methods and coding style in matlab for this and i am not very familiar with matlab.

      Thanks.

  2. Hi Pavan,
    Good Morning.Thanks for this wonderful site.Please keep up the good work.Please share your knowledge and experiences through this website.Thank You.May GOD BLESS You.

  3. Very interesting artcile! I’m involved in a similar project but i have a doubt …. What would you do to compare images that are different in size ? Because two images can be the same but different in width and height!
    Regards and keep up your good work!

  4. Hi, I am doing project on content based image retrieval, I retrieved by color but not with shape and texture. please reply soon

    1. Sorry Naveen, i too dont have answer for your question ill post the same question to my friends if i get any answer, ill reply back

      Thanks,
      Pavan Kumar Mantha

    1. Hi Akasha,

      to my knowledge more automation is also injurious to humanity… but any have please send your idea in breif so that i will try to suggest some solution.

      1. Hello Sir,
        I was thinking how about wen I upload a pic the system generates an automatic caption for it, like the location where the pictures were taken, time at which the photo was clicked.So, I thought of using geo-tagging for this purpose. My question is whether it is a good approach or not

      2. Good Idea but to a wrong person, i think some research persons can help you but not me, if you get ans please let me also know that.

        Thanks.
        Pavan Kumar

    1. Hi Saran,

      This is a complex question and lot of R&D is going on, also CAPTCHA is a security and we can’t breach it through code, any have i wll do some R&D from my side and will get back to your question.

      Thanks
      Pavan Kumar Mantha

  5. @Mantha pavan we can get the info of the images from the meta data that exist with them.
    It can be accessed by using “javax.imageio” with the method getImageMetadata(0);

  6. Hello Sir,
    Just wanted to ask you if its possible to compare two faces to show if the person in both the images are same or the different person. Is there any algorithm for that?

    Thank you

  7. Hi
    Is there any possible way to compare a image in the web and the image in the desktop.If yes please provide the details.

    1. Hi Shubham,

      Yes you can pass the path instead of static images, but then your code should fetch images based on the path and then compare.
      in any case at the end of the day its images that should be compared.

      1. Hi,
        It was a mistake, somehow the way i was capturing the image to be compared had different sizes and dimension in Mac and Windows. Once I baselined the correct images, it was working as smooth as knife goes through butter 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s