I have problem to find method to compare two trajectories (curves). The first original contains points (x,y). The second one can be offset, smaller or larger scale, and with rotation - also array with points (x,y)
My first method that i did is to find smallest distance between two points and repeat this process in every iteration, sum of it and divide by number of points - then my result tell me value the average error per point: http://www.mathopenref.com/coorddist.html
And also i find this method: https://help.scilab.org/docs/6.0.0/en_US/fminsearch.html
But i cant figure out how to use it. I would like compare both trajectories but my results have to include rotation, or at least offset for beginning.
My current result is calculate error per point (distance)
- get coordinate (x,y) second trajectory.
- in loop i try to find min_distance between (x,y) from 1. and point from original trajectory.
- add smallest_distance what i found in 2 step.
- divide sum of smallest distance by number of points from second trajectory.
My result describe average error(distance) per points if we compare with original trajectory.
But i can not figure how to handle if trajectory is rotated, scaled or is shifted.
Please look at my example trajectories:
So you need to compare shape of 2 curves invariant on rotation,translation and scale.
Solution
Let assume 2 sinwaves for testing. Both rotated and scaled but with the same aspect ratio and one with added noise. I generated them in C++ like this:
OBB oriented bounding box
compute OBB which will find the rotation angle and position of both curves so rotate one of them so they start at the same position and has the same orientation.
If the OBB sizes are too different then the curves are different.
For above example it yealds this result:
Each OBB is defined by start point
P
and basis vectorsU,V
where|U|>=|V|
and z coordinate ofU x V
is positive. That will ensure the same winding for all OBBs. It can be done inOBBox_compute
by adding this to the end:So
curve0
hasp0,u0,v0
andcurve1
hasp1,u1,v1
.Now we want to rescale,translate and rotate
curve1
to matchcurve0
It can be done like this:You can use Understanding 4x4 homogenous transform matrices to do all this in one step. Here the result:
sampling
in case of non uniform or very different point density between curves or between any parts of it you should re-sample your curves to have common point density. You can use linear or polynomial interpolation for this. You also do not need to store the new sampling in memory but instead you could build function that returns point of each curve parametrized by arc-length from start.
comparison
Now you can substract the 2 curves and sum up the abs of the differences. Then divide it by the curve length and threshold the result.
You should try this even with +180deg rotation as the orientation difference from OBB has only half of the true range.
Here few related QAs: