Prerequisites:
- Image Processing in Java – Read and Write
- Image Processing In Java – Get and Set Pixels
- Image Processing in Java – Colored Image to Grayscale Image Conversion
- Image Processing in Java – Colored Image to Negative Image Conversion
- Image Processing in Java – Colored to Red Green Blue Image Conversion
- Image Processing in Java – Colored Image to Sepia Image Conversion
- Image Processing in Java – Creating a Random Pixel Image
- Image Processing in Java – Creating a Mirror Image
- Image Processing in Java – Face Detection
- Image Processing in Java – Watermarking an Image
- Image Processing in Java – Changing Orientation of Image
- Image Processing in Java – Contrast Enhancement
- Image Processing in Java – Brightness Enhancement
Note: 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
This article is contributed by Pratik Agarwal. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar 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.