java 如何在Java中比较两个图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37598977/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:38:57  来源:igfitidea点击:

How to compare two images in Java

javaimage

提问by Pawan

I like to compare two images. imagemagic tool does this for me using cmd prompt. it compares two images (for e.g. a button's position is different) and output the result image in new gif image which highlights the difference. However i want some tool which generate the new image like this however only if there is a difference. Please suggest how can i do it using any tool or even if this is possible using selenium with java. imagemagic generate the new result image even if there is no difference between them.

我喜欢比较两个图像。imagemagic 工具使用 cmd 提示符为我执行此操作。它比较两个图像(例如按钮的位置不同)并以新的 gif 图像输出结果图像,突出显示差异。但是,我想要一些工具来生成这样的新图像,但是只有在存在差异的情况下。请建议我如何使用任何工具来做到这一点,或者即使使用 selenium 和 java 也可以做到这一点。即使它们之间没有区别,imagemagic 也会生成新的结果图像。

edited: I have done RnD and concluded that the imagemagic + iam4java can be used through selenium to compare the images but still cannot find how to put condition of generating the output image only if there is a difference

编辑:我已经完成了RnD并得出结论,可以通过selenium使用imagemagic + iam4java来比较图像,但仍然无法找到如何放置生成输出图像的条件只有在存在差异时

回答by Tiago Bértolo

Using java to compare 2 images:

使用java比较2张图片:

    BufferedImage imgA = ImageIO.read(new File("C:/img/picA.jpg")); 
    BufferedImage imgB = ImageIO.read(new File("C:/img/picB.jpg"));   

    boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
     for (int x = 0; x < img1.getWidth(); x++) {
      for (int y = 0; y < img1.getHeight(); y++) {
       if (img1.getRGB(x, y) != img2.getRGB(x, y))
        return false;
       }
      }
     } else {
        return false;
     }
     return true;
    }

To produce the difference image you can do something like this:

要生成差异图像,您可以执行以下操作:

    private static void subtractImages(BufferedImage image1, BufferedImage image2) throws IOException {
    BufferedImage image3 = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
    int color;
    for(int x = 0; x < image1.getWidth(); x++)
        for(int y = 0; y < image1.getHeight(); y++) {
            color = Math.abs(image2.getRGB(x, y) - image1.getRGB(x, y));                
            image3.setRGB(x, y, color);
        }
    ImageIO.write(image3, "bmp",  new File("image.bmp"));
 }

Source of subtractImages method

subtractImages 方法的来源

Source of bufferedImagesEqual method

bufferedImagesEqual 方法的来源

Gist with a working example

带有工作示例的要点

回答by Anna Tsibulskaya

You can try to use applitools ( http://applitools.com/) for image comparison testing. It saves images on its server and print link to the image in log only in case of faillure. So if your test passes (images are the same) - you'll just see a green test. If there is a difference between images - test will fail and you'll see in the log the link to applitools where you'll get your image with highlighted areas of differences and will be able to see baseline image(to compare with your own eyes:)).

您可以尝试使用 applitools ( http://applitools.com/) 进行图像比较测试。它将图像保存在其服务器上,并仅在出现故障时才将图像链接打印到日志中。因此,如果您的测试通过(图像相同)- 您只会看到绿色测试。如果图像之间存在差异 - 测试将失败,您将在日志中看到指向 applitools 的链接,您将在其中获得带有突出显示差异区域的图像,并且能够看到基线图像(与您自己的眼睛进行比较) :))。

回答by parthiv ponkiya

Here is what I have done... Open image and store Images as File Java IO

这是我所做的...打开图像并将图像存储为文件 Java IO

NewImage= new File(NewImagePath) //gets newImage from path NewImagePath
Original= new File(OriginalPath) // gets Original from pathOriginalPath

Created a function "getImagePercentage", that compares both the image and retruns machines %percetage, 100% if both images are same else any other value.

创建了一个函数“getImagePercentage”,它比较图像和返回机器 %percetage,如果两个图像相同则为 100%,否则为任何其他值。

Call this method as : getImagePercentage(Original,NewImage);

将此方法调用为: getImagePercentage(Original,NewImage);

    getImagePercentage(File fileA, File fileB) { // start of the function getImagePercentage

    def percentage = 0;
    BufferedImage biA = ImageIO.read(fileA); // reads fileA into bufferedImage
    DataBuffer dbA = biA.getData().getDataBuffer();
    int sizeA = dbA.getSize();
    BufferedImage biB = ImageIO.read(fileB); // reads fileA into bufferedImage
    DataBuffer dbB = biB.getData().getDataBuffer();
    int sizeB = dbB.getSize();
    int count = 0;
    // compare data-buffer objects //
    if (sizeA == sizeB) { // checks the size of the both the bufferedImage

        for (int i = 0; i < sizeA; i++) {

            if (dbA.getElem(i) == dbB.getElem(i)) { // checks bufferedImage array is same in both the image
                count = count + 1;
            }
        }
        percentage = (count * 100) / sizeA; // calculates matching percentage
    } else {
        System.out.println("Both the images are not of same size");
    }
    return percentage; // returns the matching percentage value
}

回答by kiran

String applicationloader = TenantLoader_lnk.getAttribute("src");

String applicationloader = TenantLoader_lnk.getAttribute("src");

        BufferedImage expected = ImageIO.read(new File("local image file path"));
        BufferedImage actual = ImageIO.read(new URL(applicationloader));

        if(actual.getWidth()==expected.getWidth() && actual.getHeight()==expected.getHeight());
        {
            for(int x=0;x<expected.getWidth();x++)
            {
                for(int y=0;y<expected.getHeight();y++)
                {
                    assertTrue(actual.getRGB(x, y)==expected.getRGB(x, y), "Image is not same hence failing");
                }
            }
        }