Python 计算网格上的光滑二元样条线

Python 计算网格上的光滑二元样条线,python,scipy,interpolation,Python,Scipy,Interpolation,我有三列非结构化数据,希望对它们进行二元样条拟合。我还不能很好地使用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

我有三列非结构化数据,希望对它们进行二元样条拟合。我还不能很好地使用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)
这将为一个点提供插值,但如果注释掉最后一行的第二行,则不起作用。如果有人能指导我如何在网格上进行样条插值,我将不胜感激。提前感谢。

方法
ev(x,y)
要求
x
y
为一维阵列。 在代码中,
grid\u x
grid\u y
是二维的

您可以尝试以下方法:

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

我一直在与这样的事情斗争,最终解决了它。对我来说,关键是使用凹凸的linspace创建两个1D栅格,一个用于x,一个用于y。并在网格上使用
\uuuuu call\uuuuu
从进行计算

在下面的脚本中,我计算一条样条曲线或两个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()

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