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=None)[source]#

套用任意幾何轉換。

給定的映射函數用於找出輸出中每個點在輸入中對應的座標。輸入在這些座標的值由所請求階數的樣條插值確定。

參數:
input (輸入)array_like (類陣列)

輸入陣列。

mapping (映射){callable, scipy.LowLevelCallable}

一個可呼叫的物件,它接受一個長度等於輸出陣列秩的元組,並回傳對應的輸入座標作為一個長度等於輸入陣列秩的元組。

output_shape (輸出形狀)tuple of ints, optional (整數元組,選用)

形狀元組。

output (輸出)array or dtype, optional (陣列或 dtype,選用)

要在其中放置輸出的陣列,或回傳陣列的 dtype。預設情況下,將建立與輸入相同 dtype 的陣列。

order (階數)int, optional (整數,選用)

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

mode (模式){‘reflect’, ‘grid-mirror’, ‘constant’, ‘grid-constant’, ‘nearest’, ‘mirror’, ‘grid-wrap’, ‘wrap’}, optional (選用)

mode 參數決定輸入陣列如何在其邊界之外延伸。預設值為 ‘constant’。每個有效值的行為如下 (請參閱 邊界模式 上的其他圖表和詳細資訊)

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

輸入通過反射最後一個像素的邊緣來延伸。此模式有時也稱為半樣本對稱。

‘grid-mirror’

這是 ‘reflect’ 的同義詞。

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

輸入通過用相同的常數值填充邊緣以外的所有值來延伸,該常數值由 cval 參數定義。在輸入的邊緣之外不執行插值。

‘grid-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)

輸入通過反射最後一個像素的中心來延伸。此模式有時也稱為全樣本對稱。

‘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)

輸入通過環繞到相對邊緣來延伸,但方式是最後一個點和初始點完全重疊。在這種情況下,在重疊點選擇哪個樣本沒有明確定義。

cvalscalar, optional (純量,選用)

如果 mode 為 ‘constant’,則填充輸入邊緣過去的值。預設值為 0.0。

prefilter (預先過濾)bool, optional (布林值,選用)

確定輸入陣列是否在插值之前使用 spline_filter 進行預先過濾。預設值為 True,如果 order > 1,這將建立一個過濾值的臨時 float64 陣列。如果將此設定為 False,則如果 order > 1,除非輸入已預先過濾,即它是呼叫 spline_filter 在原始輸入上的結果,否則輸出會稍微模糊。

extra_arguments (額外參數)tuple, optional (元組,選用)

傳遞給 mapping 的額外參數。

extra_keywords (額外關鍵字)dict, optional (字典,選用)

傳遞給 mapping 的額外關鍵字。

回傳值:
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 錯誤狀態後再回傳,否則呼叫函數會設定預設錯誤訊息。

此外,還接受一些其他低階函數指標規範,但這些僅用於向後相容性,不應在新程式碼中使用。

對於複數值 input,此函數獨立轉換實部和虛部。

Added in version 1.6.0: 新增複數值支援。

範例

>>> 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])