栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可以使用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)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号