2017-02-15 36 views
1

这里的基本问题是,我绘制了一个图像,它是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()方法,但它有我的抗锯齿问题。所以得到一些帮助会很好。需要

enter image description here

光灰度化部以从图像中减去:

,图像被。

所需要的图像是:

enter image description here

多边形可以是任何形状的。

+0

可以说你想要什么样的结果?最好制作一张你想要的图像。 –

+0

@BahramdunAdil我想要的是绘制灰度图像和任何多边形以上的颜色 –

+0

您的意思是说,复制具有多边形形状的图像部分并将其放入新图像中?但最好发布最后想要的图像,意味着您希望结果图像是什么样的结果,编辑问题并发布图像。 –

回答

0

在这里,我会建议你尝试这种方式: 你可以创建一个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()); 
     } 
    } 
} 

它是(没有任何额外的过程)

+0

does contains()方法的形状不同,因为它不提供良好的形状结果。 –

+0

此方法未被证明是稳定的,并且灰度缩放图像的多边形不在其位置 –

+0

但是如果要检查是否有任何点在多边形内部,则需要首先创建一个多边形对象,然后它将很容易地播放用。 –

0

代码中的小错误:索引倒在循环中。 正确的版本是:

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