Scipy map_coordinates双线性插值与interp和IDL插值的比较 [英] Scipy map_coordinates bilinear interpolation compared to interp and IDL interpolate

查看:175
本文介绍了Scipy map_coordinates双线性插值与interp和IDL插值的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将同事IDL代码重写为python,并提出了一些令我感到困惑的差异.根据其他SO问题和邮件列表线程,我发现如果使用scipy.ndimage.interpolation.map_coordinates并指定order=1,则应该执行双线性插值.当比较IDL代码(在GDL中运行)和python(map_coordinates)之间的结果时,我得到了不同的结果.然后,我尝试使用mpl_toolkits.basemap.interp,并且得到了与IDL代码相同的结果.下面是一个简单的示例,显示了错误所在.有人可以帮我弄清楚map_coordinates在做什么,还是order=1不是双线性的?

I'm in the process of rewriting a coworkers IDL code into python and am coming up with some differences that I'm confused about. According to other SO questions and mailing list threads I've found if you use scipy.ndimage.interpolation.map_coordinates and specify order=1 it is supposed to do bilinear interpolation. When comparing results between the IDL code (run in GDL) and python (map_coordinates) I got different results. Then I tried using mpl_toolkits.basemap.interp and I got the same result as the IDL code. Below is a simple example that shows what is wrong. Could someone help me figure out what I am doing wrong with map_coordinates or is order=1 not bilinear?

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文档没有提到双线性,实际上是双线性吗?我想念什么?

I am perfectly fine using interp, but I was curious why map_coordinates didn't return the same result. I noticed that the map_coordinates documentation doesn't mention bilinear, is it actually bilinear? What am I missing?

推荐答案

使用map_coordinates时,您需要转置数组或将坐标更改为(y,x)格式,因为数组的形状为(height, width)

When use map_coordinates, you need transpose the array or change you coordinates to (y, x) format, because the shape of the array is (height, width).

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]

这篇关于Scipy map_coordinates双线性插值与interp和IDL插值的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆