【发布时间】:2019-09-24 08:52:58
【问题描述】:
所以给定一个二维点数组,我如何找到多边形坐标的每个点都与下一个点(对角邻接)的所有多边形?
例如这张图片:
我有兴趣淹没由闭合点链包围的区域(每个坐标都与下一个坐标相邻,形成一个闭合环),就其形状而言。我对识别单个多边形不感兴趣,只填充多边形。每个点都有一个标签(1、2、3、+ 等),其中 0 未设置所以例如:
- 黄色是填充中心点的正方形。
- 橙色是一个更大的正方形,但边角只是对角相邻。
- 绿色是一种形状,如果您跟随点链,就不能总是向右转。
- 红色不是有效的形状,因为它的点链没有闭合。
- 蓝色不是有效形状,因为它的点链没有闭合,边缘也没有闭合。
我看过凸包,但这给了我所有点的包,这不是我感兴趣的。
情侣规则: - 每个点都有一个从 1 到 10 的值。 - 只有当 closed 点链具有相同标签时,才能填充区域。 - 未闭合的点链不是有效区域。 - 点的 x/y 坐标是整数。
我正在用 Java 实现它,我已经尝试过了:https://github.com/Merowech/java-concave-hull/blob/master/ConcaveHull.java
但是给定这样的地图:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 1, 0, 1, 0, 1, 0, 2, 2, 0, 0, 0, 0, 2, 0
0, 1, 0, 0, 0, 1, 2, 0, 0, 2, 0, 0, 0, 0, 0
0, 0, 1, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0
0, 0, 0, 1, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 3, 3, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0
它给了我这个作为输出:
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 1, 0, 0, 0, 1, 0, 2, 2, 0, 0, 0, 0, 2, 0
0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0
我也尝试过 DBSCAN,它最终无法忽略相同标签的大集群旁边的小集群
更新:
一个简单的洪水填充是行不通的,因为要求多边形的所有周围像素都具有相同的值:
public static void floodFill(int[][] map, int fill, Point loc) {
if (loc.x < 0 || loc.x >= map[0].length || loc.y < 0 || loc.y >= map.length) throw new IllegalArgumentException();
int old = map[loc.y][loc.x];
// Checks trivial case where loc is of the fill color
if (fill == old) return;
floodLoop(map, loc.x, loc.y, fill, old);
}
// Recursively fills surrounding pixels of the old color
private static void floodLoop(int[][] map, int x, int y, int fill, int old) {
int[] aux = {255, 255, 255, 255};
// finds the left side, filling along the way
int fillL = x;
do {
map[y][fillL] = fill;
fillL--;
} while (fillL >= 0 && map[y][fillL] == old);
fillL++;
// find the right right side, filling along the way
int fillR = x;
do {
map[y][fillR] = fill;
fillR++;
} while (fillR <= map[0].length - 1 && map[y][fillR] == old);
fillR--;
// checks if applicable up or down
for (int i = fillL; i <= fillR; i++) {
if (y > 0 && map[y-1][i] == old) floodLoop(map, i, y - 1,
fill, old);
if (y < map.length - 1 && map[y+1][i] == old) floodLoop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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 4, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 4, 0, 4, 0, 0, 0, 0, 5, 5, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
private static void printMap(int[][] map)
{
for (int y = 0; y < map.length; y++)
{
for (int x = 0; x < map[0].length; x++)
{
System.out.print(map[y][x] + " ,");
}
System.out.println();
}
}
public static void main(String[] args)
{
int[][] copy = map.clone();
floodFill(copy, 1, new Point(0,0));
printMap(copy);
}
【问题讨论】:
-
为了获得更好的帮助,请尽快发布正确的minimal reproducible example,以展示您的问题和解决问题的努力。顺便说一句,您使用的是哪个 Java 框架?摇摆?
-
我没有使用任何 Java 框架。 DBSCAN 来自 apache math3 库。
-
我对 DBSCAN 或 math3 都不熟悉,但任何熟悉的人都需要查看您的代码。请将其作为代码格式的文本发布在您的问题中,而不是在外部链接中
-
@Wesley 如果您对多边形本身不感兴趣,我很确定使用专门的4-direction flood fill 会比实际找到多边形容易得多。你考虑过使用它吗?您可以使用不同的颜色多次执行泛洪填充,并检查填充是否仅接触一个多边形。
-
@Gilles-PhilippePaillé 我添加了一个我无法开始工作的示例。我只对用完全封闭的值填充图块感兴趣,并且仅当该值相同时才感兴趣。