【问题标题】:How to subtract area of Image from Shape如何从形状中减去图像的面积
【发布时间】:2017-02-15 12:05:43
【问题描述】:

这里的基本问题是我绘制了一个灰度图像并且是形状的矩形边界。我已经在那个矩形上绘制了形状。现在我需要从图像中删除访问区域。

从形状中获取矩形边界的代码是:

public static Rectangle getBoundingBox(Shape shape,Graphics2D g) {
     int minX = Integer.MAX_VALUE;
     int minY = Integer.MAX_VALUE;
     int maxX = Integer.MIN_VALUE;
     int maxY = Integer.MIN_VALUE;

    final Rectangle polygonBounds = shape.getBounds();

    int ax = polygonBounds.x;
    int ay = polygonBounds.y;
    int bx = ax + polygonBounds.width;
    int by = ay + polygonBounds.height;

    minX = Math.min(ax, minX);
    minY = Math.min(ay, minY);
    maxX = Math.max(bx, maxX);
    maxY = Math.max(by, maxY);


final Rectangle boundingBox = new Rectangle(minX, minY, 1, 1);
boundingBox.add(maxX, maxY);
return boundingBox;
}    

绘制矩形和选中形状的代码是:

        Rectangle rect = getBoundingBox((Shape)selectedArea,g);
        int x = (int)rect.getX();
        int y = (int)rect.getY();
        int width = (int)rect.getWidth();
        int height = (int)rect.getHeight();
        if((x+width)>grayScaledImage.getWidth()){
            width = grayScaledImage.getWidth()-x;
        }
        if(((y+height)>grayScaledImage.getHeight())){
            height = grayScaledImage.getHeight()-y;
        }

        BufferedImage img = grayScaledImage.getSubimage(x,y,width,height);
            }
            }
        }

        g.drawImage(img,x,y,null);

对矩形内的图像进行灰度化的代码是:

       private BufferedImage toGray(BufferedImage image) {
            int width = image.getWidth();
            int height = image.getHeight();
            for (int i = 0; i < height; i++) {
                 for (int j = 0; j < width; j++) {
                 Color c = new Color(image.getRGB(j, i));
                 int red = (int) (c.getRed() * 0.3);
                 int green = (int) (c.getGreen() * 0.59);
                 int blue = (int) (c.getBlue() * 0.11);

                 int sum = red + green + blue;
                 Color newColor = new Color(sum, sum, sum);
                 //Color newColor =Color.red;
                 image.setRGB(j, i, newColor.getRGB());
              }
           }
           return image;
     }

我已经尝试过使用 setClip() 方法,但它存在某种抗锯齿问题。所以能得到一些帮助会很棒。

图片是:

需要从图像中减去浅灰度部分。

需要的图片是:

多边形可以是任何形状。

【问题讨论】:

  • 可以说你想要什么样的结果?最好在最后做出你想要的图像。
  • @BahramdunAdil 我想要的是在任何多边形形状上方绘制灰度图像和颜色
  • 你的意思是,复制图像中具有多边形形状的部分并将其放入新图像中?但最好在最后贴一张你想要的图片,意思是你希望结果图片是什么样的结果,编辑问题并发布图片。
  • @BahramdunAdil 我已经按照要求编辑了图片。
  • 表示要清除灰色内的矩形,只留下多边形区域?

标签: java graphics paint paintcomponent graphics2d


【解决方案1】:

在这里,我建议您尝试这种方式: 您可以创建一个Polygon 对象并提供xPointsyPoint。像这样的

int[] xPoints;
int[] yPoints;
int length = xPoint.length;
Polygon polygon = new Polygon(xPoints, yPoints, length );

现在添加这样的颜色:

Rectangle bounds = polygon.getBounds();
for (int i = 0; i < bounds.width; i++) {
    for (int j = 0; j < bounds.height; j++) {
        if (polygon.contains(i, j)) {// check if the point is inside the polygon
            // do the color stuff here
            Color c = new Color(image.getRGB(j, i));
            int red = (int) (c.getRed() * 0.3);
            int green = (int) (c.getGreen() * 0.59);
            int blue = (int) (c.getBlue() * 0.11);

            int sum = red + green + blue;
            Color newColor = new Color(sum, sum, sum);
            //Color newColor =Color.red;
            image.setRGB(j, i, newColor.getRGB());
        }
    }
}

就是这样(无需任何额外过程)

【讨论】:

  • contain() 方法是否因形状而异,因为它不能为形状提供良好的结果。
  • 此方法不稳定,灰度图像多边形不在其位置
  • 但是如果你想检查任何点是否在多边形内,首先需要创建一个多边形对象,然后它就很容易玩了。
【解决方案2】:

代码中的一个小错误:索引在循环中被反转。 正确的版本是:

Color c = new Color(image.getRGB(i, j));
        int red = (int) (c.getRed() * 0.3);
        int green = (int) (c.getGreen() * 0.59);
        int blue = (int) (c.getBlue() * 0.11);

        int sum = red + green + blue;
        Color newColor = new Color(sum, sum, sum);
        //Color newColor =Color.red;
        image.setRGB(i, j, newColor.getRGB());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多