scipy.ndimage.
grey_erosion#
- scipy.ndimage.grey_erosion(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0, *, axes=None)[source]#
計算灰階腐蝕,可使用結構元素或對應於平面結構元素的足跡。
灰階腐蝕是一種數學形態學操作。對於完整且平面的結構元素的簡單情況,它可以被視為滑動窗口上的最小值濾波器。
- 參數:
- input類陣列
計算灰階腐蝕的陣列。
- size整數元組
用於灰階腐蝕的平面且完整結構元素的形狀。如果提供了 footprint 或 structure,則為選填。
- footprint整數陣列,選填
用於灰階腐蝕的平面結構元素中非無限元素的位置。非零值給出了選擇最小值的中心鄰域集合。
- structure整數陣列,選填
用於灰階腐蝕的結構元素。structure 可以是非平面結構元素。structure 陣列為鄰域中的每個像素應用減法偏移。
- output陣列,選填
可以提供一個用於儲存腐蝕輸出的陣列。
- mode{‘reflect’,’constant’,’nearest’,’mirror’, ‘wrap’}, 選填
mode 參數決定如何處理陣列邊界,其中 cval 是當 mode 等於 ‘constant’ 時的值。預設值為 ‘reflect’
- cval純量,選填
如果 mode 為 ‘constant’,則用於填充輸入邊緣外的值。預設值為 0.0。
- origin純量,選填
origin 參數控制濾波器的位置。預設值為 0
- axes整數元組或 None
應用濾波器的軸。如果為 None,則 input 會沿所有軸進行濾波。如果提供了 origin 元組,則其長度必須與軸的數量相符。
- 回傳值:
- outputndarray
input 的灰階腐蝕。
另請參閱
註解
由在域 E 上定義的結構元素 s 對影像輸入進行的灰階腐蝕由下式給出
(input+s)(x) = min {input(y) - s(x-y), for y in E}
特別是,對於定義為當 y 在 E 中時 s(y) = 0 的結構元素,灰階腐蝕會計算由 E 定義的滑動窗口內輸入影像的最小值。
參考文獻
範例
>>> from scipy import ndimage >>> import numpy as np >>> a = np.zeros((7,7), dtype=int) >>> a[1:6, 1:6] = 3 >>> a[4,4] = 2; a[2,3] = 1 >>> a array([[0, 0, 0, 0, 0, 0, 0], [0, 3, 3, 3, 3, 3, 0], [0, 3, 3, 1, 3, 3, 0], [0, 3, 3, 3, 3, 3, 0], [0, 3, 3, 3, 2, 3, 0], [0, 3, 3, 3, 3, 3, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> ndimage.grey_erosion(a, size=(3,3)) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 3, 2, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> footprint = ndimage.generate_binary_structure(2, 1) >>> footprint array([[False, True, False], [ True, True, True], [False, True, False]], dtype=bool) >>> # Diagonally-connected elements are not considered neighbors >>> ndimage.grey_erosion(a, footprint=footprint) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 3, 1, 2, 0, 0], [0, 0, 3, 2, 2, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]])