📜  Java中的图像处理——两个图像的比较

📅  最后修改于: 2022-05-13 01:55:03.618000             🧑  作者: Mango

Java中的图像处理——两个图像的比较

先决条件:

  • Java中的图像处理——读写
  • Java中的图像处理——获取和设置像素
  • Java中的图像处理——彩色图像到灰度图像的转换
  • Java中的图像处理——彩色图像到负图像的转换
  • Java中的图像处理——彩色到红绿蓝图像的转换
  • Java中的图像处理——彩色图像到棕褐色图像的转换
  • Java中的图像处理——创建随机像素图像
  • Java中的图像处理——创建镜像
  • Java中的图像处理——人脸检测
  • Java中的图像处理——给图像加水印
  • Java中的图像处理——改变图像的方向
  • Java中的图像处理——对比度增强
  • Java中的图像处理——亮度增强

算法:

  1. 检查两个图像的尺寸是否匹配。
  2. 获取两个图像的 RGB 值。
  3. 计算三个颜色分量的两个对应像素的差值。
  4. 对图像的每个像素重复步骤 2-3。
  5. 最后,通过将差异总和除以像素数来计算百分比。

实施:我们在下面展示了输入图像和输出图像,以感知比较并说明它们之间的差异。

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);
        }
    }
}



输出:
用例 1:输入图像

用例 2:输入图像

用例 3:输入图像