代码之家  ›  专栏  ›  技术社区  ›  djhoese Sanzhar Dan

Scipy map_cordinates双线性插值与interp和IDL插值相比

  •  5
  • djhoese Sanzhar Dan  · 技术社区  · 13 年前

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

    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 ,但我很好奇为什么 地图坐标 没有返回相同的结果。我注意到 地图坐标 文档中没有提到双线性,它实际上是双线性的吗?我错过了什么?

    1 回复  |  直到 13 年前
        1
  •  7
  •   Joe Kington    13 年前

    使用时 map_coordinates ,您需要转置数组或将坐标更改为(y,x)格式,因为数组的形状为 (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]