5

我正在將同事IDL代碼重寫爲python,並且提出了一些我感到困惑的差異。根據我發現的其他SO問題和郵件列表線程,如果您使用scipy.ndimage.interpolation.map_coordinates並指定order=1它應該執行雙線性插值。當比較IDL代碼(在GDL中運行)和python(map_coordinates)之間的結果時,我得到了不同的結果。然後我嘗試使用mpl_toolkits.basemap.interp,並得到了與IDL代碼相同的結果。下面是一個簡單的例子,顯示了什麼是錯的。有人可以幫我弄清楚我在做什麼map_coordinatesorder=1不是雙線性的嗎?Scipy map_coordinates雙線性插值與interp和IDL插值相比

from scipy.ndimage.interpolation import map_coordinates 
from mpl_toolkits.basemap import interp 
import numpy 

in_data = numpy.array([[ 25.89125824, 25.88840675],[ 25.90930748, 25.90640068]], dtype=numpy.float32) 

map_coordinates(in_data, [[0.0],[0.125]], order=1, mode='nearest') 
# map_coordinates results in "25.89090157" 
interp(in_data, numpy.array([0,1]), numpy.array([0,1]), numpy.array([0.0]), numpy.array([0.125]), order=1) 
# interp results in "25.89351439", same as GDL's "25.8935" when printed 

我完全沒有使用interp,但我很好奇,爲什麼map_coordinates沒有返回相同的結果。我注意到map_coordinates文檔沒有提到雙線性,它實際上是雙線性的嗎?我錯過了什麼?

回答

7

使用map_coordinates時,由於數組的形狀爲(height, width),所以需要轉置數組或將座標更改爲(y,x)格式。

from scipy.ndimage.interpolation import map_coordinates 
from mpl_toolkits.basemap import interp 
import numpy 

in_data = numpy.array([[ 25.89125824, 25.88840675],[ 25.90930748, 25.90640068]], dtype=numpy.float32) 

print map_coordinates(in_data.T, [[0.0],[0.125]], order=1, mode='nearest') 
print interp(in_data, numpy.array([0,1]), numpy.array([0,1]), numpy.array([0.0]), numpy.array([0.125]), order=1) 

這將輸出:

[ 25.89351463] 
[ 25.89351439] 
+0

哇,我不能相信我沒有想到這一點。我以爲我試過了,非常感謝。 – daveydave400 2013-02-25 03:06:54