Open In App

Image Processing in Java – Comparison of Two Images

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisites:

Note: Image must be of the same dimension 

Algorithm:

  1. Check if the dimensions of both images match.
  2. Get the RGB values of both images.
  3. Calculate the difference in two corresponding pixels of three color components.
  4. Repeat Steps 2-3 for each pixel of the images.
  5. 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

 

 



Last Updated : 31 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads