簡體   English   中英

從點創建java.awt.geom.Area

[英]Create java.awt.geom.Area from points

我試圖創建一個java.awt.geom.Area(或使用Shape接口的任何對象)來描述一組創建復雜形狀的點。 這是一個例子:

黃色斑點

我正在嘗試創建一個Area對象(或許多Area對象)來描述黃色區域。 我有一個簡單的方法來獲取黃色像素的坐標,但是我不知道如何創建一個包含所有黃色點的Area對象(或多個Area對象)。

這個stackoverflow問題似乎非常相關( 從邊界點創建封閉的多邊形 ),但是正在使用Matlab。 我認為有人在這里問了一個非常類似的問題( 將列表java.awt.geom.Point2D轉換為java.awt.geom.Area ),但是答案中提供的鏈接已失效。

例如

黃色斑點輪廓

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");
    }
}

我將創建一個GeneralPathPath2D ,它們都實現Shape 本教程說明了如何使用GeneralPath ,它與Path2D非常相似。 然后可以使用Shape來創建Area

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM