2014-03-26 31 views
13

我無法準確理解反射模式如何處理我的數組。我有這個非常簡單的陣列:scipys ndimage過濾器的「反射」模式到底有多精確?

import numpy as np 
from scipy.ndimage.filters import uniform_filter 
from scipy.ndimage.filters import median_filter 

vector = np.array([[1.0,1.0,1.0,1.0,1.0],[2.0,2.0,2.0,2.0,2.0],[4.0,4.0,4.0,4.0,4.0],[5.0,5.0,5.0,5.0,5.0]]) 

print(vector) 

[[1. 1. 1. 1. 1.] [2. 2. 2. 2. 2.] [4. 4。4. 4。 4.] [5。5. 5. 5。5.]]

施加均勻的(平均)爲3的窗口大小過濾得到以下:

filtered = uniform_filter(vector, 3, mode='reflect') 

print(filtered) 

[[1.33333333 1.33333333 1.33333333 1.33333333 1.33333333] [2.33333333 2.33333333 2.33333333 2.33333333 2.33333333] [3.66666667 3.66666667 3.66666667 3.66666667 3.66666667] [4.66666667 4.66666667 4.66666667 4.66666667 4.66666667]

如果我試圖複製手動運動我能得到這個結果。綠色的原始矩陣,橙色的窗口,並導致黃色。白色是「反映」的觀察結果。

enter image description here

結果是:

enter image description here

但是當我嘗試的4或5我失敗的窗口大小以能夠複製的結果。

filtered = uniform_filter(vector, 4, mode='reflect') 

print(filtered) 

[[1.5 1.5 1.5 1.5 1.5] [2. 2. 2. 2. 2.] [3. 3. 3. 3. 3.] [4. 4。4. 4 4]

做手工:

enter image description here

我也得到:

enter image description here

如果窗口大小均勻,窗口是如何處理的?但無論如何,如果我試圖複製5號窗口的結果並且模式反映出我不能。儘管我會認爲其行爲與尺寸3相似。

+1

在下面的圖像「手動操作:」中,沒有正確填寫擴展數組的第一行。您只需垂直複製值「1」。用'mode ='reflect'',在主(明亮的黃色)陣列上面的最上面應該有'2'。 –

+0

這正是我想知道的,你能幫我理解模式的工作原理嗎?對於窗口大小n = 2,3,...等?我會接受這個答案,我特別感興趣的是當尺寸是偶數時窗口是如何工作的。 – JEquihua

回答

33

假設一個軸上的數據爲1 2 3 4 5 6 7 8。下表顯示了數據是如何擴展爲每種模式(假定cval=0):

mode  | Ext |   Input   | Ext 
    -----------+---------+------------------------+--------- 
    'mirror' | 4 3 2 | 1 2 3 4 5 6 7 8 | 7 6 5 
    'reflect' | 3 2 1 | 1 2 3 4 5 6 7 8 | 8 7 6 
    'nearest' | 1 1 1 | 1 2 3 4 5 6 7 8 | 8 8 8 
    'constant' | 0 0 0 | 1 2 3 4 5 6 7 8 | 0 0 0 
    'wrap'  | 6 7 8 | 1 2 3 4 5 6 7 8 | 1 2 3 

對於偶窗口大小n,考慮大小n+1的窗口,然後不包括下邊緣和右邊緣。 (窗口的位置可以通過使用參數origin來更改。)

+0

所以這發生在每個維度?所以對於n = 3(正如你使用的)'反射'在第一列將是'1 1 1',左上角'1'旁邊的對角線會發生什麼? – JEquihua

+0

doc在哪裏? –

+0

我在這裏找到了文檔:http://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html –