比较2个图像并用矩形标记差异

时间:2017-09-14 14:16:14

标签: java image image-processing

编辑:更新了代码,下面的代码现在正确地绘制了多个形状周围的矩形,但仍有一个小问题,有时在一个形状上创建多个矩形。

我有2个图像,我逐个像素地相互比较,我希望我的程序在差异区域周围创建矩形(多个矩形与多个差异实例)。到目前为止,我设法用一个矩形来做到这一点,所以如果我有多个"实例",它们都在一个大矩形中。现在我试图让程序创建多个矩形,但遇到IndexOutOfBoundsException。

程序本身将2个图像与不透明度进行比较,并将生成的叠加图像与矩形一起输出到新文件中。两个被比较的图像具有一致的相等宽度和高度。

我正在调用我想要绘制的矩形"地区"在代码中。 在比较运行时,区域列表会不断更新。

我问自己的第一个问题是,差异点(像素差异)何时属于某个区域? 我的尝试是定义一个"容差",因此只要被比较的像素在最后找到的差异点的容差范围内,它就属于同一个区域。我很快意识到,只要我的形象上有一个巨大的U形,我就不会工作,顶点相距足够远,不在公差范围内。现在我有点卡住了,因为我觉得自己走错了路。

以下是我目前的代码:

private void compareImages() throws IOException{
    BufferedImage img1;
    BufferedImage img2;
    try {
        img1 = ImageIO.read(new File(path_to_img1));
        img2 = ImageIO.read(new File(path_to_img2));
    } catch (Throwable e) {
        System.out.println("Unable to load the Images!");
        return;
    }
    BufferedImage dest = new BufferedImage(img1.getWidth(), img1.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D gfx = dest.createGraphics();
    gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.65f));

    //Compare Images pixel by pixel
    int sX = 9999; //Start X
    int sY = 9999; //Start Y
    int eX = 0; //End X
    int eY = 0; //End Y
    boolean isDrawable = false;
    boolean loadedRegion = false;
    List<Rectangle> regions = new ArrayList<>();
    List<Rectangle> check_regions = new ArrayList<>();
    Rectangle tmp_comparison;
    int regionID = 0;
    int tolerance = 25;

    for (int i = 0; i < img1.getHeight(); i++) {
        for (int j = 0; j < img1.getWidth(); j++) {
            loadedRegion = false;
            regionID = 0;
            sX = 9999;
            sY = 9999;
            eX = 0;
            eY = 0;
            if ( img1.getRGB(j, i) != img2.getRGB(j, i) ){
                isDrawable = true;
                if (regions.size() != 0){
                    //Attempting to locate a matching existing Region
                    tmp_comparison = new Rectangle(j, i, 1, 1);
                    for (int trID = 0; trID<regions.size(); trID++){
                        if (tmp_comparison.intersects(check_regions.get(trID).getBounds()) == true) {
                            // Region found
                            sX = (int) regions.get(trID).getX();
                            sY = (int) regions.get(trID).getY();
                            eX = (int) regions.get(trID).getWidth();
                            eY = (int) regions.get(trID).getHeight();
                            regionID = trID;
                            loadedRegion = true;
                            break;
                        }
                    }
                }
                //Update Region Dimension
                if (j<sX){
                    sX = j;
                }
                if (j>eX){
                    eX = j;
                }
                if (i<sY){
                    sY = i;
                }
                if (i>eY){
                    eY = i;
                }
                if (regions.size() == 0 || loadedRegion == false){
                    regions.add(new Rectangle(sX, sY, eX, eY));
                    check_regions.add(new Rectangle(sX-tolerance, sY-tolerance, eX-sX+(tolerance*2), eY-sY+(tolerance*2)));
                } else {
                    regions.set(regionID, new Rectangle(sX, sY, eX, eY));
                    check_regions.set(regionID, new Rectangle(sX-tolerance, sY-tolerance, eX-sX+(tolerance*2), eY-sY+(tolerance*2)));
                }
            }
        }
    }
    // If there are any differences, draw the Regions
    // Regions are 10px bigger in all directions as compared to the actual rectangles of difference
    if (isDrawable == true){
        gfx.setPaint(Color.red);
        for (int i = 0; i<regions.size(); i++) {
            int dsX = 0;
            int dsY = 0;
            int deX = 0;
            int deY = 0;
            sX = (int) regions.get(i).getX();
            sY = (int) regions.get(i).getY();
            eX = (int) regions.get(i).getWidth();
            eY = (int) regions.get(i).getHeight();
            if (sX>=10){dsX = sX-10;}
            if (eX<=img1.getWidth()-10){deX = eX-sX+20;}
            if (sY>=10){dsY = sY-10;}
            if (eY<=img1.getHeight()-10){deY = eY-sY+20;}
            gfx.draw(new Rectangle2D.Double(dsX, dsY, deX, deY));
        }
    }
    gfx.drawImage(img1, 0, 0, null);
    gfx.drawImage(img2, 0, 0, null);
    gfx.dispose();
    File out = new File("C:\\output.png");
    ImageIO.write(dest, "PNG", out);
}

下面是围绕被比较图像中所有差异创建一个大矩形的代码。

private void oneRectangle() throws IOException{
    BufferedImage img1;
    BufferedImage img2;
    try {
        img1 = ImageIO.read(new File(path_to_img1));
        img2 = ImageIO.read(new File(path_to_img2));
    } catch (Throwable e) {
        System.out.println("Unable to load the Images!");
        return;
    }
    BufferedImage dest = new BufferedImage(img1.getWidth(), img1.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D gfx = dest.createGraphics();
    gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.65f));

    //Compare Images pixel by pixel
    boolean isDrawable = false;
    int sX = 9999;
    int sY = 9999;
    int eX = 0;
    int eY = 0;

    for (int i = 0; i < img1.getHeight(); i++) {
        for (int j = 0; j < img1.getWidth(); j++) {
            if ( img1.getRGB(j, i) != img2.getRGB(j, i) ){
                isDrawable = true;                      
                if (j<sX){
                    sX = j;
                }
                if (j>eX){
                    eX = j;
                }
                if (i<sY){
                    sY = i;
                }
                if (i>eY){
                    eY = i;
                }
            }
        }
    }
    // Draw rectangle if there are any differences
    if (isDrawable == true){
        gfx.setPaint(Color.red);
        int dsX = 0;
        int dsY = 0;
        int deX = 0;
        int deY = 0;
        if (sX>=10){dsX = sX-10;}
        if (eX<=img1.getWidth()-10){deX = eX-sX+20;}
        if (sY>=10){dsY = sY-10;}
        if (eY<=img1.getHeight()-10){deY = eY-sY+20;}
        gfx.fill(new Rectangle2D.Double(dsX, dsY, deX, deY));
    }
    gfx.drawImage(img1, 0, 0, null);
    gfx.drawImage(img2, 0, 0, null);
    gfx.dispose();
    File out = new File("C:\\output.png");
    ImageIO.write(dest, "PNG", out);
}

0 个答案:

没有答案