image comparison in java [closed]

别等时光非礼了梦想. 提交于 2019-12-11 13:27:20

问题


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?


回答1:


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




回答2:


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);



回答3:


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



来源:https://stackoverflow.com/questions/7292208/image-comparison-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!