有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

图像Java将曲面分割成小正方形

我想知道是否有这样的算法:

给定一个特定的表面,它将其划分为大小相同的较小矩形

类似于此示例图:

enter image description here

灰色区域是曲面,红色正方形是分区本身

我在想,是否有一个优化的方法来做到这一点

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

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

提前多谢;)


共 (2) 个答案

  1. # 1 楼答案

    这里有一个方法

    1. 创建图像的遮罩。(我刚用过Photoshop)

      enter image description hereenter image description here

    2. 窃取AndrewThompson的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());
              }
          }
      }
      

    完整示例

    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);
                }
            }
        }
    }
    
  2. # 2 楼答案

    你只是想用正方形填充它,还是想用最佳的正方形数填充它

    第二种算法更难

    对于第一步,只需一次通过一个正方形大小的图像。如果该点的像素已填充,则扫描整个正方形,如果全部填充,则绘制正方形。如果没有,请转到下一点

    即,如果正方形为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
        }
    }