16

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

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

这是我想做的事情的草图:

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)
4

2 回答 2

29

您想使用参数样条曲线,而不是yx值进行插值,而是设置一个新参数 ,并从 的值和 的值进行t插值,对两者都使用单变量样条曲线。您如何为每个点分配值会影响结果,并且正如您的问题所建议的那样,使用距离可能是一个好主意:yxtt

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()

在此处输入图像描述

于 2013-01-15T22:42:28.830 回答
6

这是使用splprepsplev的示例:

import numpy as np
import scipy.interpolate
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 = np.sqrt((x[:-1] - x[1:])**2 + (y[:-1] - y[1:])**2)
dist_along = np.concatenate(([0], dist.cumsum()))

# build a spline representation of the contour
spline, u = scipy.interpolate.splprep([x, y], u=dist_along, s=0)

# resample it at smaller distance intervals
interp_d = np.linspace(dist_along[0], dist_along[-1], 50)
interp_x, interp_y = scipy.interpolate.splev(interp_d, spline)
plot(interp_x, interp_y, '-o')

参数样条示例

于 2016-06-10T08:11:35.653 回答