Python中3D势场的平滑/插值

时间:2019-11-15 13:56:36

标签: python numpy multidimensional-array scipy 3d

我正在研究有障碍物的配置空间中的路径规划问题。我用势场建模,其中障碍物是从表面出现的峰值。我想对这些峰进行插值,以便更顺利地表示障碍物。我尝试使用scipy griddata函数,但是它给了我这个:(参见第二张图片)。请问有人有主意吗? Uneven surface interpolated surface

这是我的代码:

#map
nx = 40
ny = 40
x = np.linspace(0,5,nx)
y = np.linspace(0,5,ny)
X,Y = np.meshgrid(x,y)

#data to plot
U = np.zeros((nx,ny))
points = []
for i in range(nx):
    for j in range(ny):
        U[i][j] = potential(X[i][j],Y[i][j],gx,gy,obstacle,mu)
        points.append([i,j])

#interpolation
xnew ,ynew = np.linspace(0,5,2*nx), np.linspace(0,5,2*ny)
Xnew, Ynew = np.meshgrid(xnew,ynew)
values = np.reshape(U,(1600,))
Z = griddata(points, values, (Xnew, Ynew), method='linear')

#plot
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(Xnew, Ynew, Z, cmap=cm.coolwarm,linewidth=0, antialiased=False)
fig2 = plt.figure()
ax = fig2.gca(projection='3d')
surf = ax.plot_surface(X, Y, U, cmap=cm.coolwarm,linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

1 个答案:

答案 0 :(得分:0)

如果您确信此数据点不是异常值,则可以使用内插函数内置函数SciPy lib。在此链接下可以找到示例:

[3d样条插值] [1] custom image

请注意,SciPy非常慢。根据某些随机数据集(512x512x110)的差异,根据第2条引用答复可能很重要:

  

SciPy的解决方案:982毫秒;您的cython解决方案:24.7ms

Spline interpolation in 3D in python

相关问题