java java中的图像比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7292208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
image comparison in java
提问by James Webster
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?
是否有任何 API/库/SDK 或 Java 中可用的任何东西,用于比较两个图像并获得它们之间的百分比匹配?
回答by Lior Ohana
回答by James Webster
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);
回答by Yash
Images Similarity
图像相似度
Comparing two BufferedImagepixel by pixeland shows the similarity persantage. Images Dimensions(Width/Height) must be same.
逐个像素比较两个BufferedImage并显示相似度。图片尺寸(宽/高)必须相同。
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 Websterpost
James Webster帖子的简要说明