javascript - 两幅图像的图像比较

标签 javascript java image image-processing

我想比较两个图像文件的某些相等标准。

是否有任何工具可以将两个图像文件进行一定百分比的比较? 如果我们传递 2 个图像,它应该能够知道图像匹配大约 'x'%

最佳答案

这可能不完全是您正在寻找的内容,但实际上编写图像处理和分析的代码可能非常有用。这是 Java 语言:

//Image percentage match
public float getPercentMatch(String imageFile1, String imageFile2) {

//Set how close a pixel has to be for a match, pixel deviation
int pd = 3;

  try {
     //get height, width and image from first file
     File input1 = new File(imageFile1);
     image1 = ImageIO.read(input1);
     width1 = image1.getWidth();
     height1 = image1.getHeight();

     //Do the same for the second one
     File input2 = new File(imageFile2);
     image2 = ImageIO.read(input2);
     width2 = image2.getWidth();
     height2 = image2.getHeight();

     int matchCount = 0;

     //Make sure they're the same height.  You could also choose to use the 
     //smaller picture for dimensions
     if (height1 != height2 || width1 != width2) {return 0.0;}


     //Iterate over each pixel
     for(int i=0; i<height1; i++){

        for(int j=0; j<width1; j++){

           //Get RGB values for each image at certain pixel
           Color c1 = new Color(image1.getRGB(j, i));
           Color c2 = new Color(image2.getRGB(j, i));

           // If the colors are close enough based on deviation value...
           if (   (Math.abs(c1.getRed() - c2.getRed()) < pd ) &&
                  (Math.abs(c1.getGreen() - c2.getGreen()) < pd ) &&
                  (Math.abs(c1.getBlue() - c2.getBlue() ) < pd ) ) {
               //...it's a match, so increment the match count
               matchCount++;
           }
        }
     }
     return matchCount / (height1 * width1);

  } catch (Exception e) {}
}

关于javascript - 两幅图像的图像比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38076685/

相关文章:

javascript - 如果我们点击页面中的任何地方,值就会更新

javascript - 无需刷新页面即可更新翻译(i18next 带有 React)

javascript - 销售人员 : JavaScript Remoting "Uncaught ReferenceError: Visualforce is not defined"

java - 如何在 while 循环中分配不同的名称?

iphone - 将 UIImage 放入 UIButton 的简单方法

android - 如何将复选框绑定(bind)到图像

javascript - 如何替换为递归函数?

java - Web 服务 java JAX-RS 中的多个参数

java - Java 的 Thread.sleep 在这里有什么用处?

css - 如何将js时钟对准页面中心?