python 贝塞尔曲线_python – 使用多段三次贝塞尔曲线和距离以及曲率约束逼近数据...

我找到了满足我的criterea的解决方案.解决方案是首先找到近似于最小二乘意义的点的B样条,然后将该样条转换为多段贝塞尔曲线. B样条确实具有以下优点:与贝塞尔曲线相比,它们不会通过控制点以及提供指定近似曲线的期望“平滑度”的方式.生成这样的样条线所需的功能在FITPACK库中实现,scipy为其提供了python绑定.让我假设我将数据读入列表x和y,然后我可以这样做:

import matplotlib.pyplot as plt

import numpy as np

from scipy import interpolate

tck,u = interpolate.splprep([x,y],s=3)

unew = np.arange(0,1.01,0.01)

out = interpolate.splev(unew,tck)

plt.figure()

plt.plot(x,y,out[0],out[1])

plt.show()

结果如下所示:

如果我想让曲线更平滑,那么我可以将s参数增加到splprep.如果我希望近似值更接近数据,我可以减小s参数,以减少平滑度.通过以编程方式遍历多个参数,我可以找到符合给定要求的良好参数.

但问题是如何将该结果转换为贝塞尔曲线. Zachary Pincus在this email年的答案.我将在这里复制他的解决方案,以完整回答我的问题:

def b_spline_to_bezier_series(tck,per = False):

"""Convert a parametric b-spline into a sequence of Bezier curves of the same degree.

Inputs:

tck : (t,c,k) tuple of b-spline knots,coefficients,and degree returned by splprep.

per : if tck was created as a periodic spline,per *must* be true,else per *must* be false.

Output:

A list of Bezier curves of degree k that is equivalent to the input spline.

Each Bezier curve is an array of shape (k+1,d) where d is the dimension of the

space; thus the curve includes the starting point,the k-1 internal control

points,and the endpoint,where each point is of d dimensions.

"""

from fitpack import insert

from numpy import asarray,unique,split,sum

t,k = tck

t = asarray(t)

try:

c[0][0]

except:

# I can't figure out a simple way to convert nonparametric splines to

# parametric splines. Oh well.

raise TypeError("Only parametric b-splines are supported.")

new_tck = tck

if per:

# ignore the leading and trailing k knots that exist to enforce periodicity

knots_to_consider = unique(t[k:-k])

else:

# the first and last k+1 knots are identical in the non-periodic case,so

# no need to consider them when increasing the knot multiplicities below

knots_to_consider = unique(t[k+1:-k-1])

# For each unique knot,bring it's multiplicity up to the next multiple of k+1

# This removes all continuity constraints between each of the original knots,# creating a set of independent Bezier curves.

desired_multiplicity = k+1

for x in knots_to_consider:

current_multiplicity = sum(t == x)

remainder = current_multiplicity%desired_multiplicity

if remainder != 0:

# add enough knots to bring the current multiplicity up to the desired multiplicity

number_to_insert = desired_multiplicity - remainder

new_tck = insert(x,new_tck,number_to_insert,per)

tt,cc,kk = new_tck

# strip off the last k+1 knots,as they are redundant after knot insertion

bezier_points = numpy.transpose(cc)[:-desired_multiplicity]

if per:

# again,ignore the leading and trailing k knots

bezier_points = bezier_points[k:-k]

# group the points into the desired bezier curves

return split(bezier_points,len(bezier_points) / desired_multiplicity,axis = 0)

所以B-Splines,FITPACK,numpy和scipy救了我的一天:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 是一种流行的编程语言,可以用于实现各种机器人任务,包括绘制曲线曲线是一种基于数学计算的曲线,有着良好的平滑性和逼真度。 在 Python 中实现四阶曲线,我们可以使用一些库和函数来简化计算过程。一种常用的方法是使用 matplotlib 库的 bezier 模块。 首先,我们需要引入必要的库。我们可以使用以下命令进行安装: ``` pip install matplotlib ``` 然后,我们可以创建一个 Python 脚本,导入所需的库: ```python import matplotlib.pyplot as plt import numpy as np from matplotlib.path import Path from matplotlib.patches import PathPatch ``` 接下来,定义四个控制点的坐标(P0,P1,P2,P3),并使用这些点绘制曲线: ```python P0 = (1, 1) P1 = (2, 3) P2 = (4, -1) P3 = (6, 2) vertices = np.array([P0, P1, P2, P3]) codes = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4] path = Path(vertices, codes) patch = PathPatch(path, facecolor='none', lw=2) fig, ax = plt.subplots() ax.add_patch(patch) ax.set_xlim(0, 7) ax.set_ylim(-2, 4) plt.show() ``` 以上代码将在图形窗口中显示出绘制的曲线。 最后,我们可以根据需要调整控制点的坐标,并使用相应的函数计算曲线。这样,我们就可以绘制出不同形状和弯曲程度的曲线。 总之,Python 提供了很多方便的工具和库,可以轻松实现机器人任务,包括绘制曲线。希望这个简短的回答能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值