Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 突出显示图像之间的差异_Java_Image_Image Processing_Pixel_Pixels - Fatal编程技术网

Java 突出显示图像之间的差异

Java 突出显示图像之间的差异,java,image,image-processing,pixel,pixels,Java,Image,Image Processing,Pixel,Pixels,我应该修改这个图像比较代码来突出显示/指出两个图像之间的差异。是否有办法修改此代码以突出显示图像中的差异。如果没有任何关于如何进行的建议,我们将不胜感激 int width1 = img1.getWidth(null); int width2 = img2.getWidth(null); int height1 = img1.getHeight(null); int height2 = img2.getHeight(nul

我应该修改这个图像比较代码来突出显示/指出两个图像之间的差异。是否有办法修改此代码以突出显示图像中的差异。如果没有任何关于如何进行的建议,我们将不胜感激

 int width1 = img1.getWidth(null);
            int width2 = img2.getWidth(null);
            int height1 = img1.getHeight(null);
            int height2 = img2.getHeight(null);
            if ((width1 != width2) || (height1 != height2)) {
                System.err.println("Error: Images dimensions mismatch");
                System.exit(1);
            }
            long diff = 0;
            for (int i = 0; i < height1; i++) {
                for (int j = 0; j < width1; j++) {
                    int rgb1 = img1.getRGB(j, i);
                    int rgb2 = img2.getRGB(j, i);
                    int r1 = (rgb1 >> 16) & 0xff;
                    int g1 = (rgb1 >> 8) & 0xff;
                    int b1 = (rgb1) & 0xff;
                    int r2 = (rgb2 >> 16) & 0xff;
                    int g2 = (rgb2 >> 8) & 0xff;
                    int b2 = (rgb2) & 0xff;
                    diff += Math.abs(r1 - r2);
                    diff += Math.abs(g1 - g2);
                    diff += Math.abs(b1 - b2);
                }
            }
            double n = width1 * height1 * 3;
            double p = diff / n / 255.0;
            return (p * 100.0);
intwidth1=img1.getWidth(null);
int-width2=img2.getWidth(null);
int height1=img1.getHeight(null);
int height2=img2.getHeight(null);
如果((宽度1!=宽度2)| |(高度1!=高度2)){
System.err.println(“错误:图像尺寸不匹配”);
系统出口(1);
}
长差=0;
对于(int i=0;i>16)和0xff;
int g1=(rgb1>>8)和0xff;
int b1=(rgb1)&0xff;
int r2=(rgb2>>16)和0xff;
int g2=(rgb2>>8)和0xff;
int b2=(rgb2)&0xff;
微分+=数学绝对值(r1-r2);
微分+=数学绝对值(g1-g2);
微分+=数学abs(b1-b2);
}
}
双n=宽度1*高度1*3;
双p=diff/n/255.0;
回报率(p*100.0);

我要做的是将每个像素设置为一幅图像中的一个像素与另一幅图像中相应像素之间的差值。在原始代码中计算的差异基于。这也叫“太”。在任何情况下,编写一个方法,接收两个图像,并返回一个大小相同的图像,该图像将每个位置设置为最终图像中共享同一位置的每对像素的差值。基本上,这将指示哪些像素是不同的。像素越白,这两个对应位置之间的差异就越大

我还将假设您使用的是
BufferedImage
类,因为使用了
getRGB()
方法,并且您正在进行位移位以访问各个通道。换句话说,创建一个如下所示的方法:

public static BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) {
    int width1 = img1.getWidth(); // Change - getWidth() and getHeight() for BufferedImage
    int width2 = img2.getWidth(); // take no arguments
    int height1 = img1.getHeight();
    int height2 = img2.getHeight();
    if ((width1 != width2) || (height1 != height2)) {
        System.err.println("Error: Images dimensions mismatch");
        System.exit(1);
    }

    // NEW - Create output Buffered image of type RGB
    BufferedImage outImg = new BufferedImage(width1, height1, BufferedImage.TYPE_INT_RGB);

    // Modified - Changed to int as pixels are ints
    int diff;
    int result; // Stores output pixel
    for (int i = 0; i < height1; i++) {
        for (int j = 0; j < width1; j++) {
            int rgb1 = img1.getRGB(j, i);
            int rgb2 = img2.getRGB(j, i);
            int r1 = (rgb1 >> 16) & 0xff;
            int g1 = (rgb1 >> 8) & 0xff;
            int b1 = (rgb1) & 0xff;
            int r2 = (rgb2 >> 16) & 0xff;
            int g2 = (rgb2 >> 8) & 0xff;
            int b2 = (rgb2) & 0xff;
            diff = Math.abs(r1 - r2); // Change
            diff += Math.abs(g1 - g2);
            diff += Math.abs(b1 - b2);
            diff /= 3; // Change - Ensure result is between 0 - 255
            // Make the difference image gray scale
            // The RGB components are all the same
            result = (diff << 16) | (diff << 8) | diff;
            outImg.setRGB(j, i, result); // Set result
        }
    }

    // Now return
    return outImg;
}

这是假设您正在类的方法中调用它。祝你玩得开心,好运

这个解决方案帮了我的忙。它突出了不同之处,并且具有我尝试过的方法中最好的性能。(假设:图像大小相同。此方法尚未使用透明胶片进行测试。)

比较1600x860 PNG图像50次的平均时间(在同一台机器上):

  • JDK7~178毫秒
  • JDK8~139毫秒
有人有更好/更快的解决方案吗

public static BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) {
    // convert images to pixel arrays...
    final int w = img1.getWidth(),
            h = img1.getHeight(), 
            highlight = Color.MAGENTA.getRGB();
    final int[] p1 = img1.getRGB(0, 0, w, h, null, 0, w);
    final int[] p2 = img2.getRGB(0, 0, w, h, null, 0, w);
    // compare img1 to img2, pixel by pixel. If different, highlight img1's pixel...
    for (int i = 0; i < p1.length; i++) {
        if (p1[i] != p2[i]) {
            p1[i] = highlight;
        }
    }
    // save img1's pixels to a new BufferedImage, and return it...
    // (May require TYPE_INT_ARGB)
    final BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    out.setRGB(0, 0, w, h, p1, 0, w);
    return out;
}

我假设您使用的是
buffereImage
类?这个编辑非常简单。你所要做的就是将图像中的每个像素设置为差异。我会给你写一个答案。谢谢你发布这个解决方案。它也解决了我的问题。但是,我使用了IntStream.range()而不是for循环,而且它运行得更快(即使在lambda中调用了AtomicDouble.getAndIncrement()。@derrdji-太棒了!你能给我们看一下代码和性能比较吗?很棒的工作..速度也很快..谢谢你的回答这对图像的特定部分有什么作用?@roger_,这取决于-但这是一个完全不同的问题。如果你在比较一个“正方形”的形状,你可能会自己计算出来。。。新的BufferedImage(宽度,高度)应该是新的BufferedImage(宽度1,高度1
public static BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) {
    // convert images to pixel arrays...
    final int w = img1.getWidth(),
            h = img1.getHeight(), 
            highlight = Color.MAGENTA.getRGB();
    final int[] p1 = img1.getRGB(0, 0, w, h, null, 0, w);
    final int[] p2 = img2.getRGB(0, 0, w, h, null, 0, w);
    // compare img1 to img2, pixel by pixel. If different, highlight img1's pixel...
    for (int i = 0; i < p1.length; i++) {
        if (p1[i] != p2[i]) {
            p1[i] = highlight;
        }
    }
    // save img1's pixels to a new BufferedImage, and return it...
    // (May require TYPE_INT_ARGB)
    final BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    out.setRGB(0, 0, w, h, p1, 0, w);
    return out;
}
import javax.imageio.ImageIO;
import java.io.File;

ImageIO.write(
        getDifferenceImage(
                ImageIO.read(new File("a.png")),
                ImageIO.read(new File("b.png"))),
        "png",
        new File("output.png"));