當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy ndimage.geometric_transform用法及代碼示例

本文簡要介紹 python 語言中 scipy.ndimage.geometric_transform 的用法。

用法:

scipy.ndimage.geometric_transform(input, mapping, output_shape=None, output=None, order=3, mode='constant', cval=0.0, prefilter=True, extra_arguments=(), extra_keywords={})#

應用任意幾何變換。

給定的映射函數用於為輸出中的每個點找到輸入中的相應坐標。這些坐標處的輸入值由請求順序的樣條插值確定。

參數

input array_like

輸入數組。

mapping {callable, scipy.LowLevelCallable}

一個可調用對象,它接受長度等於輸出數組秩的元組,並將相應的輸入坐標作為長度等於輸入數組秩的元組返回。

output_shape 整數元組,可選

形狀元組。

output 數組或數據類型,可選

放置輸出的數組,或返回數組的 dtype。默認情況下,將創建一個與輸入具有相同 dtype 的數組。

order 整數,可選

樣條插值的階數,默認為 3。階數必須在 0-5 範圍內。

mode {‘reflect’、‘grid-mirror’、‘constant’、‘grid-constant’、‘nearest’、‘mirror’、‘grid-wrap’、‘wrap’},可選

模式參數確定輸入數組如何擴展到其邊界之外。默認為‘constant’。每個有效值的行為如下(請參閱其他圖表和詳細信息邊界模式):

‘reflect’ (d c b a | a b c d | d c b a)

通過反射最後一個像素的邊來擴展輸入。此模式有時也稱為half-sample 對稱模式。

‘grid-mirror’

這是‘reflect’ 的同義詞。

‘constant’ (k k k k | a b c d |呸呸呸呸)

通過使用 cval 參數定義的相同常量值填充邊之外的所有值來擴展輸入。在輸入邊之外不執行插值。

‘grid-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 對稱模式。

‘grid-wrap’ (a b c d | a b c d | A B C D)

通過環繞到相對邊來擴展輸入。

‘wrap’ (d b c d | a b c d | b c a b)

輸入通過環繞到相反的邊來擴展,但是以某種方式使最後一個點和初始點完全重疊。在這種情況下,沒有很好地定義在重疊點將選擇哪個樣本。

cval 標量,可選

如果模式為‘constant’,則填充過去輸入邊的值。默認值為 0.0。

prefilter 布爾型,可選

確定輸入數組是否經過預過濾scipy.ndimage.spline_filter插值之前。默認為 True,這將創建一個臨時浮點數64過濾值數組 if訂單 > 1.如果將此設置為 False,則輸出會稍微模糊,如果訂單 > 1, 除非輸入是預過濾的,即它是調用的結果scipy.ndimage.spline_filter在原始輸入上。

extra_arguments 元組,可選

傳遞給映射的額外參數。

extra_keywords 字典,可選

傳遞給映射的額外關鍵字。

返回

output ndarray

過濾後的輸入。

注意

此函數還接受具有以下簽名並包裝在 scipy.LowLevelCallable 中的低級回調函數:

int mapping(npy_intp *output_coordinates, double *input_coordinates,
            int output_rank, int input_rank, void *user_data)
int mapping(intptr_t *output_coordinates, double *input_coordinates,
            int output_rank, int input_rank, void *user_data)

調用函數迭代輸出數組的元素,在每個元素處調用回調函數。當前輸出元素的坐標通過 output_coordinates 傳遞。回調函數必須返回必須在 input_coordinates 中對輸入進行插值的坐標。輸入和輸出數組的等級分別由input_rankoutput_rank 給出。 user_data 是按原樣提供給 scipy.LowLevelCallable 的數據指針。

回調函數必須返回一個整數錯誤狀態,如果出錯則為零,否則為一。如果發生錯誤,您通常應該在返回之前使用信息性消息設置 Python 錯誤狀態,否則調用函數會設置默認錯誤消息。

此外,還接受其他一些低級函數指針規範,但這些規範僅用於向後兼容,不應在新代碼中使用。

對於complex-valued 輸入,此函數獨立地轉換實部和虛部。

例子

>>> import numpy as np
>>> from scipy.ndimage import geometric_transform
>>> a = np.arange(12.).reshape((4, 3))
>>> def shift_func(output_coords):
...     return (output_coords[0] - 0.5, output_coords[1] - 0.5)
...
>>> geometric_transform(a, shift_func)
array([[ 0.   ,  0.   ,  0.   ],
       [ 0.   ,  1.362,  2.738],
       [ 0.   ,  4.812,  6.187],
       [ 0.   ,  8.263,  9.637]])
>>> b = [1, 2, 3, 4, 5]
>>> def shift_func(output_coords):
...     return (output_coords[0] - 3,)
...
>>> geometric_transform(b, shift_func, mode='constant')
array([0, 0, 0, 1, 2])
>>> geometric_transform(b, shift_func, mode='nearest')
array([1, 1, 1, 1, 2])
>>> geometric_transform(b, shift_func, mode='reflect')
array([3, 2, 1, 1, 2])
>>> geometric_transform(b, shift_func, mode='wrap')
array([2, 3, 4, 1, 2])

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.ndimage.geometric_transform。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。