从矩阵中获取区域/区域边界
本文关键字:区域 区域边界 获取 | 更新日期: 2023-09-27 18:15:15
听起来我有一个非常基本的需求:提取组成矩阵的区域坐标。
让我举个例子。下面是一些矩阵:
| A | B | D | E | F | G | H | I | J |
| 1 | 0 | 0 | 0 | 2 | 2 | 2 | 4 | 4 | 4 |
| 2 | 0 | 0 | 2 | 2 | 2 | 2 | 4 | 4 | 4 |
| 3 | 0 | 0 | 2 | 2 | 2 | 3 | 3 | 4 | 4 |
| 4 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
| 5 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
| 6 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
| 7 | 1 | 0 | 0 | 0 | 1 | 1 | 3 | 0 | 4 |
| 8 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 9 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
| 10| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
我想得到一个数组的边界和每个区域的值(不需要特定的顺序)。
左上角区域示例:
- 值:0
- 边界:{A1, D1, B2, B3, 13}
你知道一些库满足这个需求吗?或者我应该自己编写这个吗?
我自己编写,不确定是否有库。
对于每个区域,我会依次考虑每个点。那么(我认为)这应该可以工作:
if (surrounding 8 squares has at least one with different region)
{
for each 3 squares, above, below, left and right
{
if (less than 3 are different, and the middle is different)
{
is a boundry
}
}
for each 3 squares, above, below
{
for each 3 squares, left, right
{
if(all 3 from outer loop and all 3 from inner loop are different)
{
is a boundry
}
}
}
not a boundry
}
else
{
not a boundry
}