Image Processing in Java - Comparison of Two Images Last Updated : 31 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image ConversionImage Processing in Java - Colored Image to Sepia Image ConversionImage Processing in Java - Creating a Random Pixel ImageImage Processing in Java - Creating a Mirror ImageImage Processing in Java - Face DetectionImage Processing in Java - Watermarking an ImageImage Processing in Java – Changing Orientation of ImageImage Processing in Java - Contrast EnhancementImage Processing in Java - Brightness EnhancementNote: Image must be of the same dimension Algorithm: Check if the dimensions of both images match.Get the RGB values of both images.Calculate the difference in two corresponding pixels of three color components.Repeat Steps 2-3 for each pixel of the images.Lastly, calculate the percentage by dividing the sum of differences by the number of pixels.In order 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: We have showcased input images below alongside output to perceive comparison and illustrate differences between them. Java // Java Program to Compare Two Images Using OpenCV // Via printing Difference Percentage // Importing required classes import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; // Main class // ImageComparison class GFG { // Main driver method public static void main(String[] args) { // Initially assigning null BufferedImage imgA = null; BufferedImage imgB = null; // Try block to check for exception try { // Reading file from local directory by // creating object of File class File fileA = new File("/home / pratik /" + " Desktop / image1.jpg"); File fileB = new File("/home / pratik /" + " Desktop / image2.jpg"); // Reading files imgA = ImageIO.read(fileA); imgB = ImageIO.read(fileB); } // Catch block to check for exceptions catch (IOException e) { // Display the exceptions on console System.out.println(e); } // Assigning dimensions to image int width1 = imgA.getWidth(); int width2 = imgB.getWidth(); int height1 = imgA.getHeight(); int height2 = imgB.getHeight(); // Checking whether the images are of same size or // not if ((width1 != width2) || (height1 != height2)) // Display message straightaway System.out.println("Error: Images dimensions" + " mismatch"); else { // By now, images are of same size long difference = 0; // treating images likely 2D matrix // Outer loop for rows(height) for (int y = 0; y < height1; y++) { // Inner loop for columns(width) 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 // Note: 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; // Lastly print the difference percentage System.out.println("Difference Percentage-->" + percentage); } } } Output:Use Case 1: Input Images Output: Difference Percentage-->2.843600130405922 Use Case 2: Input Images Output: Difference Percentage-->6.471412648669786 Use Case 3: Input Images Output : Difference Percentage-->0.0 Comment More infoAdvertise with us Next Article Image Processing in Java - Creating a Mirror Image K kartik Improve Article Tags : Java Image-Processing Practice Tags : Java Similar Reads Image Processing in Java - Creating a Mirror Image Prerequisite: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C 3 min read Image Processing in Java - Watermarking an Image Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C 3 min read Image Processing in Java - Creating a Random Pixel Image Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C 2 min read Image Processing in Java - Face Detection Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image C 3 min read Image Processing In Java - Get and Set Pixels Prerequisite - Image Processing in Java - Read and Write In this set, we will learn about the pixels of images, how we can get pixel values of an image and how to set pixel values in an image using Java programming language. Pixels are the smallest unit of an image which consists of four components 3 min read Image Processing in Java - Colored image to Negative Image Conversion Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored image to Grayscale Image Conversion In this set, we will be converting a colored image to a negative image. Colored Image (RGB Color Model) - The RGB color model i 3 min read Like