计算轨迹(路径)中的转折点枢轴点

计算轨迹(路径)中的转折点枢轴点,第1张

计算轨迹(路径)中的转折点/枢轴点

您可以使用Ramer-Douglas-Peucker(RDP)
算法来简化路径。然后,您可以计算沿简化路径的每个线段的方向变化。与方向最大变化相对应的点可以称为转折点:

可以在github上找到RDP算法的Python实现。

import matplotlib.pyplot as pltimport numpy as npimport osimport rdpdef 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 = 70min_angle = np.pi*0.22filename = os.path.expanduser('~/tmp/bla.data')points = np.genfromtxt(filename).Tprint(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 curvedirections = 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]+1fig = 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算法采用一个参数,tolerance它代表简化路径可以偏离原始路径的最大距离。越大tolerance,简化路径越粗糙。另一个参数是,min_angle它定义了什么是转折点。(我将转折点设为原始路径上的任意点,其简化路径上的进入和离开向量之间的角度大于min_angle)。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4984262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存