scipy.ndimage.

morphological_gradient#

scipy.ndimage.morphological_gradient(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0, *, axes=None)[原始碼]#

多維形態學梯度。

形態學梯度計算為輸入影像的膨脹與侵蝕,使用給定的結構元素兩者之差。

參數:
inputarray_like

計算形態學梯度的陣列。

sizetuple of ints

用於數學形態學運算的平面且完整結構元素的形狀。若有提供 footprintstructure 則為選用項。較大的 size 會產生更模糊的梯度。

footprintarray of ints, optional

用於形態學運算的平面結構元素中非無限元素的位置。較大的 footprint 會產生更模糊的形態學梯度。

structurearray of ints, optional

用於形態學運算的結構元素。structure 可以是非平面結構元素。structure 陣列將偏移量應用於鄰域中的像素(偏移量在膨脹期間是加法的,在侵蝕期間是減法的)

outputarray, optional

可用於儲存形態學梯度輸出的陣列。

mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}, optional

mode 參數決定如何處理陣列邊界,其中 cval 是當 mode 等於 ‘constant’ 時的值。預設值為 ‘reflect’

cvalscalar, optional

mode 為 ‘constant’,則用於填充輸入邊緣外的值。預設值為 0.0。

originscalar, optional

origin 參數控制濾波器的位置。預設值為 0

axestuple of int or None

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

返回:
morphological_gradientndarray

input 的形態學梯度。

註解

對於平面結構元素,在給定點計算的形態學梯度對應於輸入元素中,結構元素在該點為中心所涵蓋的元素之間的最大差異。

參考文獻

範例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((7,7), dtype=int)
>>> a[2:5, 2:5] = 1
>>> ndimage.morphological_gradient(a, size=(3,3))
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 0, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> # The morphological gradient is computed as the difference
>>> # between a dilation and an erosion
>>> ndimage.grey_dilation(a, size=(3,3)) -\
...  ndimage.grey_erosion(a, size=(3,3))
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 0, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> a = np.zeros((7,7), dtype=int)
>>> a[2:5, 2:5] = 1
>>> a[4,4] = 2; a[2,3] = 3
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 3, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 2, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.morphological_gradient(a, size=(3,3))
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 1, 3, 3, 3, 1, 0],
       [0, 1, 3, 3, 3, 1, 0],
       [0, 1, 3, 2, 3, 2, 0],
       [0, 1, 1, 2, 2, 2, 0],
       [0, 1, 1, 2, 2, 2, 0],
       [0, 0, 0, 0, 0, 0, 0]])