图片比较代码工作不正常

rqqzpn5f  于 2022-09-17  发布在  Java
关注(0)|答案(1)|浏览(244)

我仍然是编程的初学者,并且面临着Java代码的问题。我将非常感谢您的建议和意见。问题如下:
我在网上找到了一个Java代码,用于比较两张图片并突出它们之间的差异。然而,代码的工作方式是使用一个循环来比较两张图片的每个像素的RGB值,如果值相等,则以特定颜色高亮显示。然而,这导致了这样一个问题,即即使两张图片之间的相机Angular 或照明条件发生最轻微的变化,也会导致像素的RGB值不同,使得代码将它们突出显示为差异,即使图片基本相同。代码写在下面,我添加了链接到照片,显示代码结果的例子。您建议我在代码中更改什么?提前感谢您的帮助。
代码:

import java.awt.image.BufferedImage; 
    import java.io.File; 
    import java.io.IOException;   
    import javax.imageio.ImageIO;  
public class PictureOverlayTest { 
    /* 
     * Four variables, three for the wanted BufferedImages, one String for the 
     * Path of the third Image, which does not already exist. 
     */   
    private BufferedImage image1; 
    private BufferedImage image2; 
    private BufferedImage image3;   
    private String pathImage3;   
    public PictureOverlayTest(String filePathAndName1, String filePathAndName2, 
            String filePathAndName3) throws IOException { 
        /* 
         * Constructor in order to keep this method reusable and clean. Needs 
         * three Strings. The paths and Filenames of all three images. Image 1 
         * and 2 should exist already, Image 3 will be created if all 
         * requirements are met. Constructor creates the first two buffered 
         * images, sets all needed variables and starts the checkAndCompare() 
         * method 
         */   
        File file = new File(filePathAndName1); 
        this.image1 = ImageIO.read(file);   
        file = new File(filePathAndName2); 
        this.image2 = ImageIO.read(file);   
        this.pathImage3 = filePathAndName3; 
        checkAndCompare(); 
    }   
    private void checkAndCompare() throws IOException { 
        /* 
         * This function creates the Color blue, compares the sizes of both 
         * pictures and if they are the same, creates a third image. Then it 
         * loops through the two images and compares each pixel. If the pixels 
         * are the same, the third image gets a blue pixel at that point 
         */   
        Color blue = Color.blue; 
        Color yellow = Color.yellow;   
        if (image1.getHeight() == image2.getHeight() 
                && image1.getWidth() == image2.getWidth()) {   
            image3 = new BufferedImage(image1.getWidth(), image1.getHeight(), 
                    image1.getType()); 
            for (int y = 0; y < image1.getHeight(); y++) { 
                for (int x = 0; x < image1.getWidth(); x++) {   
                    int colorImage1 = image1.getRGB(x, y); 
                    int colorImage2 = image2.getRGB(x, y);   
                    if (colorImage1 == colorImage2) {   
                        image3.setRGB(x, y, blue.getRGB());   
                    } else { 
                              image3.setRGB(x, y, yellow.getRGB());  
                        // Whatever Color you want. By default it is black.   
                    }   
                } 
            } 
            savePicture3(); 
            System.out.println("Message: Image comparison is done");   
        } else {   
            System.out.println("Error: Image dimensions do not match");   
        }   
    }   
    private void savePicture3() throws IOException { 
        /* 
         * This method saves the created Image into a file onto your computer. 
         * The if() statement is used to check if the file was successfully 
         * created, in order to avoid unwanted errors. Keep in mind, that you 
         * have to change the "bmp" in ImageIO.write() to whatever format you 
         * actually want 
         */   
        File file = new File(pathImage3); 
        if (file.createNewFile()) { 
            ImageIO.write(image3, "bmp", file); 
        } 
    }   
}   
import java.io.IOException;   
public class Main {   
    public static void main(String[] args) { 
        // TODO Auto-generated method stub   
        try { 
            PictureOverlayTest test = new PictureOverlayTest( 
                    "C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test1.png", 
                    "C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test2.png", 
                    "C:\\Users\\Rabee Taha\\Desktop\\Java Test Pics\\test3.png"); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    }   
}  

以下是链接到结果的示例图像(https://postimg.cc/gallery/rkXfPr7 )

kiz8lqtg

kiz8lqtg1#

我仍然是编程的初学者,并且面临着Java代码的问题。我将非常感谢您的建议和意见。问题如下:
我在网上找到了一个Java代码,用于比较两张图片并突出它们之间的差异。然而,代码的工作方式是使用一个循环来比较两张图片的每个像素的RGB值,如果值相等,则以特定颜色高亮显示。然而,这导致了这样一个问题,即即使两张图片之间的相机Angular 或照明条件发生最轻微的变化,也会导致像素的RGB值不同,使得代码将它们突出显示为差异,即使图片基本相同。代码写在下面,我添加了链接到照片,显示代码结果的例子。您建议我在代码中更改什么?提前感谢您的帮助。
您可以查看机器学习/人工智能库。我相信有一些与图像处理相关的东西可以达到这个目的。

相关问题