[英]How to efficiently calculate distance to nearest 1 in mask in numpy?
In numpy I have a 2d array of 1s and 0s. 在numpy我有一个1和0的2d数组。 I need to calculate a new array (same dimensions) where each element contains the distance to the nearest 1 from the corresponding point in the mask array.
我需要计算一个新的数组(相同的维度),其中每个元素包含距离掩码数组中相应点最近的1的距离。
eg 例如
a=np.array(
[[1,1,0],
[1,0,0],
[1,0,0]])
I need b to look like this: 我需要b看起来像这样:
array([[0,0,1],
[0,1,1.41],
[0,1,2]])
PS. PS。 I'll be doing this over very large arrays, so the more efficient the better!
我将在非常大的阵列上执行此操作,因此效率越高越好! Thanks!
谢谢!
You're looking for the equivalent of MATLAB's bwdist
; 你正在寻找相当于MATLAB的
bwdist
; check out this SO question for more details. 查看此SO问题了解更多详情。 The short answer is to use
scipy.ndimage.morphology.distance_transform_edt
. 简短的回答是使用
scipy.ndimage.morphology.distance_transform_edt
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.