In numpy I have a 2d array of 1s and 0s. 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.
e.g.
a=np.array(
[[1,1,0],
[1,0,0],
[1,0,0]])
I need b to look like this:
array([[0,0,1],
[0,1,1.41],
[0,1,2]])
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; check out this SO question for more details. The short answer is to usescipy.ndimage.morphology.distance_transform_edt.