任意轮廓f(长度)的平滑样条线表示->设 [英] Smooth spline representation of an arbitrary contour, f(length) --> x,y

查看:93
本文介绍了任意轮廓f(长度)的平滑样条线表示->设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一组x,y坐标来标记沿轮廓的点.有没有一种方法可以建立轮廓的样条曲线表示,可以在其特定位置沿其长度求值并恢复插值的x,y坐标?

Suppose I have a set of x,y coordinates that mark points along contour. Is there a way that I can build a spline representation of the contour that I can evaluate at a particular position along its length and recover interpolated x,y coordinates?

X和Y值之间通常不是1:1对应,因此单变量样条曲线对我不利.双变量样条曲线会很好,但是据我所知,在scipy.interpolate中用于评估双变量样条曲线的所有函数都采用x,y值并返回z,而我需要给出z并返回x,y(因为x,y是直线上的点,每个z都映射到唯一的x,y).

It is often not the case that there is a 1:1 correspondence between X and Y values, so univariate splines are no good to me. Bivariate splines would be fine, but as far as I can tell all of the functions for evaluating bivariate splines in scipy.interpolate take x,y values and return z, whereas I need to give z and return x,y (since x,y are points on a line, each z maps to a unique x,y).

以下是我想做的事的草图:

Here's a sketch of what I'd like to be able to do:

import numpy as np
from matplotlib.pyplot import plot

# x,y coordinates of contour points, not monotonically increasing
x = np.array([ 2.,  1.,  1.,  2.,  2.,  4.,  4.,  3.])
y = np.array([ 1.,  2.,  3.,  4.,  2.,  3.,  2.,  1.])

# f: X --> Y might not be a 1:1 correspondence
plot(x,y,'-o')

# get the cumulative distance along the contour
dist = [0]
for ii in xrange(x.size-1):
    dist.append(np.sqrt((x[ii+1]-x[ii])**2 + (y[ii+1]-y[ii])**2))
d = np.array(dist)

# build a spline representation of the contour
spl = ContourSpline(x,y,d)

# resample it at smaller distance intervals
interp_d = np.linspace(d[0],d[-1],1000)
interp_x,interp_y = spl(interp_d)

推荐答案

您要使用参数样条线,您可以在其中设置新参数t,而不是根据x值对y进行插值,并使用单变量样条从t的值内插yx.如何为每个点分配t值会影响结果,如您的问题所示,使用距离可能是一个好主意:

You want to use a parametric spline, where instead of interpolating y from the x values, you set up a new parameter, t, and interpolate both y and x from the values of t, using univariate splines for both. How you assign t values to each point affects the result, and using distance, as your question suggest, may be a good idea:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

x = np.array([ 2.,  1.,  1.,  2.,  2.,  4.,  4.,  3.])
y = np.array([ 1.,  2.,  3.,  4.,  2.,  3.,  2.,  1.])
plt.plot(x,y, label='poly')

t = np.arange(x.shape[0], dtype=float)
t /= t[-1]
nt = np.linspace(0, 1, 100)
x1 = scipy.interpolate.spline(t, x, nt)
y1 = scipy.interpolate.spline(t, y, nt)
plt.plot(x1, y1, label='range_spline')

t = np.zeros(x.shape)
t[1:] = np.sqrt((x[1:] - x[:-1])**2 + (y[1:] - y[:-1])**2)
t = np.cumsum(t)
t /= t[-1]
x2 = scipy.interpolate.spline(t, x, nt)
y2 = scipy.interpolate.spline(t, y, nt)
plt.plot(x2, y2, label='dist_spline')

plt.legend(loc='best')
plt.show()

这篇关于任意轮廓f(长度)的平滑样条线表示->设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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