本文簡要介紹 python 語言中 scipy.ndimage.generic_filter
的用法。
用法:
scipy.ndimage.generic_filter(input, function, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0, extra_arguments=(), extra_keywords=None)#
使用給定函數計算多維濾波器。
在每個元素處調用提供的函數。該元素的過濾器占用空間內的輸入值作為雙精度值的一維數組傳遞給函數。
- input: array_like
輸入數組。
- function: {callable, scipy.LowLevelCallable}
應用於每個元素的函數。
- size: 標量或元組,可選
請參見下麵的足跡。如果給出足跡,則忽略。
- footprint: 數組,可選
任何一個尺寸或者腳印必須定義。尺寸給出在每個元素位置從輸入數組中獲取的形狀,以定義過濾器函數的輸入.腳印是一個布爾數組,它(隱式)指定一個形狀,而且這個形狀中的哪些元素將被傳遞給過濾器函數。因此
size=(n,m)
相當於footprint=np.ones((n,m))
.我們調整尺寸到輸入數組的維數,因此,如果輸入數組是形狀(10,10,10),並且尺寸為 2,則實際使用的大小為 (2,2,2)。什麽時候腳印給出,尺寸被忽略。- output: 數組或數據類型,可選
放置輸出的數組,或返回數組的 dtype。默認情況下,將創建一個與輸入具有相同 dtype 的數組。
- mode: {‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’},可選
mode 參數確定輸入數組如何擴展到其邊界之外。默認為‘reflect’。每個有效值的行為如下:
- ‘reflect’ (d c b a | a b c d | d c b a)
通過反射最後一個像素的邊來擴展輸入。此模式有時也稱為half-sample 對稱模式。
- ‘constant’ (k k k k | a b c d |呸呸呸呸)
通過使用 cval 參數定義的相同常量值填充邊之外的所有值來擴展輸入。
- ‘nearest’ (啊啊啊啊| a b c d |嘀嘀嘀嘀)
通過複製最後一個像素來擴展輸入。
- ‘mirror’ (d c b | a b c d | c b a)
通過反射最後一個像素的中心來擴展輸入。此模式有時也稱為whole-sample 對稱模式。
- ‘wrap’ (a b c d | a b c d | A B C D)
通過環繞到相對邊來擴展輸入。
為了與插值函數保持一致,還可以使用以下模式名稱:
- ‘grid-mirror’
這是‘reflect’ 的同義詞。
- ‘grid-constant’
這是‘constant’ 的同義詞。
- ‘grid-wrap’
這是‘wrap’ 的同義詞。
- cval: 標量,可選
如果模式為‘constant’,則填充過去輸入邊的值。默認值為 0.0。
- origin: int 或序列,可選
控製過濾器在輸入數組像素上的位置。值 0(默認值)使過濾器以像素為中心,正值將過濾器向左移動,負值向右移動。通過傳遞長度等於輸入數組維數的原點序列,可以沿每個軸指定不同的移位。
- extra_arguments: 順序,可選
傳遞給傳遞函數的額外位置參數序列。
- extra_keywords: 字典,可選
要傳遞給傳遞函數的額外關鍵字參數的字典。
- generic_filter: ndarray
過濾數組。具有與輸入相同的形狀。
參數 ::
返回 ::
注意:
此函數還接受具有以下簽名之一並包裝在
scipy.LowLevelCallable
中的低級回調函數:int callback(double *buffer, npy_intp filter_size, double *return_value, void *user_data) int callback(double *buffer, intptr_t filter_size, double *return_value, void *user_data)
調用函數迭代輸入和輸出數組的元素,在每個元素處調用回調函數。當前元素處濾波器占用空間內的元素通過
buffer
參數傳遞,占用空間內的元素數量通過filter_size
傳遞。計算值在return_value
中返回。user_data
是按原樣提供給scipy.LowLevelCallable
的數據指針。回調函數必須返回一個整數錯誤狀態,如果出錯則為零,否則為一。如果發生錯誤,您通常應該在返回之前使用信息性消息設置python錯誤狀態,否則調用函數會設置默認錯誤消息。
此外,還接受其他一些低級函數指針規範,但這些規範僅用於向後兼容,不應在新代碼中使用。
例子:
導入必要的模塊並加載用於過濾的示例圖像。
>>> import numpy as np >>> from scipy import datasets >>> from scipy.ndimage import generic_filter >>> import matplotlib.pyplot as plt >>> ascent = datasets.ascent()
通過將簡單的 NumPy 聚合函數作為參數傳遞給函數,計算內核大小為 10 的最大過濾器。
>>> maximum_filter_result = generic_filter(ascent, np.amax, [10, 10])
雖然也可以使用
maximum_filter
直接獲得最大過濾器,但generic_filter
允許使用通用 Python 函數或scipy.LowLevelCallable
作為過濾器。在這裏,我們以內核大小為 5 為例計算最大值和最小值之間的範圍。>>> def custom_filter(image): ... return np.amax(image) - np.amin(image) >>> custom_filter_result = generic_filter(ascent, custom_filter, [5, 5])
繪製原始圖像和過濾後的圖像。
>>> fig, axes = plt.subplots(3, 1, figsize=(4, 12)) >>> plt.gray() # show the filtered result in grayscale >>> top, middle, bottom = axes >>> for ax in axes: ... ax.set_axis_off() # remove coordinate system >>> top.imshow(ascent) >>> top.set_title("Original image") >>> middle.imshow(maximum_filter_result) >>> middle.set_title("Maximum filter, Kernel: 10x10") >>> bottom.imshow(custom_filter_result) >>> bottom.set_title("Custom filter, Kernel: 5x5") >>> fig.tight_layout()
相關用法
- Python SciPy ndimage.generic_filter1d用法及代碼示例
- Python SciPy ndimage.generic_laplace用法及代碼示例
- Python SciPy ndimage.generic_gradient_magnitude用法及代碼示例
- Python SciPy ndimage.generate_binary_structure用法及代碼示例
- Python SciPy ndimage.geometric_transform用法及代碼示例
- Python SciPy ndimage.grey_erosion用法及代碼示例
- Python SciPy ndimage.gaussian_laplace用法及代碼示例
- Python SciPy ndimage.grey_closing用法及代碼示例
- Python SciPy ndimage.gaussian_filter1d用法及代碼示例
- Python SciPy ndimage.gaussian_filter用法及代碼示例
- Python SciPy ndimage.grey_opening用法及代碼示例
- Python SciPy ndimage.gaussian_gradient_magnitude用法及代碼示例
- Python SciPy ndimage.grey_dilation用法及代碼示例
- Python SciPy ndimage.correlate用法及代碼示例
- Python SciPy ndimage.morphological_gradient用法及代碼示例
- Python SciPy ndimage.variance用法及代碼示例
- Python SciPy ndimage.correlate1d用法及代碼示例
- Python SciPy ndimage.binary_dilation用法及代碼示例
- Python SciPy ndimage.distance_transform_bf用法及代碼示例
- Python SciPy ndimage.find_objects用法及代碼示例
- Python SciPy ndimage.label用法及代碼示例
- Python SciPy ndimage.maximum_filter1d用法及代碼示例
- Python SciPy ndimage.iterate_structure用法及代碼示例
- Python SciPy ndimage.map_coordinates()用法及代碼示例
- Python SciPy ndimage.binary_opening用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.ndimage.generic_filter。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。