【问题标题】:Smooth spline representation of an arbitrary contour, f(length) --> x,y任意轮廓的平滑样条表示,f(length) --> x,y
【发布时间】:2023-03-04 04:54:01
【问题描述】:

假设我有一组沿轮廓标记点的 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)

【问题讨论】:

  • 我不明白你的 x 和 y 数组如何不能有 1:1 的对应关系并且仍然定义曲线上的点......你能试着解释一下你有什么吗?介意举个例子吗?
  • 尝试绘制我的示例坐标 - 在这种情况下,线曲线会回到自身,因此不会有来自 X-->Y 或 Y-->X 的唯一映射

标签: numpy scipy curve-fitting bezier spline


【解决方案1】:

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

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

【讨论】:

  • ZOMG 在 Python 中如此简单,我不得不用 Ruby 编写自己的 Catmull-Rom 实现......:/好东西
  • @Jaime 这是非常有趣的技术,谢谢!除了 dist_spline 之外,t 的其他合理值是什么?我在哪里可以阅读更多关于它的信息?由于某种原因,scipy 中没有记录样条方法:docs.scipy.org/doc/scipy-0.16.1/reference/interpolate.html
  • @baltazar 你可以阅读parametric equations 的曲线。在曲线的微分几何上下文中,曲线长度的参数化(类似于距离)具有一些特殊的属性,因此被称为natural parametrization。如果您的数据集具有正确的几何形状,则参数的另一个明显选项是围绕中心点的角度。
  • 这样一个非常简单的例子——希望我能把它装裱起来挂在墙上。现在我想知道在缩放中是否可以考虑“非共线性”,或者这是否是矫枉过正。
  • 抱歉,我收到AttributeError: module 'scipy.interpolate' has no attribute 'spline',有什么想法吗?
【解决方案2】:

这是一个使用splprep 和splev 的示例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多