检查曲线是否闭合 [英] Check if a curve is closed

查看:144
本文介绍了检查曲线是否闭合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何有效检查曲线是否闭合?例如看这个图:

How can I check efficiently if a curve is closed? For example look this figure:

在黑色背景上,曲线将始终为白色. 我尝试使用洪水填充算法,但在这种情况下效果不佳(我不知道如何修改).

The curve will always be white on a black background. I tried with flood fill algorithm but not works well with this situation (I don't understand how modify it).

代码在这里:

public static boolean isWhite(BufferedImage image, int posX, int posY) {
    Color color = new Color(image.getRGB(posX, posY));
    int r=color.getRed();
    int g=color.getGreen();
    int b=color.getBlue();
    if(r==0&&g==0&&b==0)
        return false;
    return true;
}

public static void checkClosed(BufferedImage bimg) {

    boolean[][] painted = new boolean[bimg.getHeight()][bimg.getWidth()];

    for (int i = 0; i < bimg.getHeight(); i++) {
        for (int j = 0; j < bimg.getWidth(); j++) {

            if (isWhite(bimg, j, i) && !painted[i][j]) {

                Queue<Point> queue = new LinkedList<Point>();
                queue.add(new Point(j, i));

                int pixelCount = 0;
                while (!queue.isEmpty()) {
                    Point p = queue.remove();

                    if ((p.x >= 0) && (p.x < bimg.getWidth() && (p.y >= 0) && (p.y < bimg.getHeight()))) {
                        if (!painted[p.y][p.x] && isWhite(bimg, p.x, p.y)) {
                            painted[p.y][p.x] = true;
                            pixelCount++;


                            queue.add(new Point(p.x + 1, p.y));
                            queue.add(new Point(p.x - 1, p.y));
                            queue.add(new Point(p.x, p.y + 1));
                            queue.add(new Point(p.x, p.y - 1));
                        }
                    }
                }
                System.out.println("Blob detected : " + pixelCount + " pixels");
            }

        }
    }
}

推荐答案

查看图像边界是否闭合的方法是通过从所有图像边缘像素开始对边界进行填充.也就是说,您将队列中图像边缘的所有背景像素放入队列中,然后从那里进行泛洪填充.

The way to see if the boundary in your image is closed is by doing a flood fill of the boundary starting at all the image edge pixels. That is, you put all the background pixels that are at the image edge on the queue, then flood fill from there.

接下来,检查是否剩余背景像素.如果洪水填充物填充在对象内部,则边界没有闭合.

Next, check to see if any background pixels are left. If the flood fill filled inside the object, the boundary wasn’t closed.

这篇关于检查曲线是否闭合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆