繁体   English   中英

在空间的单个点插值3d数据(Python 2.7)

[英]Interpolating 3d data at a single point in space (Python 2.7)

我有一个4维的点云,其中云中的每个点都有一个位置和一个值(x,y,z,Value)。 另外,我在3d点云中有一个“特殊”点S0。 我已使用示例在云中找到相对于S0最接近的10个点。 现在,我有一个numpy数组,用于10个最接近的点及其值。 如何对这10个点进行插值,以找到点S0处的插值? 示例代码如下所示:

import numpy as np
import matplotlib.pyplot as plt

numpoints = 20
linexs = 320
lineys = 40
linezs = 60
linexe = 20
lineye = 20
lineze = 0

# Create vectors of points
xpts = np.linspace(linexs, linexe, numpoints)
ypts = np.linspace(lineys, lineye, numpoints)
zpts = np.linspace(linezs, lineze, numpoints)
lin = np.dstack((xpts,ypts,zpts))

# Image line of points
fig = plt.figure()
ax = fig.add_subplot(211, projection='3d')
ax.set_xlim(0,365); ax.set_ylim(-85, 85); ax.set_zlim(0, 100)
ax.plot_wireframe(xpts, ypts, zpts)
ax.view_init(elev=12, azim=78)

def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) + vmin

n = 10
for n in range(21):
    xs = randrange(n, 0, 350)
    ys = randrange(n, -75, 75)
    zs = randrange(n, 0, 100)
    ax.scatter(xs, ys, zs)
dat = np.dstack((xs,ys,zs))
ax.set_xlabel('X Label')
ax.set_xlim(0,350)
ax.set_ylabel('Y Label')
ax.set_ylim(-75,75)
ax.set_zlabel('Z Label')
ax.set_zlim(0,100)

ax = fig.add_subplot(212, projection='3d')
ax.set_xlim(0,365); ax.set_ylim(-85, 85); ax.set_zlim(0, 100)
ax.plot_wireframe(xpts,ypts,zpts)
ax.view_init(elev=12, azim=78)

plt.show()
dist = []

# Calculate distance from first point to all other points in cloud
for l in range(len(xpts)):
    aaa = lin[0][0]-dat
    dist.append(np.sqrt(aaa[0][l][0]**2+aaa[0][l][1]**2+aaa[0][l][2]**2))
full = np.dstack((dat,dist))
aaa = full[0][full[0][:,3].argsort()]
print(aaa[0:10])

一个基本的例子。 请注意,插值不需要meshgrid ,仅需进行快速ufunc即可生成示例函数A=f(x,y,z) ,此处为A=x+y+z

from scipy.interpolate import interpn
import numpy as np

#make up a regular 3d grid 
X=np.linspace(-5,5,11)
Y=np.linspace(-5,5,11)
Z=np.linspace(-5,5,11)
xv,yv,zv = np.meshgrid(X,Y,Z)

# make up a function   
# see http://docs.scipy.org/doc/numpy/reference/ufuncs.html
A = np.add(xv,np.add(yv,zv))   
#this one is easy enough for us to know what to expect at (.5,.5,.5)

# usage : interpn(points, values, xi, method='linear', bounds_error=True, fill_value=nan) 
interpn((X,Y,Z),A,[0.5,0.5,0.5])

输出:

array([ 1.5])

如果您传递兴趣点数组,它将给您多个答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM