如何使用基于均方误差的基于像素的图像比较度量来比较 java 中的一组图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21179019/
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
how to compare set of images in java using pixel based image comparision metric based on mean squared error?
提问by user3205339
In my project i have a set of images. I need to compare them. Each pixel from one image is compared to the pixel at the same location in all other images in the dataset. After applying mean squared error calculation to all of the pixels in image space, a set of different pixels are identified which represents pixels with varying color values in the images. I have compared and stored similarities pixels in a file for two images.but can't do this for 12 images.'code'
在我的项目中,我有一组图像。我需要比较它们。将一张图像中的每个像素与数据集中所有其他图像中相同位置的像素进行比较。在对图像空间中的所有像素进行均方误差计算后,识别出一组不同的像素,这些像素代表图像中具有不同颜色值的像素。我已经比较并在一个文件中存储了两个图像的相似性像素。但不能对 12 个图像执行此操作。“代码”
import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
class spe
{
public static void main(String args[])
throws IOException
{
long start = System.currentTimeMillis();
int q=0;
File file1 = new File("filename.txt");
/* if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}*/
FileWriter fw = new FileWriter(file1.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
File file= new File("2000.png");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
int[][] clr= new int[width][height];
File files= new File("2002.png");
BufferedImage images = ImageIO.read(files);
int widthe = images.getWidth(null);
int heighte = images.getHeight(null);
int[][] clre= new int[widthe][heighte];
int smw=0;
int smh=0;
int p=0;
//CALUCLATING THE SMALLEST VALUE AMONG WIDTH AND HEIGHT
if(width>widthe)
{
smw =widthe;
}
else
{
smw=width;
}
if(height>heighte)
{
smh=heighte;
}
else
{
smh=height;
}
//CHECKING NUMBER OF PIXELS SIMILARITY
for(int a=0;a<smw;a++)
{
for(int b=0;b<smh;b++)
{
clre[a][b]=images.getRGB(a,b);
clr[a][b]=image.getRGB(a,b);
if(clr[a][b]==clre[a][b])
{
p=p+1;
bw.write("\t");
bw.write(Integer.toString(a));
bw.write("\t");
bw.write(Integer.toString(b));
bw.write("\n");
}
else
q=q+1;
}
}
float w,h=0;
if(width>widthe)
{
w=width;
}
else
{
w=widthe;
}
if(height>heighte)
{
h = height;
}
else
{
h = heighte;
}
float s = (smw*smh);
//CALUCLATING PERCENTAGE
float x =(100*p)/s;
System.out.println("THE PERCENTAGE SIMILARITY IS APPROXIMATELY ="+x+"%");
long stop = System.currentTimeMillis();
System.out.println("TIME TAKEN IS ="+(stop-start));
System.out.println("NO OF PIXEL GETS VARIED:="+q);
System.out.println("NO OF PIXEL GETS MATCHED:="+p);
}
}
回答by PeterMmm
Right now your code will compare always the file 2000.png with 2002.png. First refactor your comparison code into a method:
现在您的代码将始终将文件 2000.png 与 2002.png 进行比较。首先将您的比较代码重构为一个方法:
CompareResult compare(BufferedImage img1,BufferedImage img2) { ... }
Declare a class CompareResult to group the result info into one Message:
声明一个类 CompareResult 将结果信息分组为一个消息:
class CompareResult {
long pixMatched;
long pixVar;
...
}
Then expect a list of image files from commandline and compre them each to each other:
然后期待来自命令行的图像文件列表并将它们相互比较:
for(int i=0;i<args.length();i++) {
BufferedImage img1 = ImageIO.read(args[i]);
for (int j=i+1;j<args.length();j++) {
BufferedImage img2 = ImageIO.read(args[j]);
CompareResult r = compare(img1,img2);
printInfo(r);
}
}
回答by Diego Catalano
You can do it using Catalano Framework. There's several metrics to compare image, including mean square error.
您可以使用Catalano Framework来做到这一点。有几个指标可以比较图像,包括均方误差。
Example:
例子:
FastBitmap original = new FastBitmap(bufferedImage1);
FastBitmap reconstructed = new FastBitmap(bufferedImage2);
ObjectiveFidelity o = new ObjectiveFidelity(original, reconstructed);
// Error total
int error = o.getTotalError();
//Mean Square Error
double mse = o.getMSE();
//Signal Noise Ratio
double snr = o.getSNR();
//Peak Signal Noise Ratio
double psnr = o.getPSNR();
All these metrics are based in the book: Computer Imaging: Digital Image Analysis and Processing - Scott E Umbaugh.
所有这些指标都基于这本书:计算机成像:数字图像分析和处理 - Scott E Umbaugh。
Next version (1.3) will contains Derivative SNR.
下一个版本 (1.3) 将包含Derivative SNR。
回答by Tamilan C.Periyasamy
File file1 = new File(args[0]);
File file2 = new File(args[1]);
boolean compareResult = FileUtils.contentEquals(file1, file2);
System.out.println("Are the files are same? " + compareResult);
Apache Commons IO library to perform this comparison(download commons-io-2.4.jar)
Apache Commons IO 库执行此比较(下载 commons-io-2.4.jar)