I need an algorithm to calculate, numerically, the degree of similarity between two drawn lines. The lines are drawn using a mouse, and are stored as a set of cartesian coordinates before being filtered and smoothed using separate algorithms.
For example, within the following diagram:
Lines A and B are clearly similar, but B and C are not. The algorithm should reflect this. Additionally, the 'direction' of the line, as indicated by the start and end points, matters. Does such an algorithm already exist?
A naive approach can be taking the sum of the distances between the corresponding points on the two lines.So lets assume both of the lines are of almost same length and the number of points on the lines are approximately same and equidistant.
1.Translate the line 2 so that its start point is same as line 1's starting point.
2. calculate the sum of distances between corresponding points between line 1 and line2.
3. If average distance (i.e. SUM/NUMBER_OF_POINTS) is lesser than the THRESHOLD then lines are similar otherwise they are different.
This can be extended to support lines with different sizes. In that case, just enlarge the smaller line to match up to the longer line, then rest can be similar to the above approach.
Apart from calculating the distance you can calculate the difference of the slopes of the lines and if different in slopes at any point (or few points, you need to some experiment for this) is way to high (above than some THRESHOLD) then they are not similar.
This answer is extremely delayed, but I'll post for the sake of others like me who stumble upon it by search.
I believe the Fréchet distance could be the measure you are looking for, particularly because the direction matters.
You could go about this in many ways, one would be to sample the points drawn at some sampling rate, and calculate the Euclidean distance between each two samples at a timestamp, then take the maximal one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With