Sunny Sachdeva
Sunny Sachdeva

Reputation: 289

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. 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 i.e png

What I have done so far:- Upon googling I found the below code which compares 2 images. 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 i.e images belong to same brand ,if not then what are the differences

Please help me what should I do to do valid comparison.

Upvotes: 0

Views: 1962

Answers (2)

jvDx
jvDx

Reputation: 41

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. (to simplify each pixel is represented with a number but should be RGB)

1 2
3 4

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

image 2 is twice bigger

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]

The average pixel value (color) is 2.5 in both images. (with real pixel colors, compare the RGB colors separately. The three should be equal or very close)

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 !

Upvotes: 1

AnatolyG
AnatolyG

Reputation: 1587

  1. Method setDimensions doesn't scale the image. Moreover, you shouldn't call it directly (see its java-doc). PixelGrabber is just a grabber to grab a subset of the pixels in an image. 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

  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. 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:

Simple and fast method to compare images for similarity

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

Upvotes: 0

Related Questions