从不规则的点集开始在3D曲面中插值Z值 [英] Interpolate Z values in a 3D surface, starting from an irregular set of points

查看:59
本文介绍了从不规则的点集开始在3D曲面中插值Z值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像图A的表面,想象一下它是顶视图.表面已计算出Z值.

I have a surface that looks like Figure A, imagine that is top view. The surface has calculated Z value.

现在,我需要在如图B所示的新点中找到所有Z值.该怎么做?我尝试了scipy.interpolate.interp2d,但它给出了一些奇怪的结果,如下所示:

Now I need to find all Z values in new points like figure B. How to do this? I tried scipy.interpolate.interp2d but it gives some weird results like this:

我只想在图"中为自定义x和y找到自定义z.

I just want to find custom z for custom x and y inside the "figure".

最小代码示例

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)的列表,而pointscaption是(x,y,z)的列表,但是我需要找到新的z.

Where pointsbottom is list of (x,y,z) and pointscaption is list of (x,y,z) but I need to find new z.

推荐答案

尝试使用网格数据代替:

    grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')

不同之处在于griddata希望将常规数据作为输入(嗯,我认为).并不是说您应该有不同的结果,而是更多,您将能够更快地发现问题.您可以轻松屏蔽常规网格"数据.

The difference is griddata expects regular data as input (hmm..., I think). It's not that you should have a different result but more that you'll be able to catch the problem faster. You can mask the "regular grid" data easily.

我的第一个猜测是这些输入坐标不是您期望的(可能与您正在计算的函数的比例不同),但是如果不进行测试就很难说.

My first guess would be that those input coordinates are not what you are expecting them to be (perhaps with a different scale from the function you are calculating) but it's difficult to say without testing.

在任何情况下,似乎都需要一个表面,顾名思义,该表面是网格数据的一种类型,因此使用这种不同的框架来发现问题应该相当容易.

In any case it seems you want a surface, which by definition is a type of grid data, so it should be fairly easy to spot the problem using this different framework.

编辑(关于发帖人的疑问的进一步考虑):

EDIT (further considerations about the doubts of the poster):

比方说,您想要一些想要在其中输入一些数据的对象.完成此操作后,您希望能够使用该数据估算任何位置.为此,您可以构建这样的类:

Let's say you want some object that you want to input some data inside. After doing this you want to be able to estimate any position using that data. For that purpose you can build a class like this:

    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空间中的任意点:

This example is using Inverse Squared Distance method which is very stable for estimation (if you avoid divisions by zero). It won't be fast but I hope it's understandable. From this point on you can estimate any point in 2D space by doing:

    e = Estimation(datax,datay,dataz)
    newZ = e.estimate(30,55) # the 30 and 55 are just example coordinates

如果要对整个网格执行此操作:

If you were to do this to an entire grid:

    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(其中颜色代表表面的高度)获得可以看到的图像:

You would obtain an image that you can see using, for instance, matplotlib (in which the color represents the height in your surface):

    import matplotlib.pyplot as plt
    plt.imshow(surf.T,origin='lower',interpolation='nearest')
    plt.scatter(datax,datay,c=dataz,s=90)
    plt.show()

这个实验的结果是这样的:

The result of this experiment is this:

如果您不想使用ISD(反平方距离),只需在Estimation类上实现一个新方法.这是您要找的吗?

If you don't want to use ISD (Inverse Squared Distance) just implement a new method on Estimation class. Is this what you are looking for?

这篇关于从不规则的点集开始在3D曲面中插值Z值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆