从不规则点集开始对3D曲面中的Z值进行插值[关闭]
问题内容:
已关闭 。这个问题需要更加集中。它当前不接受答案。
想改善这个问题吗? 更新问题,使其仅通过编辑此帖子来关注一个问题。
4年前关闭。
我有一个看起来像图A的表面,想象一下它是顶视图。表面已计算出Z值。
现在,我需要在如图B所示的新点中找到所有Z值。该怎么做?我试过了,scipy.interpolate.interp2d
但给出了一些奇怪的结果:
我只想在“图”中为自定义x和y查找自定义z。
最小代码示例
func_int = scipy.interpolate.interp2d([point[0] for point in pointsbottom],[point[1] for point in pointsbottom],[point[2] for point in pointsbottom], kind = 'linear')
pointscaption = map(lambda point:(point[0],point[1],func_int(point[0],point[1])),pointscaption)
其中pointsbottom
(x,y,z)的列表是(x,y,z)pointscaption
的列表,但我需要找到新的z。
问题答案:
尝试改用griddata:
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
区别在于griddata希望将常规数据作为输入(嗯,我认为)。并不是说您应该有不同的结果,而是更多,您将能够更快地发现问题。您可以轻松屏蔽“常规网格”数据。
我的第一个猜测是这些输入坐标不是您期望的(可能与您正在计算的函数的比例不同),但是如果不进行测试就很难说。
在任何情况下,似乎都需要一个表面,根据定义,该表面是网格数据的一种,因此使用此不同的框架很容易发现问题。
编辑(关于海报问题的进一步考虑):
假设您想要一些对象,您想要在其中输入一些数据。完成此操作后,您希望能够使用该数据估算任何位置。为此,您可以构建这样的类:
import numpy as np
class Estimation():
def __init__(self,datax,datay,dataz):
self.x = datax
self.y = datay
self.v = dataz
def estimate(self,x,y,using='ISD'):
"""
Estimate point at coordinate x,y based on the input data for this
class.
"""
if using == 'ISD':
return self._isd(x,y)
def _isd(self,x,y):
d = np.sqrt((x-self.x)**2+(y-self.y)**2)
if d.min() > 0:
v = np.sum(self.v*(1/d**2)/np.sum(1/d**2))
return v
else:
return self.v[d.argmin()]
本示例使用反平方距离方法,该方法对于估计非常稳定(如果避免除以零)。它不会很快,但我希望它是可以理解的。从这一点开始,您可以通过执行以下操作来估算2D空间中的任意点:
e = Estimation(datax,datay,dataz)
newZ = e.estimate(30,55) # the 30 and 55 are just example coordinates
如果要对整个网格执行此操作:
datax,datay = np.random.randint(0,100,10),np.random.randint(0,100,10)
dataz = datax/datay
e = Estimation(datax,datay,dataz)
surf = np.zeros((100,100))
for i in range(100):
for j in range(100):
surf[i,j] = e.estimate(i,j)
您将使用例如matplotlib(其中颜色代表表面的高度)获得可以看到的图像:
import matplotlib.pyplot as plt
plt.imshow(surf.T,origin='lower',interpolation='nearest')
plt.scatter(datax,datay,c=dataz,s=90)
plt.show()
该实验的结果是:
如果您不想使用ISD(平方反比),只需在Estimation类上实现一个新方法。这是你想要的?