2017-02-15 58 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());