用python平滑线的路径 [英] smooth the path of line with python

查看:43
本文介绍了用python平滑线的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组点,它们一起创建了一个轨道,其中顺序至关重要.我可以使用线条绘制轨迹,如何在获取新点之后或同时对其进行平滑处理?曲目可能看起来像 1 张图片:

I have a set of points, all together they create a track, where the sequence is crucial. I can plot the track using lines, how could I smooth it after or while new points are fetching? Track might look like 1 pic:

图片一

图二

图三

最后我想要的是2张照片.我试图用 scipy.interpolate 进行插值,但是没有用,因为它需要排序序列(我最后只实现了 pic3)

2 picture is what I want to have in the end. I tried to interpolate with scipy.interpolate but it didn't work, because it requires sorted sequence (I only achieved pic3 in the end)

推荐答案

听起来不同的插值方法或方法可能会得到您想要的结果.三次样条将使您在顶点处获得更直的曲线,正如 scipy 库和此循环点示例集所使用的:

It sounds like a different interpolation method or approach might get what you want. A cubic spline will get you straighter lines with curves at the vertices, as utilized from the scipy libary and this sample set of loop points:

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

arr = np.array([[0,0],[2,.5],[2.5, 1.25],[2.6,2.8],[1.3,1.1]])
x, y = zip(*arr)
#in this specific instance, append an endpoint to the starting point to create a closed shape
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]
#create spline function
f, u = interpolate.splprep([x, y], s=0, per=True)
#create interpolated lists of points
xint, yint = interpolate.splev(np.linspace(0, 1, 100), f)
plt.scatter(x, y)
plt.plot(xint, yint)
plt.show()

原始直线如下所示:

这篇关于用python平滑线的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆