While Matlab bwdist returns the distance to the nearest non-zero cell, Python distance_transform_edt returns the distance "to the nearest background element". The SciPy documentation is not clear about what it considers to be the “background”; there are some type conversion mechanisms behind it; in practice, 0 is the background; non-zero is the foreground.
So, if we have a matrix a :
>>> a = np.array(([0,1,0,0,0], [1,0,0,0,0], [0,0,0,0,1], [0,0,0,0,0], [0,0,1,0,0]))
then to calculate the same result we need to replace them with zeros and zeros by ones, for example. consider the matrix 1-a :
>>> a array([[0, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]]) >>> 1 - a array([[1, 0, 1, 1, 1], [0, 1, 1, 1, 1], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 0, 1, 1]])
In this case, scipy.ndimage.morphology.distance_transform_edt gives the expected results:
>>> distance_transform_edt(1-a) array([[ 1. , 0. , 1. , 2. , 2. ], [ 0. , 1. , 1.41421356, 1.41421356, 1. ], [ 1. , 1.41421356, 2. , 1. , 0. ], [ 2. , 1.41421356, 1. , 1.41421356, 1. ], [ 2. , 1. , 0. , 1. , 2. ]])