Question

i am trying to get difference between two images ( same size ) i found this code :

            BufferedImage img1 = null;
            BufferedImage img2 = null;

            try{            
                URL url1 = new URL("http://rosettacode.org/mw/images/3/3c/Lenna50.jpg");
                URL url2 = new URL("http://rosettacode.org/mw/images/b/b6/Lenna100.jpg");
                img1 = ImageIO.read(url1);
                img2 = ImageIO.read(url2);
             } catch (IOException e) {
                e.printStackTrace();
             }
             int width1 = img1.getWidth(null);
             int width2 = img2.getWidth(null);
             int height1 = img1.getHeight(null);
             int height2 = img2.getHeight(null);

             if ((width1 != width2) || (height1 != height2)) {
                 System.err.println("Error: Images dimensions mismatch");
                 System.exit(1);
             }

             long diff = 0;
             for (int i = 0; i < height1; i++) {
                 for (int j = 0; j < width1; j++) {
                     int rgb1 = img1.getRGB(i, j);
                     int rgb2 = img2.getRGB(i, j);
                     int r1 = (rgb1 >> 16) & 0xff;
                     int g1 = (rgb1 >>  8) & 0xff;
                     int b1 = (rgb1      ) & 0xff;
                     int r2 = (rgb2 >> 16) & 0xff;
                     int g2 = (rgb2 >>  8) & 0xff;
                     int b2 = (rgb2      ) & 0xff;
                     diff += Math.abs(r1 - r2);
                     diff += Math.abs(g1 - g2);
                     diff += Math.abs(b1 - b2);
                 }
             }
             double n = width1 * height1 * 3;
             double p = diff / n / 255.0;
             System.out.println("diff percent: " + (p * 100.0));    `

it works fine for the two images given in the url , but when i changed the images given i get this exception :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:299)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
at Main.main(Main.java:77)

I changed the code to :

          File sourceimage1 = new File("C:\\lo.jpg");
          File sourceimage2 = new File("C:\\lo1.jpg");
          img1 = ImageIO.read(sourceimage1);
          img2 = ImageIO.read(sourceimage2);

the two images are black and white , and their dimensions are smaller than the two previous images (lenna50 and lenna100) the lo.jpg and lo1.jpg are the same image to test the algorithm also they are in black and white how can I change the code to make it work for any image dimension ?

Was it helpful?

Solution

Toggle the i and j

in the following code as I have done below:

int rgb1 = img1.getRGB(j, i);
int rgb2 = img2.getRGB(j, i);

OTHER TIPS

Your error clearly says that while reading rgb point as code in line img1.getRGB(i, j); it is going out of Array for image RGB. Check the values of i & j inside your inner for loop and check whether something you are doing wrong. As already Hirak pointed may be that you are not initializing your variables properly and so the reason it is going out of height or width.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top