當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy ndimage.distance_transform_edt用法及代碼示例

本文簡要介紹 python 語言中 scipy.ndimage.distance_transform_edt 的用法。

用法:

scipy.ndimage.distance_transform_edt(input, sampling=None, return_distances=True, return_indices=False, distances=None, indices=None)#

精確的歐幾裏得距離變換。

此函數通過將每個前景(非零)元素替換為其到背景(任何 zero-valued 元素)的最短距離來計算輸入的距離變換。

除了距離變換,還可以計算特征變換。在這種情況下,最接近每個前景元素的背景元素的索引在單獨的數組中返回。

參數

input array_like

輸入要轉換的數據。可以是任何類型,但會轉換為二進製:輸入等於 True 的地方為 1,其他地方為 0。

sampling 浮點數,或浮點數序列,可選

沿每個維度的元素間距。如果是一個序列,其長度必須等於輸入等級;如果是單個數字,則用於所有軸。如果未指定,則暗示統一的網格間距。

return_distances 布爾型,可選

是否計算距離變換。默認為真。

return_indices 布爾型,可選

是否計算特征變換。默認為假。

distances float64 ndarray,可選

一個輸出數組,用於存儲計算的距離變換,而不是返回它。 return_distances 必須為真。它必須與輸入的形狀相同。

indices int32 ndarray,可選

一個輸出數組,用於存儲計算的特征變換,而不是返回它。 return_indicies 必須為真。它的形狀必須是 (input.ndim,) + input.shape。

返回

distances float64 ndarray,可選

計算出的距離變換。僅當 return_distances 為 True 且未提供距離時才返回。它將具有與輸入數組相同的形狀。

indices int32 ndarray,可選

計算的特征變換。對於輸入的每個維度,它都有一個input-shaped 數組。請參見下麵的示例。僅當 return_indices 為 True 且未提供索引時返回。

注意

歐幾裏得距離變換給出歐幾裏得距離的值:

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

其中 b[i] 是與輸入點 x[i] 的歐幾裏得距離最小的背景點(值 0),n 是維數。

例子

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

沿 x 采樣 2 個單位,沿 y 采樣 1 個:

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

還要求索引:

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

為就地輸出提供數組:

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

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.ndimage.distance_transform_edt。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。