Created
August 7, 2015 15:07
-
-
Save gakhov/85af28056ffb0ffd8f18 to your computer and use it in GitHub Desktop.
Using Ramer-Douglas-Peucker algorithm construct an approximated trajectory and find "valuable" turning points.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rdp import rdp | |
def angle(directions): | |
"""Return the angle between vectors | |
""" | |
vec2 = directions[1:] | |
vec1 = directions[:-1] | |
norm1 = np.sqrt((vec1 ** 2).sum(axis=1)) | |
norm2 = np.sqrt((vec2 ** 2).sum(axis=1)) | |
cos = (vec1 * vec2).sum(axis=1) / (norm1 * norm2) | |
return np.arccos(cos) | |
# Build simplified (approximated) trajectory | |
# using RDP algorithm. | |
simplified_trajectory = rdp(trajectory, epsilon=200) | |
sx, sy = simplified_trajectory.T | |
# Visualize trajectory and its simplified version. | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
ax.plot(x, y, 'r--', label='trajectory') | |
ax.plot(sx, sy, 'b-', label='simplified trajectory') | |
ax.set_xlabel("X") | |
ax.set_ylabel("Y") | |
ax.legend(loc='best') | |
# Define a minimum angle to treat change in direction | |
# as significant (valuable turning point). | |
min_angle = np.pi / 5.0 | |
# Compute the direction vectors on the simplified_trajectory. | |
directions = np.diff(simplified_trajectory, 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 | |
# Visualize valuable turning points on the simplified trjectory. | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
ax.plot(sx, sy, 'gx-', label='simplified trajectory') | |
ax.plot(sx[idx], sy[idx], 'ro', markersize = 7, label='turning points') | |
ax.set_xlabel("X") | |
ax.set_ylabel("Y") | |
ax.legend(loc='best') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment