[英]How does the scipy distance_transform_edt function work?
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html 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. 我无法理解欧几里德距离变换函数在Scipy中是如何工作的。 From what I understand, it is different than the Matlab function (bwdist).
据我所知,它与Matlab函数(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: scipy.ndimage.distance_transform_edt函数返回相同的数组:
[[ 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: 但matlab函数返回:
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. 这更有意义,因为它将“距离”返回到最近的那个。
It is not clear from the docstring, but distance_transform_edt
computes the distance from non-zero (ie non-background) points to the nearest zero (ie background) point. 从文档字符串中不清楚,但
distance_transform_edt
计算从非零(即非背景)点到最近的零(即背景)点的距离。
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)
(ie invert the foreground and background): 您可以通过将
distance_transform_edt()
应用于np.logical_not(a)
(即反转前景和背景)来获得相当于Matlab的bwdist(a)
):
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 has already explained how distance_transform_edt
works. 沃伦已经解释了
distance_transform_edt
工作原理。 In your case,you could change sampling units along x and y 在您的情况下,您可以沿x和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.]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.