2013-09-03 1 views
1

복잡한 모양을 만드는 점 집합을 설명하기 위해 java.awt.geom.Area (또는 Shape 인터페이스를 사용하는 모든 객체)를 만들려고합니다. 나는 노란색 영역을 설명하기 위해 지역 객체 (또는 여러 영역 개체)를 만들려고하고포인트에서 java.awt.geom.Area 만들기

Yellow Blobs

예를 들면 다음과 같습니다. 노란색 픽셀의 좌표를 얻는 쉬운 방법이 있지만 모든 노랑색 점을 포함하는 Area 개체 (또는 여러 Area 개체)를 만드는 방법을 알지 못합니다.

이 stackoverflow 질문은 매우 관련이 보인다 Create closed polygon from boundary points)하지만 Matlab을 사용하고 있습니다. 나는 다른 누군가가 매우 비슷한 질문을 여기 (Convert a list java.awt.geom.Point2D to a java.awt.geom.Area) 요청했지만 답변에 제공된 링크가 죽었어요.

+0

가능한 중복 (http://stackoverflow.com/questions/7052422/image-graphic-into-a-shape) –

답변

0

나는 모두 Shape을 구현하는 GeneralPath 또는 Path2D을 만들 것입니다. This tutorialPath2D과 매우 유사한 GeneralPath을 사용하는 방법을 설명합니다. 그런 다음 Shape을 사용하여 Area을 만들 수 있습니다.

2

E.G.

Yellow Blob Outline

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.awt.geom.Area; 
import javax.imageio.ImageIO; 
import java.io.File; 
import java.net.URL; 
import java.util.Date; 
import javax.swing.*; 

/* See also http://stackoverflow.com/q/7052422/418556 */ 
class ImageOutline { 

    public static Area getOutline(
     BufferedImage image, Color color, boolean include, int tolerance) { 

     Area area = new Area(); 
     for (int x=0; x<image.getWidth(); x++) { 
      for (int y=0; y<image.getHeight(); y++) { 
       Color pixel = new Color(image.getRGB(x,y)); 
       if (include) { 
        if (isIncluded(color, pixel, tolerance)) { 
         Rectangle r = new Rectangle(x,y,1,1); 
         area.add(new Area(r)); 
        } 
       } else { 
        if (!isIncluded(color, pixel, tolerance)) { 
         Rectangle r = new Rectangle(x,y,1,1); 
         area.add(new Area(r)); 
        } 
       } 
      } 
     } 
     return area; 
    } 

    public static boolean isIncluded(
     Color target, Color pixel, int tolerance) { 

     int rT = target.getRed(); 
     int gT = target.getGreen(); 
     int bT = target.getBlue(); 
     int rP = pixel.getRed(); 
     int gP = pixel.getGreen(); 
     int bP = pixel.getBlue(); 
     return(
      (rP-tolerance<=rT) && (rT<=rP+tolerance) && 
      (gP-tolerance<=gT) && (gT<=gP+tolerance) && 
      (bP-tolerance<=bT) && (bT<=bP+tolerance)); 
    } 

    public static BufferedImage drawOutline(int w, int h, Area area) { 

     final BufferedImage result = new BufferedImage(
      w, 
      h, 
      BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = result.createGraphics(); 

     g.setColor(Color.white); 
     g.fillRect(0,0,w,h); 

     g.setClip(area); 
     g.setColor(Color.red); 
     g.fillRect(0,0,w,h); 

     g.setClip(null); 
     g.setStroke(new BasicStroke(1)); 
     g.setColor(Color.blue); 
     g.draw(area); 

     return result; 
    } 

    public static BufferedImage createAndWrite(
     BufferedImage image, 
     Color color, 
     boolean include, 
     int tolerance, 
     String name) 
     throws Exception { 

     int w = image.getWidth(); 
     int h = image.getHeight(); 

     System.out.println("Get Area: " + new Date() + " - " + name); 
     Area area = getOutline(image, color, include, tolerance); 
     System.out.println("Got Area: " + new Date() + " - " + name); 

     final BufferedImage result = drawOutline(w,h,area); 
     displayAndWriteImage(result, name); 

     return result; 
    } 

    public static void displayAndWriteImage(
     BufferedImage image, String fileName) throws Exception { 

     ImageIO.write(image, "png", new File(fileName)); 
     JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image))); 
    } 

    public static void main(String[] args) throws Exception { 
     URL url = new URL("http://i.stack.imgur.com/aGBuT.png"); 
     final BufferedImage outline = ImageIO.read(url); 
     displayAndWriteImage(outline, "motorcycle-01.png"); 
     createAndWrite(
      outline, Color.white, false, 60, "YellowBlobOutline.png"); 
    } 
} 
[A 모양으로 이미지/그래픽]의
+0

가 [을 수용하십시오 대답] (http://meta.stackexchange.com/a/5235/155831)을 참조하십시오. –