简体   繁体   English

给定一组在(X,Y,Z)坐标中定义的点,在任意(X,Y)处插值Z值

[英]Given a set of points defined in (X, Y, Z) coordinates, interpolate Z-value at arbitrary (X, Y)

Given a set of points in (X, Y, Z) coordinates that are points on a surface, I would like to be able to interpolate Z-values at arbitrary (X, Y) coordinates. 给定(X,Y,Z)坐标中的一组点是表面上的点,我希望能够在任意(X,Y)坐标处插值Z值。 I've found some success using mlab.griddata for interpolating values on a grid, but I want to be able to call a general use function for any (X, Y) coordinate. 我发现使用mlab.griddata在网格上插值可以获得一些成功,但我希望能够为任何(X,Y)坐标调用通用函数。

The set of points form a roughly hemispherical surface. 这组点形成一个大致半球形的表面。 To simplify the problem, I am trying to write a method that interpolates values between known points of the hemisphere defined by the x, y, and z coordinates below. 为了简化问题,我试图编写一种方法,在下面的x,y和z坐标定义的半球的已知点之间插值。 Although there is an analytical solution to find z = f(x, y) for a perfect sphere, such that you don't have to interpolate, the actual set of points will not be a perfect sphere, so we should assume that we need to interpolate values at unknown (X, Y) coordinates. 虽然有一个分析解决方案可以找到完美球体的z = f(x,y),这样你就不需要插值,实际的点集将不是一个完美的球体,所以我们应该假设我们需要在未知(X,Y)坐标处插值。 Link to IPython notebook with point data 使用点数据链接到IPython笔记本

resolution = 10
u = np.linspace(-np.pi / 2, np.pi / 2, resolution)
v = np.linspace(0, np.pi, resolution)

U, V = np.meshgrid(u, v)

xs = np.sin(U) * np.cos(V) 
ys = np.sin(U) * np.sin(V)
zs = np.cos(U)

I have been using scipy.interpolate.interp2d , which "returns a function whose call method uses spline interpolation to find the value of new points." 我一直在使用scipy.interpolate.interp2d ,它“返回一个函数,其调用方法使用样条插值来查找新点的值。”

def polar(xs, ys, zs, resolution=10):
    rs = np.sqrt(np.multiply(xs, xs) + np.multiply(ys, ys))
    ts = np.arctan2(ys, xs)
    func = interp2d(rs, ts, zs, kind='cubic')
    vectorized = np.vectorize(func)

    # Guesses
    ri = np.linspace(0, rs.max(), resolution)
    ti = np.linspace(0, np.pi * 2, resolution)

    R, T = np.meshgrid(ri, ti)
    Z = vectorized(R, T)
    return R * np.cos(T), R * np.sin(T), Z

Unfortunately I get pretty weird results, similarly to another StackOverflow user who tried to use interp2d . 不幸的是,我得到了非常奇怪的结果,类似于另一个尝试使用interp2d的 StackOverflow 用户

极地结果

The most success I have found thus far is using inverse squares to estimate values of Z at (X, Y). 到目前为止我发现的最大成功是使用反平方来估计(X,Y)处的Z值。 But the function is not perfect for estimating values of Z near Z=0. 但该函数对于估计Z = 0附近的Z值并不完美。

逆平方结果

What can I do to get a function z = f(x, y) given a set of points in (x, y, z)? 在给定(x,y,z)中的一组点的情况下,我能做些什么才能得到函数z = f(x, y) )? Am I missing something here... do I need more than a point cloud to reliably estimate a value on a surface? 我在这里遗漏了什么......我是否需要更多的点云来可靠地估算表面上的值?

EDIT: 编辑:

This is the function that I ended up writing. 这是我写完的功能。 The function takes input arrays of xs, ys, zs and interpolates at x, y using scipy.interpolate.griddata , which does not require a regular grid. 该函数使用scipy.interpolate.griddatax, y处获取xs, ys, zs输入数组和插值,这不需要常规网格。 I'm sure there is a smarter way to do this and would appreciate any updates, but it works and I'm not concerned with performance. 我确信有一种更聪明的方法可以做到这一点,并希望任何更新,但它的工作原理,我不关心性能。 Including a snippet in case it helps anyone in the future. 包括一个片段,以防将来帮助任何人。

def interpolate(x, y, xs, ys, zs):
    r = np.sqrt(x*x + y*y)
    t = np.arctan2(y, x)

    rs = np.sqrt(np.multiply(xs, xs) + np.multiply(ys, ys))
    ts = np.arctan2(ys, xs)

    rs = rs.ravel()
    ts = ts.ravel()
    zs = zs.ravel()

    ts = np.concatenate((ts - np.pi * 2, ts, ts + np.pi * 2))
    rs = np.concatenate((rs, rs, rs))
    zs = np.concatenate((zs, zs, zs))


    Z = scipy.interpolate.griddata((rs, ts), zs, (r, t))
    Z = Z.ravel()
    R, T = np.meshgrid(r, t)
    return Z

You're saying that you've tried using griddata . 你说你尝试过使用griddata So why was that not working? 那为什么那不起作用? griddata also works if the new points are not regularly spaced. 如果新点没有规则间隔, griddata也可以工作。 For example, 例如,

# Definitions of xs, ys and zs
nx, ny = 20, 30
x = np.linspace(0, np.pi, nx)
y = np.linspace(0, 2*np.pi, ny)

X,Y = np.meshgrid(x, y)

xs = X.reshape((nx*ny, 1))
ys = Y.reshape((nx*ny, 1))

## Arbitrary definition of zs
zs = np.cos(3*xs/2.)+np.sin(5*ys/3.)**2

## new points where I want the interpolations
points = np.random.rand(1000, 2)

import scipy.interpolate
zs2 = scipy.interpolate.griddata(np.hstack((xs, ys)), zs, points)

Is this not what you are after? 这不是你追求的吗?

If I understand your question, you have points xs , ys , zs that are defined by 如果我理解你的问题,你有由x定义的点xsyszs

xs = np.sin(U) * np.cos(V) 
ys = np.sin(U) * np.sin(V)
zs = np.cos(U)

What you want is to be able to interpolate and find a z-value for a given x and y? 你想要的是能够插入并找到给定x和y的z值? Why do you need interpolation? 你为什么需要插值? The above equations represent a sphere, they can be rewritten as xs*xs + ys*ys + zs*zs = 1 , so there is an easy analytical solution to this problem: 上面的等式代表一个球体,它们可以重写为xs*xs + ys*ys + zs*zs = 1 ,因此这个问题有一个简单的解析解:

def Z(X, Y):
    return np.sqrt(1-X**2-Y**2)
    ## or return -np.sqrt(1-X**2-Y**2) since this equation has two solutions

unless I misunderstood the question. 除非我误解了这个问题。

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

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