Tutorialspoint.dev

Image Processing in Java | Set 14 ( Comparison of two images )

We strongly recommend to refer below posts as a prerequisite of this.

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter