16

輪郭に沿ってポイントをマークする x、y 座標のセットがあるとします。その長さに沿った特定の位置で評価し、補間された x、y 座標を復元できる輪郭のスプライン表現を作成する方法はありますか?

X 値と Y 値が 1:1 で対応することはあまりないため、一変量スプラインは私には適していません。二変量スプラインは問題ありませんが、二変量スプラインを評価するためのすべての関数を伝えることができる限り、scipy.interpolatex、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

パラメトリック スプラインを使用する場合、値yから補間する代わりにx、新しいパラメータ を設定し、との値から両方を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 に答える