当前位置: 首页 > 工具软件 > EDT > 使用案例 >

scipy.ndimage.distance_transform_edt

刘才俊
2023-12-01

cipy.ndimage.distance_transform_edt(input, sampling=None, return_distances=True, return_indices=False, distances=None, indices=None)[source]
Exact euclidean distance transform.

In addition to the distance transform, the feature transform can be calculated. In this case the index of the closest background element is returned along the first axis of the result.
除了距离变换外,还可以计算特征变换。在这种情况下,将沿结果的第一个轴返回最近背景元素的索引。

Parameters:
input : array_like

Input data to transform. Can be any type but will be converted into binary: 1 wherever input equates to True, 0 elsewhere.
输入要转换的数据。可以是任何类型,但将转换为二进制:输入等于True的地方为1,其他地方为0。

sampling : float or int, or sequence of same, optional 浮点或整型,或相同的序列,可选

Spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied.
沿每个维度的元素间距。如果是序列,则长度必须等于输入秩;如果只有一个数字,则该数字用于所有轴。如果未指定,则表示栅格间距为单位间距。

return_distances : bool, optional

Whether to return distance matrix. At least one of return_distances/return_indices must be True. Default is True.
是否返回距离矩阵。返回距离/返回索引中必须至少有一个为真。默认值为True。

return_indices : bool, optional

Whether to return indices matrix. Default is False.

distances: ndarray, optional

Used for output of distance array, must be of type float64.

indices: ndarray, optional

Used for output of indices, must be of type int32.

Returns:
distance_transform_edt : ndarray or list of ndarrays

Either distance matrix, index matrix, or a list of the two, depending on return_x flags and distance and indices input parameters.

Notes

The euclidean distance transform gives values of the euclidean distance:

         n
y_i = sqrt(sum (x[i]-b[i])**2)
              i

where b[i] is the background point (value 0) with the smallest Euclidean distance to input points x[i], and n is the number of dimensions.
Examples

>>> from scipy import ndimage
>>> 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.4142,  2.2361,  3.    ],
      ​[ 0.    ,  0.    ,  1.    ,  2.    ,  2.    ],
      ​[ 0.    ,  1.    ,  1.4142,  1.4142,  1.    ],
      ​[ 0.    ,  1.    ,  1.4142,  1.    ,  0.    ],
      ​[ 0.    ,  1.    ,  1.    ,  0.    ,  0.    

With a sampling of 2 units along x, 1 along y:

>>> ndimage.distance_transform_edt(a, sampling=[2,1])
array([[ 0.    ,  1.    ,  2.    ,  2.8284,  3.6056],
       [ 0.    ,  0.    ,  1.    ,  2.    ,  3.    ],
       [ 0.    ,  1.    ,  2.    ,  2.2361,  2.    ],
       [ 0.    ,  1.    ,  2.    ,  1.    ,  0.    ],
       [ 0.    ,  1.    ,  1.    ,  0.    ,  0.    ]])

Asking for indices as well:

>>> edt, inds = ndimage.distance_transform_edt(a, return_indices=True)
>>> inds
array([[[0, 0, 1, 1, 3],
        [1, 1, 1, 1, 3],
        [2, 2, 1, 3, 3],
        [3, 3, 4, 4, 3],
        [4, 4, 4, 4, 4]],
       [[0, 0, 1, 1, 4],
        [0, 1, 1, 1, 4],
        [0, 0, 1, 4, 4],
        [0, 0, 3, 3, 4],
        [0, 0, 3, 3, 4]]])

With arrays provided for inplace outputs:

>>> indices = np.zeros(((np.ndim(a),) + a.shape), dtype=np.int32)
>>> ndimage.distance_transform_edt(a, return_indices=True, indices=indices)
array([[ 0.    ,  1.    ,  1.4142,  2.2361,  3.    ],
       [ 0.    ,  0.    ,  1.    ,  2.    ,  2.    ],
       [ 0.    ,  1.    ,  1.4142,  1.4142,  1.    ],
       [ 0.    ,  1.    ,  1.4142,  1.    ,  0.    ],
       [ 0.    ,  1.    ,  1.    ,  0.    ,  0.    ]])
>>> indices
array([[[0, 0, 1, 1, 3],
        [1, 1, 1, 1, 3],
        [2, 2, 1, 3, 3],
        [3, 3, 4, 4, 3],
        [4, 4, 4, 4, 4]],
       [[0, 0, 1, 1, 4],
        [0, 1, 1, 1, 4],
        [0, 0, 1, 4, 4],
        [0, 0, 3, 3, 4],
        [0, 0, 3, 3, 4]]])

参考:https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.ndimage.distance_transform_edt.html

 类似资料:

相关阅读

相关文章

相关问答