如何有效地检查曲线是否闭合?例如看这个图:
![Image with curve](https://i.stack.imgur.com/czC6f.png)
曲线在黑色背景上始终为白色。
我尝试使用洪水填充算法,但在这种情况下效果不佳(我不明白如何修改它)。
这里是代码:
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");
}
}
}
}
查看图像中的边界是否闭合的方法是从所有图像边缘像素开始对边界进行洪水填充。也就是说,您将图像边缘的所有背景像素放入队列中,然后从那里进行洪水填充。
接下来,检查是否留下任何背景像素。如果洪水填充填充到对象内部,则边界并未闭合。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)