scipy.ndimage.

binary_opening#

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

使用給定的結構元素進行多維二值開運算。

輸入影像透過結構元素的開運算,是指影像先被結構元素腐蝕,再進行膨脹

參數:
inputarray_like

要進行開運算的二值 array_like。非零 (True) 元素構成要進行開運算的子集。

structurearray_like,選擇性

用於開運算的結構元素。非零元素被視為 True。如果沒有提供結構元素,則會產生一個具有等於 1 的正方形連通性的元素(即,只有最近鄰元素連接到中心,對角線連接的元素不被視為鄰居)。

iterationsint,選擇性

開運算的腐蝕步驟,然後膨脹步驟各自重複 iterations 次(預設為一次)。 如果 iterations 小於 1,則每個操作都會重複執行,直到結果不再改變為止。 僅接受整數的 iterations。

outputndarray,選擇性

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

originint 或 int 元組,選擇性

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

maskarray_like,選擇性

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

在 1.1.0 版本中新增。

border_valueint (轉換為 0 或 1),選擇性

輸出陣列邊界的值。

在 1.1.0 版本中新增。

brute_forceboolean,選擇性

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

在 1.1.0 版本中新增。

axesint 或 None 的元組

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

返回:
binary_opening布林值的 ndarray

輸入影像透過結構元素的開運算結果。

筆記

開運算 [1] 是一種數學形態學運算 [2],其組成是先對輸入影像進行腐蝕,然後再使用相同的結構元素進行膨脹。 因此,開運算會移除小於結構元素大小的物件。

閉運算 (binary_closing) 一起,開運算可用於去除雜訊。

參考文獻

範例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5,5), dtype=int)
>>> a[1:4, 1:4] = 1; a[4, 4] = 1
>>> a
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, 1]])
>>> # Opening removes small objects
>>> ndimage.binary_opening(a, structure=np.ones((3,3))).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]])
>>> # Opening can also smooth corners
>>> ndimage.binary_opening(a).astype(int)
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]])
>>> # Opening is the dilation of the erosion of the input
>>> ndimage.binary_erosion(a).astype(int)
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(ndimage.binary_erosion(a)).astype(int)
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]])