我想在一组500个随机点上拟合一个三次贝塞尔曲线。适合立方贝塞尔曲线的最小二乘法
下面是我对贝塞尔曲线代码:
import numpy as np
from scipy.misc import comb
def bernstein_poly(i, n, t):
"""
The Bernstein polynomial of n, i as a function of t
"""
return comb(n, i) * (t**(n-i)) * (1 - t)**i
def bezier_curve(points, nTimes=1000):
nPoints = len(points)
x = np.array([p[0] for p in points])
y = np.array([p[1] for p in points])
t = np.linspace(0.0, 1.0, nTimes)
polynomial_array = np.array([ bernstein_poly(i, nPoints-1, t) for i in range(0, nPoints) ])
xvals = np.dot(x, polynomial_array)
yvals = np.dot(y, polynomial_array)
return xvals, yvals
if __name__ == "__main__":
from matplotlib import pyplot as plt
nPoints = 4
points = np.random.rand(nPoints,2)*200
xpoints = [p[0] for p in points]
ypoints = [p[1] for p in points]
xvals, yvals = bezier_curve(points, nTimes=1000)
plt.plot(xvals, yvals)
plt.plot(xpoints, ypoints, "ro")
for nr in range(len(points)):
plt.text(points[nr][0], points[nr][1], nr)
plt.show()
我知道,与NumPy和SciPy的有最小二乘法:numpy.linalg.lstsq和scipy.optimize.least_squares
但我不知道我怎么能用它们来拟合500点的曲线。有人可以提供一些帮助吗?
谢谢
什么部分数据证明使用贝塞尔曲线,而不是更适合噪音数据拟合的东西?因为在某人回答你的问题之前,你应该能够回答为什么你认为你需要贝塞尔曲线而不是更合适的东西。 –
在这种情况下,我特别使用Bezier曲线,因为我按照书中的说明进行操作,并且希望实施该操作。我知道有更好的选择,但在这个特定的例子中,我需要使用贝塞尔曲线。 – stepp0
这表明你的问题还没有完全诚实。我确定说出你刚才在文章中所说的内容,并说*哪本书是不必要的信息的反面。 –