scipy.ndimage.

shift#

scipy.ndimage.shift(input, shift, output=None, order=3, mode='constant', cval=0.0, prefilter=True)[原始碼]#

移動陣列。

陣列會使用所請求階數的 spline 插值進行移動。輸入邊界外的點會根據給定的模式填滿。

參數:
inputarray_like

輸入陣列。

shiftfloat 或 sequence

沿軸的位移量。如果為 float,則 shift 對於每個軸都相同。如果為 sequence,則 shift 應包含每個軸的一個值。

outputarray 或 dtype,選用

放置輸出的陣列,或傳回陣列的 dtype。預設會建立與輸入相同 dtype 的陣列。

orderint,選用

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

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

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,選用

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

prefilterbool,選用

決定是否在插值之前使用 spline_filter 預先篩選輸入陣列。預設值為 True,如果 order > 1,則會建立篩選值的暫時 float64 陣列。如果將此設定為 False,則如果 order > 1,輸出會稍微模糊,除非輸入已預先篩選,也就是它是呼叫原始輸入上的 spline_filter 的結果。

傳回:
shiftndarray

移動後的輸入。

另請參閱

affine_transform

仿射轉換

註解

對於複數值 input,此函數會獨立移動實部和虛部。

在 1.6.0 版本中新增:新增複數值支援。

範例

匯入必要的模組和範例影像。

>>> from scipy.ndimage import shift
>>> import matplotlib.pyplot as plt
>>> from scipy import datasets
>>> image = datasets.ascent()

垂直移動影像 20 像素。

>>> image_shifted_vertically = shift(image, (20, 0))

垂直移動影像 -200 像素,水平移動 100 像素。

>>> image_shifted_both_directions = shift(image, (-200, 100))

繪製原始影像和移動後的影像。

>>> 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(image)
>>> top.set_title("Original image")
>>> middle.imshow(image_shifted_vertically)
>>> middle.set_title("Vertically shifted image")
>>> bottom.imshow(image_shifted_both_directions)
>>> bottom.set_title("Image shifted in both directions")
>>> fig.tight_layout()
../../_images/scipy-ndimage-shift-1.png