有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用基于均方误差的基于像素的图像比较度量来比较java中的图像集?

在我的项目中,我有一组图像。我需要比较一下。将一幅图像中的每个像素与数据集中所有其他图像中相同位置的像素进行比较。在对图像空间中的所有像素应用均方误差计算之后,识别一组不同的像素,其表示图像中具有不同颜色值的像素。 我在一个文件中比较并存储了两幅图像的相似性像素。但不能对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);
  }
}

共 (3) 个答案

  1. # 1 楼答案

    你可以用Catalano Framework来做。有几个指标可以比较图像,包括均方误差

    例如:

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

    所有这些指标都基于《计算机成像:数字图像分析与处理——斯科特·E·乌博》一书

    下一个版本(1.3)将包含Derivative SNR

  2. # 2 楼答案

      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库(下载Commons-IO-2.4.jar)

  3. # 3 楼答案

    现在,您的代码将始终与文件2000进行比较。巴布亚新几内亚与2002年。巴布亚新几内亚。 首先,将比较代码重构为一个方法:

    CompareResult compare(BufferedImage  img1,BufferedImage  img2) { ... }
    

    声明一个类CompareResult,将结果信息分组为一条消息:

    class CompareResult {
         long pixMatched;
         long pixVar;
         ...
    }
    

    然后从命令行中获得一个图像文件列表,并相互压缩:

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