Home > Java > javaTutorial > How to compare two images using Java OpenCV library?

How to compare two images using Java OpenCV library?

王林
Release: 2023-08-28 16:25:01
forward
1162 people have browsed it

Compare two images -

  • Read them using the Image.IO.read() method.

  • Get the height and width of both, making sure they are equal.

  • Get the pixel value and get the RGB value of the two images.

  • Get the sum of the differences between the RGB values ​​of these two images.

    li>
  • Use the following formula to calculate the percentage of difference -

Average = difference/weight*height*3;
Percentage = (Average/255)*100;
Copy after login

Example

import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class ComparingImages {
   public static void main(String[] args) throws Exception {
      BufferedImage img1 = ImageIO.read(new File("D:\Images\test1.jpg"));
      BufferedImage img2 = ImageIO.read(new File("D:\Images\test2.jpg"));
      int w1 = img1.getWidth();
      int w2 = img2.getWidth();
      int h1 = img1.getHeight();
      int h2 = img2.getHeight();
      if ((w1!=w2)||(h1!=h2)) {
         System.out.println("Both images should have same dimwnsions");
      } else {
         long diff = 0;
         for (int j = 0; j < h1; j++) {
            for (int i = 0; i < w1; i++) {
               //Getting the RGB values of a pixel
               int pixel1 = img1.getRGB(i, j);
               Color color1 = new Color(pixel1, true);
               int r1 = color1.getRed();
               int g1 = color1.getGreen();
               int b1 = color1.getBlue();
               int pixel2 = img2.getRGB(i, j);
               Color color2 = new Color(pixel2, true);
               int r2 = color2.getRed();
               int g2 = color2.getGreen();
               int b2= color2.getBlue();
               //sum of differences of RGB values of the two images
               long data = Math.abs(r1-r2)+Math.abs(g1-g2)+ Math.abs(b1-b2);
               diff = diff+data;
            }
         }
         double avg = diff/(w1*h1*3);
         double percentage = (avg/255)*100;
         System.out.println("Difference: "+percentage);
      }
   }
}
Copy after login

Input 1

如何使用Java OpenCV库比较两个图像?

Input 2

如何使用Java OpenCV库比较两个图像?

Output

Difference: 92.54901960784314
Copy after login

The above is the detailed content of How to compare two images using Java OpenCV library?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template