Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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_Graphics_Paint_Paintcomponent_Graphics2d - Fatal编程技术网

Java 如何从形状中减去图像的面积

Java 如何从形状中减去图像的面积,java,graphics,paint,paintcomponent,graphics2d,Java,Graphics,Paint,Paintcomponent,Graphics2d,这里的基本问题是,我画了一个灰度图像,是形状的矩形边界。我已经在那个矩形上画了形状。现在我需要从图像中删除访问区域 从形状获取矩形边界的代码为: public static Rectangle getBoundingBox(Shape shape,Graphics2D g) { int minX = Integer.MAX_VALUE; int minY = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE;

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

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

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);
绘制矩形和选定形状的代码为:

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;
     }
private buffereImage toGray(buffereImage图像){
int width=image.getWidth();
int height=image.getHeight();
对于(int i=0;i
我已经尝试过setClip()方法,但它存在某种抗锯齿问题。所以能得到一些帮助就太好了

图像是:

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

所需图像为:


多边形可以是任何形状。

这里我建议您尝试以下方法: 您可以创建一个
多边形
对象,并给出
xPoints
yPoint
。像这样的

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());
        }
    }
}
Rectangle bounds=polygon.getBounds();
for(int i=0;i

就是这样(没有任何额外的过程)

在这里,我建议您尝试以下方法: 您可以创建一个
多边形
对象,并给出
xPoints
yPoint
。像这样的

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());
        }
    }
}
Rectangle bounds=polygon.getBounds();
for(int i=0;i

(没有任何额外的过程)

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

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());

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

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());

可以说你想要什么样的结果?最好是在最后制作一幅你想要的图像。@BahramdunAdil我想要的是在任何多边形形状上绘制一幅灰度图像和一种颜色。你的意思是,复制图像中具有多边形形状的部分并将其放入新图像中?但是最好在最后贴一张你想要的图片,这意味着你想要的结果图片是什么样的,编辑问题并发布图像。@BahramdunAdil我已经按照要求编辑了图像。这意味着你想清除灰色内的矩形,只留下多边形区域?你能说你想要什么样的结果吗?最好是在最后制作一幅你想要的图像。@BahramdunAdil我想要的是在任何多边形形状上绘制一幅灰度图像和一种颜色。你的意思是,复制图像中具有多边形形状的部分并将其放入新图像中?但最好在最后发布一幅你想要的图像,这意味着你希望结果图像是什么样的,编辑问题并发布图像。@BahramdunAdil我已经按照要求编辑了图像。这意味着你想清除灰色内的矩形,只留下多边形区域?是否包含()形状的方法不同,因为它不能为形状提供良好的结果。此方法证明不稳定,且具有灰度图像的多边形不在其位置,但如果要检查多边形内是否有任何点,则需要先创建多边形对象,然后再轻松使用。does contains()方法因形状不同而不同,因为它不能为形状提供良好的结果。此方法证明不稳定,并且带有灰度图像的多边形不在其位置,但如果要检查多边形内是否有任何点,则需要首先创建多边形对象,然后使用它将很容易。