【发布时间】:2018-07-30 11:15:26
【问题描述】:
如何有效地检查曲线是否闭合?例如看这个图:
曲线将始终为黑色背景上的白色。 我尝试了洪水填充算法,但在这种情况下效果不佳(我不明白如何修改它)。
代码如下:
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");
}
}
}
}
【问题讨论】:
-
请定义“效果不佳”。它有什么作用?您希望它做什么或不做什么?你试过调试吗?
-
@M.Prokhorov 算法不遵循这条线,而是从上到下,从左到右。所以我不明白如何适应它
-
这不是因为您在图像中迭代点并将它们添加到“队列”中的方式。您也不能通过使用另一条单像素线来“跟随”一条像素线,在对角线上会有一些像素边上没有白色。
标签: java computer-vision ocr curve