Article directory
Euclidean distance transformation
edt
, that is Euclidean distance transform.
, the Euclidean distance transform. For a binary matrix AAA,元素 a ∈ A a\in A a∈A,则 edt ( a ) \operatorname{edt}(a) edt ( a ) toaaThe shortest distance from a to the 0 element in the matrix. Suppose there is a matrix
A = [ 0 1 1 1 1 0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 0 1 1 0 0 ] A = \begin{bmatrix} 0&1&1&1&1\\ 0&0&1&1&1\\ 0&1&1&1&1\\ 0&1&1&1&0\\ 0&1&1&0&0 \end{bmatrix} A= 0000010111111111111011100
记 B = edt ( a ) B=\operatorname{edt}(a) B=edt ( a ) , then
- For A 11 A_{11}A11As far as it is 0, the corresponding B 11 = 0 B_{11}=0B11=0
- A 12 = 1 A_{12}=1 A12=1 , distanceA 12 A_{12}A12The nearest 0 is in A 11 A_{11}A11和A 22 A_{22}A22, the interval is 1, so B 12 = 1 B_{12}=1B12=1
- A 13 = 1 A_{13}=1 A13=1 , distanceA 13 A_{13}A13The nearest 0 is in A 22 A_{22}A22, since it is in the lower left corner, the distance is 1 + 1 = 2 \sqrt{1+1}=\sqrt{2}1+1=2, ie B 12 = 2 B_{12}=\sqrt{2}B12=2
By analogy, get
B = [ 0. 1. 2 5 3. 0. 0. 1. 2. 2. 0. 1. 2 2 1. 0. 1. 2 1. 0. 0. 1. 1. 0. 0. ] B = \begin{bmatrix} 0.&1.&\sqrt{2}&\sqrt{5}&3.\\ 0.&0.&1.&2.&2.\\ 0.&1.&\sqrt{2}&\; sqrt{2}&1.\\ 0.&1.&\sqrt{2}&1.&0.\\ 0.&1.&1.&0.&0 \end{bmatrix}B= 0.0.0.0.0.1.0.1.1.1.21.221.52.21.0.3.2.1.0.0.
distance_transform_edt
In scipy.ndimage
, distance_transform_edt
the Euclidean transformation can be computed,
from scipy import ndimage
import numpy as np
a = np.array(([0,1,1,1,1],
[0,0,1,1,1],
[0,1,1,1,1],
[0,1,1,1,0],
[0,1,1,0,0]))
ndimage.distance_transform_edt(a)
'''
array([[0. , 1. , 1.41421356, 2.23606798, 3. ],
[0. , 0. , 1. , 2. , 2. ],
[0. , 1. , 1.41421356, 1.41421356, 1. ],
[0. , 1. , 1.41421356, 1. , 0. ],
[0. , 1. , 1. , 0. , 0. ]])
'''
Its full definition is
distance_transform_edt(input, sampling=None, return_distances=True, return_indices=False, distances=None, indices=None)
The meaning of its parameters is
sampling
Grid spacing, equivalent to BBB multiplied by a factorreturn_distances
True
Returns the distance matrix whenreturn_indices
True
Returns the feature transformation matrix whendistances, indices
Arrays for passing parameters by pointer, don’t worry
Other distance transformation functions
scipy.ndimage
Besides edt
, two other distance transformation functions are provided
istance_transform_bf(input, metric='euclidean', sampling=None, return_distances=True, return_indices=False, distances=None, indices=None)
distance_transform_cdt(input, metric='chessboard', return_distances=True, return_indices=False, distances=None, indices=None)
Compared with the two edt
, there are more metric
functions, bf
three of which are optional euclidean, taxicab, chessboard
, and cdt
there is no euclidean
option. The other difference between the two is mainly the algorithm used.
These three different metric
schemes used in calculating the distance are different
euclidean
That is, the aforementioned Euclidean distancechessboard
will count the diagonal distance as 1, not 2 \sqrt22taxicab
Similar to Manhattan distance, does not handle diagonals
The difference between the two is as follows
>>> ndimage.distance_transform_bf(a,'chessboard')
array([[0, 1, 1, 2, 3],
[0, 0, 1, 2, 2],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 1, 1, 0, 0]], dtype=uint32)
>>> ndimage.distance_transform_bf(a,'taxicab')
array([[0, 1, 2, 3, 3],
[0, 0, 1, 2, 2],
[0, 1, 2, 2, 1],
[0, 1, 2, 1, 0],
[0, 1, 1, 0, 0]], dtype=uint32)
>>>