论坛类网站建设,网页传奇打金,网页设计项目案例网站,新手学网站建设解疑与技巧1200例您可以使用
Ramer-Douglas-Peucker (RDP) algorithm来简化路径。然后#xff0c;您可以计算简化路径每段的方向变化。对应于方向最大变化的点可以称为转折点#xff1a;
RDP算法的Python实现可以在on github中找到。
import matplotlib.pyplot as plt
import numpy as np
imp…您可以使用
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 directionsdir2 dir[1:]
dir1 dir[:-1]
return np.arccos((dir1*dir2).sum(axis1)/(
np.sqrt((dir1**2).sum(axis1)*(dir2**2).sum(axis1))))
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, axis0)
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(thetamin_angle)[0]1
fig plt.figure()
ax fig.add_subplot(111)
ax.plot(x, y, b-, labeloriginal path)
ax.plot(sx, sy, g--, labelsimplified path)
ax.plot(sx[idx], sy[idx], ro, markersize 10, labelturning points)
ax.invert_yaxis()
plt.legend(locbest)
plt.show()
以上使用两个参数RDP算法采用一个参数公差其中代表简化路径的最大距离可能偏离原来的路径。公差越大粗鲁的路径就越简单。另一个参数是min_angle它定义了被认为是一个转折点。 (我正在转弯点是原始路径上的任何一点简化路径上的进入和退出向量之间的角度大于min_angle)。