Python Euclidean distance transform

Euclidean distance transformation

edt, that is Euclidean distance transform., the Euclidean distance transform. For a binary matrix AAA,元素 a ∈ A a\in A aA,则 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}A11A 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.2 1.2 2 1.5 2.2 1.0.3.2.1.0.0.

distance_transform_edt

In scipy.ndimage, distance_transform_edtthe 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

  • samplingGrid spacing, equivalent to BBB multiplied by a factor
  • return_distancesTrueReturns the distance matrix when
  • return_indicesTrueReturns the feature transformation matrix when
  • distances, indicesArrays for passing parameters by pointer, don’t worry

Other distance transformation functions

scipy.ndimageBesides 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 metricfunctions, bfthree of which are optional euclidean, taxicab, chessboard, and cdtthere is no euclideanoption. The other difference between the two is mainly the algorithm used.

These three different metricschemes used in calculating the distance are different

  • euclideanThat is, the aforementioned Euclidean distance
  • chessboardwill count the diagonal distance as 1, not 2 \sqrt22
  • taxicabSimilar 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)
>>>

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/130107466