16

https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html

I'm having trouble understanding how the Euclidean distance transform function works in Scipy. From what I understand, it is different than the Matlab function (bwdist). As an example, for the input:

[[ 0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.]]

The scipy.ndimage.distance_transform_edt function returns the same array:

[[ 0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.]]

But the matlab function returns this:

1.4142    1.0000    1.4142    2.2361    3.1623
1.0000         0    1.0000    2.0000    2.2361
1.4142    1.0000    1.4142    1.0000    1.4142
2.2361    2.0000    1.0000         0    1.0000
3.1623    2.2361    1.4142    1.0000    1.4142

which makes more sense, as it is returning the "distance" to the nearest one.

user135237
  • 389
  • 1
  • 3
  • 12
  • FYI, the scipy source code links to [this paper](http://ieeexplore.ieee.org.proxy.lib.fsu.edu/stamp/stamp.jsp?arnumber=1177156). – Matt Hancock Nov 01 '17 at 01:22

2 Answers2

25

It is not clear from the docstring, but distance_transform_edt computes the distance from non-zero (i.e. non-background) points to the nearest zero (i.e. background) point.

For example:

In [42]: x
Out[42]: 
array([[0, 0, 0, 0, 0, 1, 1, 1],
       [0, 1, 1, 1, 0, 1, 1, 1],
       [0, 1, 1, 1, 0, 1, 1, 1],
       [0, 0, 1, 1, 0, 0, 0, 1]])

In [43]: np.set_printoptions(precision=3)  # Easier to read the result with fewer digits.

In [44]: distance_transform_edt(x)
Out[44]: 
array([[ 0.   ,  0.   ,  0.   ,  0.   ,  0.   ,  1.   ,  2.   ,  3.   ],
       [ 0.   ,  1.   ,  1.   ,  1.   ,  0.   ,  1.   ,  2.   ,  2.236],
       [ 0.   ,  1.   ,  1.414,  1.   ,  0.   ,  1.   ,  1.   ,  1.414],
       [ 0.   ,  0.   ,  1.   ,  1.   ,  0.   ,  0.   ,  0.   ,  1.   ]])

You can get the equivalent of Matlab's bwdist(a) by applying distance_transform_edt() to np.logical_not(a) (i.e. invert the foreground and background):

In [71]: a
Out[71]: 
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

In [72]: distance_transform_edt(np.logical_not(a))
Out[72]: 
array([[ 1.414,  1.   ,  1.414,  2.236,  3.162],
       [ 1.   ,  0.   ,  1.   ,  2.   ,  2.236],
       [ 1.414,  1.   ,  1.414,  1.   ,  1.414],
       [ 2.236,  2.   ,  1.   ,  0.   ,  1.   ],
       [ 3.162,  2.236,  1.414,  1.   ,  1.414]])
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • how could I use the resulting matrix to calculate a ratio of distances? – mLstudent33 Oct 20 '19 at 16:57
  • 1
    @mLstudent33, that's not a question for the comments. Create a new question, showing what you are trying to do, what you've tried so far, etc. (i.e. follow all the usual guidelines for a good question). – Warren Weckesser Oct 20 '19 at 18:25
2

Warren has already explained how distance_transform_edt works. In your case,you could change sampling units along x and y

ndimage.distance_transform_edt(a)
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

But

>>> ndimage.distance_transform_edt(a, sampling=[2,2])
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  2.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  2.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

Or

ndimage.distance_transform_edt(a, sampling=[3,3])
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  3.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  3.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
MishaVacic
  • 1,812
  • 8
  • 25
  • 29