简体   繁体   English

识别2D numpy数组中的连续区域

[英]Identify contiguous regions in 2D numpy array

I have a large numpy array that I've applied a filter over. 我有一个大的numpy数组,我已经应用了一个过滤器。 I'd like to identify the contiguous regions in this masked array. 我想识别这个蒙版数组中的连续区域。 Here I'm defining a region to be contiguous if, for any index (x1,y1) to any other index (x2,y2) , they belong to the same region if there is a path of True values along equal integer steps along the axes (diagonals are valid steps). 在这里,如果对于任何索引(x1,y1)与任何其他索引(x2,y2) ,如果沿着相等的整数步长存在一个True值的路径,则它们属于同一个区域,这里我定义了一个连续的区域。轴(对角线是有效的步骤)。

That may not be as clear as a simple picture. 这可能不如简单图片那么清晰。 Given the mask: 鉴于面具:

0010000
0100000
0110000
0000011
1000010

There should be three regions identified such that the output is something like 应该确定三个区域,使输出类似

[ [[0,2],[1,1],[2,1],[2,2]], [[3,5],[3,6],[4,5]], [[4,0]] ]

I'd like to use something built into numpy , without resorting to writing my own Flood Fill algorithm. 我想使用内置于numpy ,而无需编写自己的Flood Fill算法。 A little bit of research in the docs only turned up a 1D version of what I'm asking. 文档中的一些研究只发现了我要问的一维版本

You're looking for scipy.ndimage.label , more info here . 您正在寻找scipy.ndimage.label这里有更多信息。 label returns an array the same shape as the input where each "unique feature has a unique value", so if you want the indices of the features you can do something like: label返回一个与输入形状相同的数组,其中每个“唯一特征具有唯一值”,因此如果您想要这些特征的索引,您可以执行以下操作:

labels, numL = label(array)
label_indices = [(labels == i).nonzero() for i in xrange(1, numL+1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM