【问题标题】:Evaluate SmoothBivariateSpline over a grid在网格上评估 SmoothBivariateSpline
【发布时间】:2013-04-25 12:23:18
【问题描述】:

我有三列非结构化数据,并希望对它们进行二元样条拟合。我对 Python 中的类还不太熟悉,所以我不明白如何做到这一点。为了显示我的问题,我编写了一个简单的代码:

#! /usr/bin/env python3

import numpy as np
from scipy import interpolate

#an array of 3 columns:
a=np.zeros((200, 3))
a[:,0]=np.random.uniform(0,1,200)
a[:,1]=np.random.uniform(3,5,200)
a[:,2]=np.random.uniform(10,12,200)

#find the boundries
min_x, max_x = np.amin(a[:,0]), np.amax(a[:,0])
min_y, max_y = np.amin(a[:,1]), np.amax(a[:,1])

#Set the resolution:
x_res=1000
y_res=int( ( (max_y-min_y) / (max_x-min_x) )*x_res )

#Make a grid
grid_x, grid_y = np.mgrid[min_x:max_x:x_res*1j, min_y:max_y:y_res*1j]

sbsp=interpolate.SmoothBivariateSpline(a[:,0], a[:,1], a[:,2])

b=sbsp.ev(4,5)
#c=sbsp.ev(grid_x, grid_y)
print(b)

这给出了一个点的插值,但如果你注释掉倒数第二行,它就不起作用。如果有人能指导我如何在网格上获得样条插值,我将不胜感激。提前致谢。

【问题讨论】:

    标签: python scipy interpolation


    【解决方案1】:

    方法ev(x,y) 要求xy 是一维数组。 在您的代码中,grid_xgrid_y 是二维的。

    您可以尝试以下方法:

    c=sbsp.ev(grid_x[0,0], grid_y[0,0])
    

    【讨论】:

      【解决方案2】:

      我一直在努力解决这样的问题,终于解决了。对我来说,关键是使用凹凸不平的 linspace 创建两个 1D 网格,一个用于 x,一个用于 y。并使用来自https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.SmoothBivariateSpline.call.html#scipy.interpolate.SmoothBivariateSpline.call.__call__ 在网格上进行评估。

      在下面的脚本中,我计算一个样条线或两个 x,y,z 数组,每个数组都作为图像输出。

      #!/bin/python3.7
      
      """
      
      Use spline interpolation to on grid of x,y,z value where z is either xdiff or ydiff for use as imagemagick 2D displacement maps
      
      """
      
      import numpy as np
      from scipy.interpolate import SmoothBivariateSpline
      from skimage import io
      
      
      # python lists of x,y dst control points and zx=xsrc-xdiff, zy=ysrc-ydiff offsets to be interpolated over full image of size 129x129
      x = [8.5, 20.5, 33.5, 48.5, 64.5, 80.5, 95.5, 109.5, 121.5, 5.5, 17.5, 31.5, 46.5, 64.5, 81.5, 97.5, 111.5, 123.5, 2.5, 14.5, 29.5, 45.5, 64.5, 83.5, 99.5, 113.5, 125.5, 1.5, 12.5, 26.5, 43.5, 64.5, 85.5, 103.5, 116.5, 127.5, 0.5, 11.5, 24.5, 41.5, 64.5, 87.5, 103.5, 117.5, 128.5, 1.5, 12.5, 25.5, 42.5, 64.5, 86.5, 103.5, 116.5, 127.5, 2.5, 14.5, 28.5, 45.5, 64.5, 83.5, 100.5, 114.5, 125.5, 5.5, 17.5, 30.5, 46.5, 64.5, 81.5, 97.5, 111.5, 123.5, 8.5, 19.5, 33.5, 48.5, 64.5, 80.5, 95.5, 109.5, 121.5]
      y = [7.5, 5.5, 3.5, 1.5, 1.5, 1.5, 3.5, 5.5, 7.5, 20.5, 16.5, 14.5, 12.5, 11.5, 12.5, 15.5, 16.5, 19.5, 33.5, 31.5, 28.5, 26.5, 24.5, 26.5, 28.5, 31.5, 33.5, 48.5, 47.5, 45.5, 42.5, 40.5, 42.5, 45.5, 46.5, 48.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 64.5, 80.5, 81.5, 83.5, 86.5, 87.5, 86.5, 83.5, 81.5, 80.5, 95.5, 97.5, 100.5, 103.5, 104.5, 102.5, 100.5, 97.5, 95.5, 109.5, 111.5, 114.5, 116.5, 117.5, 116.5, 114.5, 111.5, 109.5, 121.5, 123.5, 125.5, 127.5, 127.5, 127.5, 125.5, 123.5, 120.5]
      zx = [119.5, 123.5, 126.5, 127.5, 127.5, 127.5, 128.5, 130.5, 134.5, 122.5, 126.5, 128.5, 129.5, 127.5, 126.5, 126.5, 128.5, 132.5, 125.5, 129.5, 130.5, 130.5, 127.5, 124.5, 124.5, 126.5, 130.5, 126.5, 131.5, 133.5, 132.5, 127.5, 122.5, 120.5, 123.5, 128.5, 127.5, 132.5, 135.5, 134.5, 127.5, 120.5, 120.5, 122.5, 127.5, 126.5, 131.5, 134.5, 133.5, 127.5, 121.5, 120.5, 123.5, 128.5, 125.5, 129.5, 131.5, 130.5, 127.5, 124.5, 123.5, 125.5, 130.5, 122.5, 126.5, 129.5, 129.5, 127.5, 126.5, 126.5, 128.5, 132.5, 119.5, 124.5, 126.5, 127.5, 127.5, 127.5, 128.5, 130.5, 134.5]
      zy = [120.5, 122.5, 124.5, 126.5, 126.5, 126.5, 124.5, 122.5, 120.5, 123.5, 127.5, 129.5, 131.5, 132.5, 131.5, 128.5, 127.5, 124.5, 126.5, 128.5, 131.5, 133.5, 135.5, 133.5, 131.5, 128.5, 126.5, 127.5, 128.5, 130.5, 133.5, 135.5, 133.5, 130.5, 129.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 127.5, 126.5, 124.5, 121.5, 120.5, 121.5, 124.5, 126.5, 127.5, 128.5, 126.5, 123.5, 120.5, 119.5, 121.5, 123.5, 126.5, 128.5, 130.5, 128.5, 125.5, 123.5, 122.5, 123.5, 125.5, 128.5, 130.5, 134.5, 132.5, 130.5, 128.5, 128.5, 128.5, 130.5, 132.5, 135.5]
      
      # convert python lists to numpy arrays
      ax = np.asarray(x)
      ay = np.asarray(y)
      azx = np.asarray(zx)
      azy = np.asarray(zy)
      
      
      # define bbox of interpolated data
      # bbox=[minx, maxx, miny, maxy]
      bbox=[0, 129, 0, 129]
      
      # convert bbox to numpy array
      abbox = np.asarray(bbox)
      
      # do interpolations
      xd = SmoothBivariateSpline(ax, ay, azx, w=None, bbox=abbox, kx=3, ky=3)
      yd = SmoothBivariateSpline(ax, ay, azy, w=None, bbox=abbox, kx=3, ky=3)
      
      # define integer grid onto which to interpolate
      grid_x=np.linspace(0, 129, num=129)
      grid_y=np.linspace(0, 129, num=129)
      
      
      # evaluate at grid points
      xdisplace = xd.__call__(grid_x, grid_y, grid=True)
      ydisplace = yd.__call__(grid_x, grid_y, grid=True)
      
      # save output using skimage
      io.imsave("xdimgs.png", xdisplace.astype('uint8'))
      io.imsave("ydimgs.png", ydisplace.astype('uint8'))
      
      # view output using skimage
      io.imshow(xdisplace.astype('uint8')) 
      io.show()
      io.imshow(ydisplace.astype('uint8')) 
      io.show()
      


      也许这会对您的脚本有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-30
        • 2011-01-06
        • 1970-01-01
        相关资源
        最近更新 更多