2 Replies Latest reply: Mar 27, 2013 3:33 AM by 999429 RSS

    Image comparison in java

    999429
      Hi,
      Currently I need a piece of code to compare 2 images and return a % match. I have written a sample and was wondering is its ok or there are better ways to do. Please let me know of any jars for this that can be used in java. I tried googling it but did not find anything straightforward. Iam new to this area so any help is highly appreciated.

      The code :

      import java.awt.image.BufferedImage;
      import java.awt.image.Raster;
      import java.io.File;
      import java.io.IOException;
      import java.net.MalformedURLException;
      import java.net.URL;

      import javax.imageio.ImageIO;
      import net.coobird.thumbnailator.Thumbnails;

      public class ImageUtil {
           public static void main(String[] args) {
                boolean result = false;
                try {
                     result = compareImage(
                               new URL(
                                         "http://images3.wikia.nocookie.net/__cb20120817021531/angrybirds/images/5/51/7632301_2.png"),
                               new URL(
                                         "http://fc02.deviantart.net/fs70/f/2012/269/d/8/angry_birds___red___super_high_quality__by_tomefc98-d5fz9by.png"));
                     System.out.println(result);
                } catch (MalformedURLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
           }

           public static boolean compareImage(URL img1, URL img2) {
                boolean ret = false; // Stores result.
                int passed=0,failed=0,percentage=0;
                try {
                     

                     BufferedImage image1 = ImageIO.read(img1);
                     
                     BufferedImage image2 = ImageIO.read(img2);

                     image1=Thumbnails.of(image1).forceSize(image2.getWidth(), image2.getHeight()).asBufferedImage();

                     Raster r1 = image1.getData();
                     Raster r2 = image2.getData();
                     
                     

                     // Check image sizes and number of bands. If there are different
                     // then no need to compare images as they are definitely not equal.

                     System.out.println("image1 band" + r1.getNumBands()
                               + " image2 band" + r2.getNumBands() + " image1 width"
                               + r1.getWidth() + " image2 width" + r2.getWidth()
                               + " image1 height" + r1.getHeight() + " image2 height"
                               + r2.getHeight());
      //
                     if (r1.getNumBands() != r2.getNumBands()     ||r1.getWidth() != r2.getWidth()
                               || r1.getHeight() != r2.getHeight()) {
                          ret = false;
                          System.out.println("Primary comparison failure");
                          // image2.setData(r1);

                     }

                     else {
                          // #Bands and image bounds match so compare each sample in turn.
                          ret = true;
                          int numBands=0;
                          if( r1.getNumBands()>r2.getNumBands()){
                               numBands=r2.getNumBands();
                          }
                          else{
                               numBands=r1.getNumBands();
                          }
                          search: for (int i = 0; i < numBands; ++i) {
                               for (int x = 0; x < r2.getWidth(); ++x) {
                                    for (int y = 0; y < r2.getHeight(); ++y) {

                                         if (r1.getSample(x, y, i) != r2.getSample(x, y, i)) {
      //                                        // At least one sample differs so result is
      //                                        // false;
      //                                        ret = false;
      //                                        System.out.println("sample difference");
      //                                        // Use labeled break to terminate all loops.
      //                                        break search;
                                              failed++;
                                         }
                                         else{
                                              passed++;
                                         }
                                    }
                               }
                          }

                     }
                     percentage= (passed* 100)/(passed+failed);
                     System.out.println(percentage);
                     if(percentage>50){
                          ret=true;
                     }
                     else{
                          ret=false;
                     }
                     return ret;
                } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                return ret;
           }
      }
        • 1. Re: Image comparison in java
          gimbal2
          I tried googling it but did not find anything straightforward
          that's because this is not a straightforward problem in the sense that it is a common requirement, you are doing something only a very small percentage of people in the world want to do so there are likely no big ongoing projects to make an easy to use API for it; nobody would really profit from that. Research/university environments is more a source of this kind of material. Google "java image compare" - that is what you have to work with. Good luck.
          • 2. Re: Image comparison in java
            999429
            Yes, I did look for java image compare in google. I found explanation to some algorithms but iam not so comfortable in java to implement those in code. I thought I could use some available jars.