且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将闭合曲线拟合到一组点

更新时间:2022-03-17 03:49:16

实际上,您距离问题的解决方案并不远.

Actually, you were not far from the solution in your question.

使用 scipy.interpolate.splprep 对于参数B样条插值,a>是最简单的方法.如果您提供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()

从根本上讲,这种方法与@Joe Kington的答案中的方法没有太大不同.但是,它可能会更健壮,因为默认情况下,根据点之间的距离而不是简单地根据它们的索引来选择等价于i向量(请参见

Fundamentally, this approach not very different from the one in @Joe Kington's answer. Although, it will probably be a bit more robust, because the equivalent of the i vector is chosen, by default, based on the distances between points and not simply their index (see splprep documentation for the u parameter).