scipy distance_transform_edt函数如何工作? [英] How does the scipy distance_transform_edt function work?

查看:566
本文介绍了scipy distance_transform_edt函数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在理解欧几里得距离变换功能在Scipy中的工作方式时遇到了麻烦.据我了解,它与Matlab函数(bwdist)不同.例如,对于输入:

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.]]

scipy.ndimage.distance_transform_edt函数返回相同的数组:

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.]]

但是matlab函数返回以下内容:

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.

推荐答案

在文档字符串中不清楚,但是

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.

例如:

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.   ]])

通过将distance_transform_edt()应用于np.logical_not(a)(即反转前景和背景),可以获得与Matlab的bwdist(a)等效的东西:

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]])

这篇关于scipy distance_transform_edt函数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆