将闭合曲线拟合到一组点

2024-05-04 11:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组点pts,它们形成一个循环,看起来像这样:

enter image description here

这有点类似于31243002,但是我不想把点放在点对之间,而是想通过点拟合一条平滑的曲线(坐标在问题的末尾给出),所以我尝试了类似于scipy文档的方法:

values = pts
tck = interpolate.splrep(values[:,0], values[:,1], s=1)
xnew = np.arange(2,7,0.01)
ynew = interpolate.splev(xnew, tck, der=0)

但我有个错误:

ValueError: Error on input data

有没有办法找到这样的合适人选?

点的坐标:

pts = array([[ 6.55525 ,  3.05472 ],
   [ 6.17284 ,  2.802609],
   [ 5.53946 ,  2.649209],
   [ 4.93053 ,  2.444444],
   [ 4.32544 ,  2.318749],
   [ 3.90982 ,  2.2875  ],
   [ 3.51294 ,  2.221875],
   [ 3.09107 ,  2.29375 ],
   [ 2.64013 ,  2.4375  ],
   [ 2.275444,  2.653124],
   [ 2.137945,  3.26562 ],
   [ 2.15982 ,  3.84375 ],
   [ 2.20982 ,  4.31562 ],
   [ 2.334704,  4.87873 ],
   [ 2.314264,  5.5047  ],
   [ 2.311709,  5.9135  ],
   [ 2.29638 ,  6.42961 ],
   [ 2.619374,  6.75021 ],
   [ 3.32448 ,  6.66353 ],
   [ 3.31582 ,  5.68866 ],
   [ 3.35159 ,  5.17255 ],
   [ 3.48482 ,  4.73125 ],
   [ 3.70669 ,  4.51875 ],
   [ 4.23639 ,  4.58968 ],
   [ 4.39592 ,  4.94615 ],
   [ 4.33527 ,  5.33862 ],
   [ 3.95968 ,  5.61967 ],
   [ 3.56366 ,  5.73976 ],
   [ 3.78818 ,  6.55292 ],
   [ 4.27712 ,  6.8283  ],
   [ 4.89532 ,  6.78615 ],
   [ 5.35334 ,  6.72433 ],
   [ 5.71583 ,  6.54449 ],
   [ 6.13452 ,  6.46019 ],
   [ 6.54478 ,  6.26068 ],
   [ 6.7873  ,  5.74615 ],
   [ 6.64086 ,  5.25269 ],
   [ 6.45649 ,  4.86206 ],
   [ 6.41586 ,  4.46519 ],
   [ 5.44711 ,  4.26519 ],
   [ 5.04087 ,  4.10581 ],
   [ 4.70013 ,  3.67405 ],
   [ 4.83482 ,  3.4375  ],
   [ 5.34086 ,  3.43394 ],
   [ 5.76392 ,  3.55156 ],
   [ 6.37056 ,  3.8778  ],
   [ 6.53116 ,  3.47228 ]])

Tags: 方法文档npscipy曲线ptsvalues末尾
3条回答

使用ROOT Framework和pyroot接口,我能够生成以下图像 Drawing Using pyroot

使用下面的代码(我将您的数据转换为一个名为data.CSV的CSV,因此将其读入根目录会更容易,并给出xp,yp的列标题)

from ROOT import TTree, TGraph, TCanvas, TH2F

c1 = TCanvas( 'c1', 'Drawing Example', 200, 10, 700, 500 )
t=TTree('TP','Data Points')
t.ReadFile('./data.csv')
t.SetMarkerStyle(8)
t.Draw("yp:xp","","ACP")
c1.Print('pydraw.png')

实际上,你的问题离解决还不远。

使用^{}进行参数B样条插值是最简单的方法。它还支持封闭曲线,如果您提供per=1参数

import numpy as np
from scipy.interpolate import splprep, splev
import matplotlib.pyplot as plt

# define pts from the question

tck, u = splprep(pts.T, u=None, s=0.0, per=1) 
u_new = np.linspace(u.min(), u.max(), 1000)
x_new, y_new = splev(u_new, tck, der=0)

plt.plot(pts[:,0], pts[:,1], 'ro')
plt.plot(x_new, y_new, 'b--')
plt.show()

enter image description here

从根本上说,这种方法与《乔·金顿的回答》中的方法没有太大区别。尽管如此,它可能会更健壮一些,因为默认情况下,选择i向量的等价项是基于点之间的距离,而不仅仅是它们的索引(请参见u参数的^{} documentation)。

你的问题是你想直接和x和y一起工作。您调用的插值函数假定x值是按顺序排列的,并且每个x值都有一个唯一的y值。

取而代之的是,你需要建立一个参数化的坐标系(例如顶点的索引)并使用它分别插值x和y。

首先,请考虑以下内容:

import numpy as np
from scipy.interpolate import interp1d # Different interface to the same function
import matplotlib.pyplot as plt

#pts = np.array([...]) # Your points

x, y = pts.T
i = np.arange(len(pts))

# 5x the original number of points
interp_i = np.linspace(0, i.max(), 5 * i.max())

xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)

fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y, 'ko')
plt.show()

enter image description here

我没有关闭多边形。如果愿意,可以将第一个点添加到数组的末尾(例如pts = np.vstack([pts, pts[0]])

如果你这样做,你会注意到在多边形闭合的地方有一个不连续点。

enter image description here

这是因为我们的参数化没有考虑到polgyon的关闭。一个快速的解决方法是用“反射”点填充阵列:

import numpy as np
from scipy.interpolate import interp1d 
import matplotlib.pyplot as plt

#pts = np.array([...]) # Your points

pad = 3
pts = np.pad(pts, [(pad,pad), (0,0)], mode='wrap')
x, y = pts.T
i = np.arange(0, len(pts))

interp_i = np.linspace(pad, i.max() - pad + 1, 5 * (i.size - 2*pad))

xi = interp1d(i, x, kind='cubic')(interp_i)
yi = interp1d(i, y, kind='cubic')(interp_i)

fig, ax = plt.subplots()
ax.plot(xi, yi)
ax.plot(x, y, 'ko')
plt.show()

enter image description here

或者,可以使用特殊的曲线平滑算法,如峰值或切角算法。

相关问题 更多 >