File Exchange

image thumbnail

Frechet Distance Calculator

version 1.0.0.0 (2.49 KB) by Tristan Ursell
Estimate the similarity between curves in space using the scalar Frechet Distance.

11 Downloads

Updated 28 May 2013

View License

Tristan Ursell
Frechet Distance between two curves
May 2013

f = frechet(X1,Y1,X2,Y2)
f = frechet(X1,Y1,X2,Y2,res)

(X1,Y1) are the x and y coordinates of the first curve (column vector).
(X2,Y2) are the x and y coordinates of the second curve (column vector).

The lengths of the two curves do not have to be the same.

'res' is an optional parameter to set the resolution of 'f', the time to
compute scales linearly with 'res'. 'res' must be positive, and if 'res'
is larger than the largest distance between any two points on the curve
the function will throw a warning. If 'res' is unspecified, the function
will select a reasonable value, given the inputs.

This function estimates the Frechet Distance, which is a measure of the
dissimilarity between two curves in space (in this case in 2D). It is a
scalar value that is symmetric with respect to the two curves (i.e.
switching X1->X2 and Y1->Y2 does not change the value). Roughly
speaking, this distance metric is the minimum length of a line that
connects a point on each curve, and allows one to traverse both curves
from start to finish. (wiki: Frechet Distance)

The function requires column input vectors, and the function 'bwlabel'
from the image processing toolbox.

EXAMPLE: compare three curves to find out which two are most similar

%curve 1
t1=0:1:50;
X1=(2*cos(t1/5)+3-t1.^2/200)/2;
Y1=2*sin(t1/5)+3;

%curve 2
X2=(2*cos(t1/4)+2-t1.^2/200)/2;
Y2=2*sin(t1/5)+3;

%curve 3
X3=(2*cos(t1/4)+2-t1.^2/200)/2;
Y3=2*sin(t1/4+2)+3;

f12=frechet(X1',Y1',X2',Y2');
f13=frechet(X1',Y1',X3',Y3');
f23=frechet(X2',Y2',X3',Y3');
f11=frechet(X1',Y1',X1',Y1');
f22=frechet(X2',Y2',X2',Y2');
f33=frechet(X3',Y3',X3',Y3');

figure;
subplot(2,1,1)
hold on
plot(X1,Y1,'r','linewidth',2)
plot(X2,Y2,'g','linewidth',2)
plot(X3,Y3,'b','linewidth',2)
legend('curve 1','curve 2','curve 3','location','eastoutside')
xlabel('X')
ylabel('Y')
axis equal tight
box on
title(['three space curves to compare'])

subplot(2,1,2)
imagesc([[f11,f12,f13];[f12,f22,f23];[f13,f23,f33]])
xlabel('curve')
ylabel('curve')
cb1=colorbar('peer',gca);
set(get(cb1,'Ylabel'),'String','Frechet Distance')
axis equal tight

Comments and Ratings (5)

gang qin

Lisa Wang

Just what i need! Thank you so much!

yak1226

just what I want ,great!

Venkat R

Nice submission. Has great potential application.

you

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux