Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating similarity between drawn lines

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: 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?

like image 757
flukes1 Avatar asked Oct 13 '22 23:10

flukes1


2 Answers

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.

like image 88
bhups Avatar answered Oct 18 '22 11:10

bhups


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.

like image 23
Gulzar Avatar answered Oct 18 '22 09:10

Gulzar