这里的基本问题是,我绘制了一个图像,它是grayScaled并且是该形状的Rectangle边界。我已经在那个矩形上绘制了形状。现在我需要从图像中删除访问区域。如何从形状中减去图像的面积
的代码以获得矩形边界从形状是:
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我想要的是绘制灰度图像和任何多边形以上的颜色 –
您的意思是说,复制具有多边形形状的图像部分并将其放入新图像中?但最好发布最后想要的图像,意味着您希望结果图像是什么样的结果,编辑问题并发布图像。 –