scipy.ndimage.

binary_dilation#

scipy.ndimage.binary_dilation(input, structure=None, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False, *, axes=None)[原始碼]#

使用給定的結構元素進行多維二值膨脹。

參數:
input類陣列

要膨脹的二值類陣列。非零 (True) 元素形成要膨脹的子集。

structure類陣列,選填

用於膨脹的結構元素。非零元素被視為 True。如果未提供結構元素,則會生成一個具有等於 1 的方形連通性的元素。

iterationsint,選填

膨脹重複 iterations 次(預設為一次)。如果迭代次數小於 1,則重複膨脹直到結果不再改變。僅接受整數的迭代次數。

mask類陣列,選填

如果給定遮罩,則在每次迭代中僅修改在對應遮罩元素處具有 True 值的元素。

outputndarray,選填

與輸入形狀相同的陣列,輸出將放置在其中。預設情況下,將建立一個新陣列。

border_valueint (轉換為 0 或 1),選填

輸出陣列中邊界的值。

originint 或 int 元組,選填

濾波器的位置,預設為 0。

brute_forceboolean,選填

記憶體條件:如果為 False,則僅追蹤在上次迭代中值發生變化的像素,作為當前迭代中要更新(膨脹)的候選像素;如果為 True,則所有像素都被視為膨脹的候選像素,無論前一次迭代中發生了什麼。預設為 False。

axesint 元組或 None

要對其應用濾波器的軸。如果為 None,則沿所有軸對 input 進行濾波。如果提供了 origin 元組,則其長度必須與軸數相符。

回傳:
binary_dilation布林值的 ndarray

輸入通過結構元素的膨脹結果。

註解

膨脹 [1] 是一種數學形態學運算 [2],它使用結構元素來擴展影像中的形狀。影像通過結構元素的二值膨脹是結構元素中心位於影像的非零點內時,結構元素覆蓋的點的軌跡。

參考文獻

範例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5, 5))
>>> a[2, 2] = 1
>>> a
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> ndimage.binary_dilation(a)
array([[False, False, False, False, False],
       [False, False,  True, False, False],
       [False,  True,  True,  True, False],
       [False, False,  True, False, False],
       [False, False, False, False, False]], dtype=bool)
>>> ndimage.binary_dilation(a).astype(a.dtype)
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> # 3x3 structuring element with connectivity 1, used by default
>>> struct1 = ndimage.generate_binary_structure(2, 1)
>>> struct1
array([[False,  True, False],
       [ True,  True,  True],
       [False,  True, False]], dtype=bool)
>>> # 3x3 structuring element with connectivity 2
>>> struct2 = ndimage.generate_binary_structure(2, 2)
>>> struct2
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> ndimage.binary_dilation(a, structure=struct1).astype(a.dtype)
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> ndimage.binary_dilation(a, structure=struct2).astype(a.dtype)
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
>>> ndimage.binary_dilation(a, structure=struct1,\
... iterations=2).astype(a.dtype)
array([[ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.]])