如何在图像上表示差异?

问题描述 投票:-2回答:1

我有代码来找到两个图像之间的差异。找到差异后,我不知道如何用任何直肠盒代表不同部分。

这是我的代码,以找到差异。

package compareImages;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageComparision {
    public static void main(String[] args) {
        BufferedImage imgA = null;
        BufferedImage imgB = null;

        try {
            File fileA = new File("E:\\Career\\A.jpg");
            File fileB = new File("E:\\Career\\B.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 {
            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;

                    if ((redA == redB) || (greenA == greenB)|| (blueA == blueB)) {
                        System.out.println("Images are identical. no difference b.w two images..");
                    } else {
                        System.out.println("how to draw a rectangular box over the difference?????????");
                    }
                }
            }
        }
    }
}

例如:图像A在位置(50,50)有一个单选按钮,图像B在相同位置有复选框。现在,我必须用2D矩形框表示图像B上的复选框。请给我一些想法来实现这一目标。

java image difference representation
1个回答
0
投票

在第一个for循环之前创建第三个BufferedImage。在循环中,设置新图像的像素,例如黑色为“有差异”,白色为“没有差异”。你也应该使用&&而不是||

像这样的代码:

BufferedImage result = new BufferedImage(width1, height1, BufferedImage.TYPE_INT_ARGB);

if ((redA == redB) && (greenA == greenB) && (blueA == blueB)) {
    result.setRGB(Color.WHITE.getRGB());
} else {
    result.setRGB(Color.BLACK.getRGB());
}

另一个想法是绘制一个假彩色图像,用于给定imgaes的像素颜色之间的数值差异:

int red = Math.abs(redA-redB);
int green = Math.abs(greenA-greenB);
int blue = Math.abs(blueA-blueB);
int alpha = 255;
result.setRGB((alpha<<24) | (red<<16) | (green<<8) | (blue));

编辑:在ARGB图像中,您需要设置alpha值。如有必要,还可以在上面的示例中进行设置。

© www.soinside.com 2019 - 2024. All rights reserved.