简体   繁体   English

在Java中修改和比较2张图片

[英]Modify and Compare 2 images in Java

I have got an assignment where I need to validate images. 我有一个需要验证图像的任务。 I have got 2 sets of folders one which is actual and other folder contain expected images. 我有2套文件夹,其中一组是实际的,其他文件夹包含预期的图像。 These images are of some brands/Companies. 这些图像是某些品牌/公司的。

Upon initial investigation, I found that images of each brand have different dimension but are of same format ie png 经过初步调查,我发现每个品牌的图片都有不同的尺寸,但格式相同,即png

What I have done so far:- Upon googling I found the below code which compares 2 images. 到目前为止,我已经完成了什么:-在谷歌搜索后,我发现了下面的代码,该代码比较了2张图片。 I ran this code for one of the brand and ofcourse the result was false. 我为其中一个品牌运行了此代码,结果当然是错误的。 Then I modify one of the image such that both the images have same dimension.. even then i got the same result. 然后,我修改图像之一,使两个图像都具有相同的尺寸。即使我得到的结果也相同。

public void testImage() throws InterruptedException{
        String file1="D:\\image\\bliss_url_2.png";
        String file2="D:\\bliss.png";
        Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
        Image image2 = Toolkit.getDefaultToolkit().getImage(file2);

        PixelGrabber grab1 =new PixelGrabber(image1, 0, 0, -1, -1, true);
        PixelGrabber grab2 =new PixelGrabber(image2, 0, 0, -1, -1, true);



        int[] data1 = null;

        if (grab1.grabPixels()) {
        int width = grab1.getWidth();
        int height = grab1.getHeight();
        System.out.println("Initial width and height of Image 1:: "+width + ">>"+ height);
        grab2.setDimensions(250, 100);
        System.out.println("width and height of Image 1:: "+width + ">>"+ height);
        data1 = new int[width * height];
        data1 = (int[]) grab1.getPixels();
        System.out.println("Image 1:: "+ data1);
        }

        int[] data2 = null;

        if (grab2.grabPixels()) {
        int width = grab2.getWidth();
        int height = grab2.getHeight();
        System.out.println("width and height of Image 2:: "+width + ">>"+ height);
        data2 = new int[width * height];
        data2 = (int[]) grab2.getPixels();
        System.out.println("Image 2:: "+ data2.toString());
        }

        System.out.println("Pixels equal: " + java.util.Arrays.equals(data1, data2));
    }

I just want to verify if the content of images are same ie images belong to same brand ,if not then what are the differences 我只想验证图片的内容是否相同,即图片属于同一品牌,如果不是,那么有什么区别

Please help me what should I do to do valid comparison. 请帮助我该怎么做才能进行有效比较。

Maybe you should not use some external library assuming it should be your own work. 也许您不应该使用某些外部库来假设它应该是您自己的工作。 In this point of view, a way to compare images is to get the average color of the same portion of both images. 从这个角度来看,比较图像的一种方法是获取两个图像相同部分的平均颜色。 If results are equals (or very similar due to compression errors etc) 如果结果相等(或由于压缩错误等而非常相似)

Lets say we have two images image 1 is 4 pixel. 假设我们有两个图像,图像1是4像素。 (to simplify each pixel is represented with a number but should be RGB) (为简化起见,每个像素均用数字表示,但应为RGB)

1 2
3 4

[ (1+2+3+4) / 4 = 2.5 ] [(1 + 2 + 3 + 4)/ 4 = 2.5]

image 2 is twice bigger 图片2的两倍大

1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4

[ ((4*1)+(4*2)+(4*3)+(4*4)) / 16 = 2.5] [(((4 * 1)+(4 * 2)+(4 * 3)+(4 * 4))/ 16 = 2.5]

The average pixel value (color) is 2.5 in both images. 在两个图像中,平均像素值(颜色)均为2.5。 (with real pixel colors, compare the RGB colors separately. The three should be equal or very close) (对于真实像素颜色,请分别比较RGB颜色。这三种颜色应该相等或非常接近)

That's the idea. 那是主意。 Now, you should make this caumputing for each pixel of the smallest image and the corresponding pixels of the bigest one (according to the scale difference of both images) 现在,您应该对最小图像的每个像素和最大图像的相应像素进行计算(根据两个图像的比例差异)

Hope you'll find out a good solution ! 希望您能找到一个好的解决方案!

  1. Method setDimensions doesn't scale the image. setDimensions方法不会缩放图像。 Moreover, you shouldn't call it directly (see its java-doc). 此外,您不应该直接调用它(请参阅其java-doc)。 PixelGrabber is just a grabber to grab a subset of the pixels in an image. PixelGrabber只是一个抓取器,用于抓取图像中一部分像素。 To scale the image use Image.getScaledInstance() http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance(int,%20int,%20int) for instance 要缩放图像,请使用Image.getScaledInstance() http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance (int,%20int,%20int 例如

  2. Even if you have got 2 images with the same size after scaling, you still cannot compare them pixel by pixel, since any algorithm of scaling is lossy by its nature. 即使缩放后有2张具有相同大小的图像,您仍然无法逐像素比较它们,因为任何缩放算法本质上都是有损的。 That means the only thing you can do is to check "similarity" of the images. 这意味着您唯一可以做的就是检查图像的“相似性”。 I'd suggest to take a look at a great image processing library OpenCV which has a wrapper for Java: 我建议看看一个很棒的图像处理库OpenCV,它具有Java的包装器:

Simple and fast method to compare images for similarity 简单快速地比较图像的相似性

http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM