2

I'm working on my college project, in which I am required to do fingerprint comparison. This can be done by comparing two image and matching their pixel similarity (as per my finding).

Is there any API/library/SDK or anything available in Java, for comparing two images and getting the percentage match between them?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I don't know of any API, but are you just wanting to compare pixel colours for each pixel and obtain an average difference? – James Webster Sep 03 '11 at 09:18
  • comparing two images by using their pixel and getting similarity percentage between them –  Sep 03 '11 at 09:52

3 Answers3

6

Check out the OpenCV library. You can find there exactly what you need. Take a look here for example of how to compare images.

Community
  • 1
  • 1
Lior Ohana
  • 3,467
  • 4
  • 34
  • 49
3

Would something like this work for you. The commented lines are likely not quite right.

int numberOfPixels = 0;
float runningTotal = 0;    
for (int i = 0; i < image.width; i++)
{
    for (int j = 0; j < image.height; j++)
    {
        //Color a = image1.getPixel(i, j);
        //Color b = image2.getPixel(i, j);

        float differenceRed = abs(a.red() - b.red()) / 255;
        float differenceGreen = abs(a.green() - b.green()) / 255;
        float differenceBlue = abs(a.blue() - b.blue()) / 255;

        float differenceForThisPixel = (differenceRed + differenceGreen + differenceBlue) / 3;
        runningTotal += differenceForThisPixel;
        numberOfPixels++;

    }
}
averageDifference = (runningTotal / numberOfPixels);
James Webster
  • 31,873
  • 11
  • 70
  • 114
  • 1
    using this method can do the work in some cases, however, in most cases it is not enough. Images similarity is not measured by pixel to pixel comparison; it should involve pattern comparison, shadow comparison, contrast comparison, etc'. I wish life were that simple :-) – Lior Ohana Feb 08 '14 at 11:08
0

Images Similarity

Comparing two BufferedImage pixel by pixel and shows the similarity persantage. Images Dimensions(Width/Height) must be same.

 public static double similarity( BufferedImage image1, BufferedImage image2 ) throws IOException{
         int total_no_ofPixels = 0;      
         int image1_PixelColor, red, blue, green;
         int image2_PixelColor, red2, blue2, green2;
         float differenceRed, differenceGreen, differenceBlue, differenceForThisPixel;
         double nonSimilarPixels = 0l, non_Similarity = 0l;

         long startTime = System.nanoTime();
// A digital image is a rectangular grid of pixels, Dimensions with/Height = 1366/728 pixels.        
// Colours are usually expressed in terms of a combination of red, green and blue values.        
         for (int row = 0; row < image1.getWidth(); row++) {
            for (int column = 0; column < image1.getHeight(); column++) {
                  image1_PixelColor   =  image1.getRGB(row, column);                
                  red                 = (image1_PixelColor & 0x00ff0000) >> 16;
                  green               = (image1_PixelColor & 0x0000ff00) >> 8;
                  blue                =  image1_PixelColor & 0x000000ff;

                  image2_PixelColor   =  image2.getRGB(row, column);                
                  red2                = (image2_PixelColor & 0x00ff0000) >> 16;
                  green2              = (image2_PixelColor & 0x0000ff00) >> 8;
                  blue2               =  image2_PixelColor & 0x000000ff;

                        if (red != red2 || green != green2 || blue != blue2) {
                            differenceRed   =  red - red2 / 255;
                            differenceGreen = ( green - green2 ) / 255;
                            differenceBlue  = ( blue - blue2 ) / 255;
                            differenceForThisPixel = ( differenceRed + differenceGreen + differenceBlue ) / 3;
                            nonSimilarPixels += differenceForThisPixel;
                        }
                 total_no_ofPixels++;

                 if ( image1_PixelColor != image2_PixelColor ) {
                     image2.setRGB(row, column, Color.green.getGreen());
                 }
            }
        }
         long endTime = System.nanoTime();
         System.out.println(String.format( "%-2d: %s", 0, toString( endTime - startTime )));

         System.out.println(" Writing the difference of first_Image to Second_Image ");
         ImageIO.write(image2, "jpeg", new File("D:\\image2.png"));

         non_Similarity = (nonSimilarPixels / total_no_ofPixels);
         System.out.println( "Total No of pixels : " + total_no_ofPixels +"\t Non Similarity is : " + non_Similarity +"%");

         return non_Similarity;          
     }
     private static String toString(long nanoSecs) {
          int minutes    = (int) ( nanoSecs / 60000000000.0 );
          int seconds    = (int) ( nanoSecs / 1000000000.0 )  - ( minutes * 60 );
          int millisecs  = (int) ( (( nanoSecs / 1000000000.0 ) - ( seconds + minutes * 60 )) * 1000 );

          if      ( minutes == 0 && seconds == 0   )    return millisecs + "ms";
          else if ( minutes == 0 && millisecs == 0 )    return seconds + "s";
          else if ( seconds == 0 && millisecs == 0 )    return minutes + "min";
          else if ( minutes == 0                   )    return seconds + "s " + millisecs + "ms";
          else if ( seconds == 0                   )    return minutes + "min " + millisecs + "ms";
          else if ( millisecs == 0                 )    return minutes + "min " + seconds + "s";

          return minutes + "min " + seconds + "s " + millisecs + "ms";
       }

Brief explanation of James Webster post

Yash
  • 9,250
  • 2
  • 69
  • 74
  • small improvement : difference write as "jpeg" is not so good to find a pixel wrote green, use "png" is better. – rufushuang Jun 11 '17 at 02:32