Java分区表面变成小方块

时间:2014-03-19 11:36:23

标签: java image graphics rectangles partition

我想知道是否有任何类似的算法:

给定一个特定的表面,它将它分成相同大小的较小矩形。

像这个示例图:

enter image description here

灰色区域是表面,红色方块是分区本身。

我在想是否有一种优化的方法可以做到这一点。

一个非常糟糕的方法是在所有像素中使用for循环并检查该特定点是否有矩形,否则会创建一个矩形,依此类推..

也许有人知道算法已经完成了吗?还是一个更好的解决方案?

提前多多谢谢;)

2 个答案:

答案 0 :(得分:2)

这是一种解决方法。

  1. 创建图像的蒙版。 (我刚用过Photoshop)

    enter image description here enter image description here

  2. 窃取安德鲁·汤普森的Creating an Area from an Image代码并用它来创建图片的Area

    Area imageArea = getOutline(Color.BLACK, imageMask);
    
  3. 为整个图像创建网格Rectangle2D对象。

    Rectangle2D[][] grid = new Rectangle2D[rows][cols];
    for (int i = 0; i < grid.length; i++) {
        int y = i * CELL_SIZE;
        for (int j = 0; j < grid[i].length; j++) {
            int x = j * CELL_SIZE;
            grid[i][j] = new Rectangle2D.Double(x, y, cellSize, cellSize);
        }
    }
    
  4. 获得网格后,您可以遍历Rectangle2D个对象并检查网格中的Area.contains每个人Rectangle2D,然后您可以将其添加到一个List<Rectangle2D>。只会添加区域中包含的矩形,为您绘制最终的矩形网格。在下面的例子中,我只是将矩形绘制为视觉效果。

    for (Rectangle2D[] rects : imageGrid) {
        for (Rectangle2D rect : rects) {
            if (imageArea.contains(rect)) {
                g2.drawRect((int) rect.getX(), (int) rect.getY(),
                        (int) rect.getWidth(), (int) rect.getHeight());
            }
        }
    }
    

  5. 完整示例

    enter image description here

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Area;
    import java.awt.geom.GeneralPath;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class SquaresInArea extends JPanel {
    
        private static final int CELL_SIZE = 30;
    
        BufferedImage image;
        BufferedImage imageMask;
        Area imageArea;
        Rectangle2D[][] imageGrid;
    
        public SquaresInArea() {
            try {
                image = ImageIO.read(getClass().getResource("/resources/floorplan.png"));
                imageMask = ImageIO.read(getClass().getResource("/resources/floorplan-black.png"));
            } catch (IOException ex) {
                Logger.getLogger(SquaresInArea.class.getName()).log(Level.SEVERE, null, ex);
            }
            imageArea = getOutline(Color.BLACK, imageMask);
            imageGrid = createGrid();
        }
    
        private Rectangle2D[][] createGrid() {
            int width = image.getWidth();
            int height = image.getHeight();
            int rows = height / CELL_SIZE;
            int cols = width / CELL_SIZE;
            Rectangle2D[][] grid = new Rectangle2D[rows][cols];
            for (int i = 0; i < grid.length; i++) {
                int y = i * CELL_SIZE;
                for (int j = 0; j < grid[i].length; j++) {
                    int x = j * CELL_SIZE;
                    grid[i][j] = new Rectangle2D.Double(x, y, CELL_SIZE, CELL_SIZE);
                }
            }
            return grid;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.drawImage(image, 0, 0, this);
            g2.setColor(Color.YELLOW);
            g2.setStroke(new BasicStroke(3f));
            for (Rectangle2D[] rects : imageGrid) {
                for (Rectangle2D rect : rects) {
                    if (imageArea.contains(rect)) {
                        g2.drawRect((int) rect.getX(), (int) rect.getY(),
                                (int) rect.getWidth(), (int) rect.getHeight());
                    }
                }
            }
        }
    
        @Override
        public Dimension getPreferredSize() {
            return image == null ? new Dimension(300, 300)
                    : new Dimension(image.getWidth(), image.getHeight());
    
        }
    
        private Area getOutline(Color target, BufferedImage bi) {
            // construct the GeneralPath
            GeneralPath gp = new GeneralPath();
    
            boolean cont = false;
            int targetRGB = target.getRGB();
            for (int xx = 0; xx < bi.getWidth(); xx++) {
                for (int yy = 0; yy < bi.getHeight(); yy++) {
                    if (bi.getRGB(xx, yy) == targetRGB) {
                        if (cont) {
                            gp.lineTo(xx, yy);
                            gp.lineTo(xx, yy + 1);
                            gp.lineTo(xx + 1, yy + 1);
                            gp.lineTo(xx + 1, yy);
                            gp.lineTo(xx, yy);
                        } else {
                            gp.moveTo(xx, yy);
                        }
                        cont = true;
                    } else {
                        cont = false;
                    }
                }
                cont = false;
            }
            gp.closePath();
    
            // construct the Area from the GP & return it
            return new Area(gp);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new SquaresInArea());
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

    这是另一个清晰的视图

    enter image description here

    private final BasicStroke thin = new BasicStroke(1f);
    private final BasicStroke thick = new BasicStroke(4f);
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(image, 0, 0, this);
        for (Rectangle2D[] rects : imageGrid) {
            for (Rectangle2D rect : rects) {
                if (imageArea.contains(rect)) {
                    g2.setStroke(thick);
                    g2.setColor(Color.GREEN);
                    g2.draw(rect);
                } else {
                    g2.setStroke(thin);
                    g2.setColor(Color.RED);
                    g2.draw(rect);
                }
            }
        }
    }
    

答案 1 :(得分:0)

你只是想用正方形填充它 - 或者你想用最佳数量的方块填充它?

第二种算法更难。

对于第一个只是逐步通过图像的方形尺寸。如果填充该点的像素,则扫描整个正方形,如果全部填充则绘制正方形。如果没有,那么进入下一点。

即。如果正方形是10 * 10像素:

for (int x=0;x<width;x+=SQUARE_SIZE) {
    for (int y=0;y<height;y+=SQUARE_SIZE) {
       // Now check if you can put a valid square here, if so draw it
    }
}