scipy.ndimage.
binary_closing#
- scipy.ndimage.binary_closing(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False, *, axes=None)[原始碼]#
使用給定的結構元素進行多維二值閉運算。
輸入影像通過結構元素的閉運算是影像通過結構元素的膨脹的侵蝕。
- 參數:
- inputarray_like
要進行閉運算的類陣列二值資料。非零 (True) 元素形成要進行閉運算的子集。
- structurearray_like, optional
用於閉運算的結構元素。非零元素被視為 True。如果未提供結構元素,則會生成一個連通性等於 1 的方形元素(即,只有最近鄰元素連接到中心,對角連接的元素不被視為鄰居)。
- iterationsint, optional
閉運算的膨脹步驟,然後是侵蝕步驟,各自重複 iterations 次(預設為一次)。如果 iterations 小於 1,則每個運算都會重複執行,直到結果不再改變。僅接受整數的 iterations。
- outputndarray, optional
與輸入形狀相同的陣列,輸出放置於其中。預設情況下,會建立一個新陣列。
- originint 或 int 元組, optional
濾波器的放置位置,預設為 0。
- maskarray_like, optional
如果給定遮罩,則在每次迭代中僅修改遮罩元素上具有 True 值的那些元素。
在 1.1.0 版本中新增。
- border_valueint (轉換為 0 或 1), optional
輸出陣列邊界的值。
在 1.1.0 版本中新增。
- brute_forceboolean, optional
記憶體條件:如果為 False,則僅追蹤上次迭代中值發生變化的像素,作為目前迭代中要更新的候選對象;如果為 true,則所有像素都被視為更新的候選對象,無論前一次迭代中發生了什麼。預設為 False。
在 1.1.0 版本中新增。
- axestuple of int 或 None
應用濾波器的軸。如果為 None,則沿所有軸對 input 進行濾波。如果提供了 origin 元組,則其長度必須與軸的數量相符。
- 返回:
- binary_closingbool 陣列
輸入通過結構元素的閉運算結果。
註解
閉運算 [1] 是一種數學形態學運算 [2],包含使用相同的結構元素對輸入進行膨脹和侵蝕的連續操作。因此,閉運算會填補小於結構元素的孔洞。
與開運算 (
binary_opening
) 一起,閉運算可用於去除雜訊。參考文獻
範例
>>> from scipy import ndimage >>> import numpy as np >>> a = np.zeros((5,5), dtype=int) >>> a[1:-1, 1:-1] = 1; a[2,2] = 0 >>> a array([[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]]) >>> # Closing removes small holes >>> ndimage.binary_closing(a).astype(int) 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]]) >>> # Closing is the erosion of the dilation of the input >>> ndimage.binary_dilation(a).astype(int) array([[0, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0, 1, 1, 1, 0]]) >>> ndimage.binary_erosion(ndimage.binary_dilation(a)).astype(int) 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]])
>>> a = np.zeros((7,7), dtype=int) >>> a[1:6, 2:5] = 1; a[1:3,3] = 0 >>> a array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> # In addition to removing holes, closing can also >>> # coarsen boundaries with fine hollows. >>> ndimage.binary_closing(a).astype(int) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0]])