滑动轨迹 曲线 python_python – 计算轨迹(路径)中的转折点/枢轴点
您可以使用
Ramer-Douglas-Peucker (RDP) algorithm來簡化路徑。然后,您可以計算簡化路徑每段的方向變化。對應于方向最大變化的點可以稱為轉折點:
RDP算法的Python實現可以在on github中找到。
import matplotlib.pyplot as plt
import numpy as np
import os
import rdp
def angle(dir):
"""
Returns the angles between vectors.
Parameters:
dir is a 2D-array of shape (N,M) representing N vectors in M-dimensional space.
The return value is a 1D-array of values of shape (N-1,), with each value
between 0 and pi.
0 implies the vectors point in the same direction
pi/2 implies the vectors are orthogonal
pi implies the vectors point in opposite directions
"""
dir2 = dir[1:]
dir1 = dir[:-1]
return np.arccos((dir1*dir2).sum(axis=1)/(
np.sqrt((dir1**2).sum(axis=1)*(dir2**2).sum(axis=1))))
tolerance = 70
min_angle = np.pi*0.22
filename = os.path.expanduser('~/tmp/bla.data')
points = np.genfromtxt(filename).T
print(len(points))
x, y = points.T
# Use the Ramer-Douglas-Peucker algorithm to simplify the path
# http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
# Python implementation: https://github.com/sebleier/RDP/
simplified = np.array(rdp.rdp(points.tolist(), tolerance))
print(len(simplified))
sx, sy = simplified.T
# compute the direction vectors on the simplified curve
directions = np.diff(simplified, axis=0)
theta = angle(directions)
# Select the index of the points with the greatest theta
# Large theta is associated with greatest change in direction.
idx = np.where(theta>min_angle)[0]+1
fig = plt.figure()
ax =fig.add_subplot(111)
ax.plot(x, y, 'b-', label='original path')
ax.plot(sx, sy, 'g--', label='simplified path')
ax.plot(sx[idx], sy[idx], 'ro', markersize = 10, label='turning points')
ax.invert_yaxis()
plt.legend(loc='best')
plt.show()
以上使用兩個參數:
> RDP算法采用一個參數,公差,其中代表簡化路徑的最大距離可能偏離原來的路徑。公差越大,粗魯的路徑就越簡單。>另一個參數是min_angle,它定義了被認為是一個轉折點。 (我正在轉彎點是原始路徑上的任何一點,簡化路徑上的進入和退出向量之間的角度大于min_angle)。
總結
以上是生活随笔為你收集整理的滑动轨迹 曲线 python_python – 计算轨迹(路径)中的转折点/枢轴点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python实现knn算法鸢尾花_Pyt
- 下一篇: xpress-mp优化实例精选_量化研究