We strongly recommend to refer below posts as a prerequisite of this.
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing In Java | Set 2 (Get and set Pixels)
In this article we will learn how to compare two given images (must be of same dimensions) and print the percentage of difference between them.
Algorithm:
Step 1 – Check if dimensions of both the image match.
Step 2 – Get the RGB values of both images.
Step 3 – Calculate the difference in two corresponding pixel of three color components.
Step 4 – Repeat Step 2-3 for each pixel of the images.
Step 5 – Calculate the percentage by dividing the sum of differences with:
- Number of pixels, to obtain the average difference per pixel
- 3, to obtain the average difference per color component
- 255, to obtain a value between 0.0 and 1.0 which can be converted into a percent value
Implementation:
// Java Program to compare two images import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; import java.io.IOException; class ImageComparision { public static void main(String[] args) { BufferedImage imgA = null; BufferedImage imgB = null; try { File fileA = new File("/home / pratik /"+ " Desktop / image1.jpg"); File fileB = new File("/home / pratik /"+ " Desktop / image2.jpg"); imgA = ImageIO.read(fileA); imgB = ImageIO.read(fileB); } catch (IOException e) { System.out.println(e); } int width1 = imgA.getWidth(); int width2 = imgB.getWidth(); int height1 = imgA.getHeight(); int height2 = imgB.getHeight(); if ((width1 != width2) || (height1 != height2)) System.out.println("Error: Images dimensions"+ " mismatch"); else { long difference = 0; for (int y = 0; y < height1; y++) { for (int x = 0; x < width1; x++) { int rgbA = imgA.getRGB(x, y); int rgbB = imgB.getRGB(x, y); int redA = (rgbA >> 16) & 0xff; int greenA = (rgbA >> 8) & 0xff; int blueA = (rgbA) & 0xff; int redB = (rgbB >> 16) & 0xff; int greenB = (rgbB >> 8) & 0xff; int blueB = (rgbB) & 0xff; difference += Math.abs(redA - redB); difference += Math.abs(greenA - greenB); difference += Math.abs(blueA - blueB); } } // Total number of red pixels = width * height // Total number of blue pixels = width * height // Total number of green pixels = width * height // So total number of pixels = width * height * 3 double total_pixels = width1 * height1 * 3; // Normalizing the value of different pixels // for accuracy(average pixels per color // component) double avg_different_pixels = difference / total_pixels; // There are 255 values of pixels in total double percentage = (avg_different_pixels / 255) * 100; System.out.println("Difference Percentage-->" + percentage); } } } |
NOTE:Code will not run on online ide since it requires image in drive.
Output:
Input images:


Output : Difference Percentage–>2.843600130405922
Input images:


Output : Difference Percentage–>6.471412648669786
Input images:


Output : Difference Percentage–>0.0
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 7 (Creating a random pixel image)
- Image Processing in Java | Set 8 (Creating mirror image)
- Image Processing in Java | Set 11 (Changing orientation of image)
- Image Processing in Java | Set 10 ( Watermarking an image )
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Image Edge Detection Operators in Digital Image Processing
- Image processing with Scikit-image in Python
- Image Processing In Java | Set 2 (Get and set Pixels)
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing in Java | Set 9 ( Face Detection )
- Image Processing in Java | Set 12 ( Contrast Enhancement )
- Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)
- Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )
- Python | Morphological Operations in Image Processing (Opening) | Set-1
- Python | Morphological Operations in Image Processing (Closing) | Set-2
- Python | Morphological Operations in Image Processing (Gradient) | Set-3

