求矩阵中的面积..?

发布于 2024-09-08 04:01:33 字数 194 浏览 7 评论 0原文

假设我有一个非常大的矩阵,其中包含 10000x10000 个元素,所有元素的值为“0”。假设有一些大的“1 巢”。这些区域甚至可能是连接的,但每周都会通过“1”的“管道”连接。

我想要一种算法能够非常快速地(如果需要的话,也可以是脏的)找到这些“1”的“巢穴”。在这里,它不应该“分割”两个每周相连的“巢”。

知道我应该如何做这样的算法吗?

lets say I have a very big matrix with 10000x10000 elements all having the value '0'. Lets say there are some big 'nests' of '1's. Those areas might even be connected, but very weekly connected by a 'pipe' of '1's.

I want to get an algorithm that very quickly (and dirty if necessary) finds these 'nests' of '1's. Here it shouldn't 'cut apart' two weekly connected 'nests'.

Any idea how I should do such an algorithm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-09-15 04:01:33

也许像 A* 这样的寻路算法(或者像 BFS 或 DFS 这样更简单的算法)可能会在这种情况下工作..

您可以:

  • 通过查找小嵌套(忽略管道)来搜索搜索的起点..因此至少有一个由1组成的3x3块
  • ,那么您应该从那里开始路径查找,直到结束“连接的组件” (诗意许可)矩阵内部
  • 从另一个小 1 的块开始重复

Maybe a pathfinding algorithm like A* (or something simpler like a BFS or DFS) may work in this case..

You can:

  • search starting point for your searches by finding small nests (ignoring pipes).. so at least a 3x3 block of 1's
  • then you should pathfind from there going through 1's until you end your "connected component" (poetic license) inside the matrix
  • repeat starting from another small 1's block
风吹短裙飘 2024-09-15 04:01:33

我想说这取决于数据的需要。如果给定两点,您需要检查它们是否在同一个 1 块中,我认为@Jack 的答案是最好的。如果您对块最初的位置有一定的了解,这也是正确的,因为您可以使用它们作为算法的起点。

如果您没有任何其他信息,也许其中之一是可能的:

如果给定一个点,您希望找到同一块中的所有元素,洪水填充 比较合适。然后,您可以在找到每个巢时对其进行缓存,当您获得另一个点时,首先查看它是否在已知的巢中,如果不是,则进行洪水填充以找到该巢,然后将其添加到缓存中。

作为实现细节,当您遍历矩阵时,每一行都应该具有前一行上存在的可用嵌套集。然后,您只需对照这些嵌套检查新点,而不是完整的集合,即可确定新点是否在已知集合中。

确保您使用查找成本非常低的集合实现,例如哈希表或可能的布隆过滤器(如果您可以处理概率效应)。

I would say it depends on how the data is needed. If, given two points, you need to check if they are in the same block of 1's, I think @Jack's answer is best. This is also true if you have some knowledge of where blocks are initially, as you can use those as starting points for your algorithm.

If you don't have any other information, maybe one of these would be a possibility:

If given a point, you wish to find all elements in the same block, a flood fill would be appropriate. Then you could cache each nest as you find it, and when you get another point first see if it's in a known nest, and if it isn't do a flood fill to find this nest then add it to the cache.

As an implementation detail, as you traverse the matrix each row should have available the set of nests present on the previous row. Then you would only need to check new points against those nests, rather than the complete set, to determine if a new point is in a known set or not.

Be sure that you use a set implementation with a very low lookup cost such as a hashtable or possibly a Bloom filter if you can deal with the probabilistic effects.

蓝色星空 2024-09-15 04:01:33
  1. 将矩阵转换为黑白位图
  2. 缩放矩阵,使大小为 N 的嵌套成为单个像素(因此,如果您寻找 10x10 嵌套,请缩放 N=10 倍)。
  3. 使用输出的剩余像素来定位巢。使用中心坐标(乘以上述系数)在矩阵中定位相同的巢。
  4. 使用低通滤波器去除连接巢穴的所有“管道”。
  5. 在位图上使用对比度滤镜找到嵌套的边界。
  6. 创建一个不包含嵌套的位图(即将嵌套的所有像素设置为 0)。
  7. 使用加宽单个像素的滤镜来扩大巢穴的轮廓。
  8. 将7和5的输出按位AND得到所有管道的连接点。
  9. 沿着管道观察它们如何连接巢穴
  1. Turn the matrix into a black&white bitmap
  2. Scale the matrix so that nests of size N become a single pixel (so if you look for 10x10 nests, scale by a factor of N=10).
  3. Use the remaining pixels of the output to locate the nests. Use the center coordinate (multiplied by the factor above) to locate the same nest in the matrix.
  4. Use a low-pass filter to get rid of all "pipes" that connect the nests.
  5. Find the border of the nest with a contrast filter on the bitmap.
  6. Create a bitmap which doesn't contain the nests (i.e. set all pixels of the nests to 0).
  7. Use a filter that widens single pixels to grow the outline of the nests.
  8. Bitwise AND the output of 7 and 5 to get the connection points of all pipes.
  9. Follow the pipes to see how they connect the nests
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文