median_filter#
- scipy.ndimage.median_filter(input, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0, *, axes=None)[原始碼]#
計算多維中值濾波器。
- 參數:
- inputarray_like
輸入陣列。
- size純量或元組,可選
請參閱下方的 footprint。如果已指定 footprint,則忽略此參數。
- footprint陣列,可選
必須定義 size 或 footprint 其中之一。size 指定從輸入陣列中提取的形狀,在每個元素位置,以定義濾波器函數的輸入。footprint 是一個布林陣列,用於(隱含地)指定形狀,以及此形狀內的哪些元素將傳遞給濾波器函數。因此,
size=(n,m)
等效於footprint=np.ones((n,m))
。我們會調整 size 以符合輸入陣列的維度數量,因此,如果輸入陣列的形狀為 (10,10,10),且 size 為 2,則實際使用的尺寸為 (2,2,2)。當指定 footprint 時,size 會被忽略。- output陣列或 dtype,可選
放置輸出的陣列,或返回陣列的 dtype。預設情況下,將建立與輸入相同 dtype 的陣列。
- mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}, 可選
mode 參數決定輸入陣列如何在其邊界之外延伸。預設值為 ‘reflect’。每個有效值的行為如下:
- ‘reflect’ (d c b a | a b c d | d c b a)
輸入透過反射最後一個像素的邊緣來延伸。此模式有時也稱為半樣本對稱。
- ‘constant’ (k k k k | a b c d | k k k k)
輸入透過以相同的常數值填充邊緣之外的所有值來延伸,常數值由 cval 參數定義。
- ‘nearest’ (a a a a | a b c d | d d d d)
輸入透過複製最後一個像素來延伸。
- ‘mirror’ (d c b | a b c d | c b a)
輸入透過反射最後一個像素的中心來延伸。此模式有時也稱為全樣本對稱。
- ‘wrap’ (a b c d | a b c d | a b c d)
輸入透過環繞到相反的邊緣來延伸。
為了與內插函數保持一致性,也可以使用以下模式名稱:
- ‘grid-mirror’
這是 ‘reflect’ 的同義詞。
- ‘grid-constant’
這是 ‘constant’ 的同義詞。
- ‘grid-wrap’
這是 ‘wrap’ 的同義詞。
- cval純量,可選
如果 mode 為 ‘constant’,則填充輸入邊緣之外的值。預設值為 0.0。
- origin整數或序列,可選
控制濾波器在輸入陣列像素上的放置位置。值 0(預設值)將濾波器置於像素中心,正值將濾波器向左移動,負值向右移動。透過傳遞與輸入陣列維度數量相等的 origin 序列,可以沿每個軸指定不同的偏移。
- axes整數元組或 None,可選
如果為 None,則沿所有軸對 input 進行濾波。否則,沿指定的軸對 input 進行濾波。當指定 axes 時,用於 size、origin 和/或 mode 的任何元組都必須與 axes 的長度相符。這些元組中的第 i 個條目對應於 axes 中的第 i 個條目。
- 返回:
- median_filterndarray
已濾波的陣列。具有與 input 相同的形狀。
註解
對於 dtype 為
uint8
、float32
或float64
的 2 維影像,專用函數scipy.signal.medfilt2d
可能更快。但它僅限於cval=0
的 constant 模式。範例
>>> from scipy import ndimage, datasets >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.gray() # show the filtered result in grayscale >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = datasets.ascent() >>> result = ndimage.median_filter(ascent, size=20) >>> ax1.imshow(ascent) >>> ax2.imshow(result) >>> plt.show()