HAUSDORFFDISTANCE Hausdorff distance between two point sets. HD = hausdorffDistance(PTS1, PTS2) Computes the Hausdorff distance between the two point sets PTS1 and PTS2. The Hausdorf distance can be used to compare two shapes. The distance between a point x and a set Y is given by: d(x, Y) = inf { d(x,y) | y in Y } The distance between two non empty sets X and Y is given by: d(X, Y) = sup { d(x,Y) | x in X } The Hausdorff distance between sets X and Y distance is defined as the maximum of d(X,Y) and d(Y,X): HD(X,Y) = max { d(X,Y), d(Y,X) } Example % Compute Hausdorff distance between an ellipse and a rectangle % first define two shapes rect = resamplePolygon(orientedBoxToPolygon([20 30 80 40 30]), 60); poly = ellipseToPolygon([20 30 40 20 30], 500); % display the shapes figure; hold on drawPolygon(poly, 'b'); drawPolygon(rect, 'g'); axis equal; % compute hausdorff distance [hd ind1 ind2] = hausdorffDistance(poly, rect); p1h = poly(ind1, :); p2h = rect(ind2, :); drawPoint([p1h;p2h], 'mo'); drawEdge([p1h p2h], 'm') See also points2d, minDistancePoints References http://en.wikipedia.org/wiki/Hausdorff_distance
0001 function [hd, ind1, ind2] = hausdorffDistance(pts1, pts2) 0002 %HAUSDORFFDISTANCE Hausdorff distance between two point sets. 0003 % 0004 % HD = hausdorffDistance(PTS1, PTS2) 0005 % Computes the Hausdorff distance between the two point sets PTS1 and 0006 % PTS2. The Hausdorf distance can be used to compare two shapes. 0007 % 0008 % The distance between a point x and a set Y is given by: 0009 % d(x, Y) = inf { d(x,y) | y in Y } 0010 % The distance between two non empty sets X and Y is given by: 0011 % d(X, Y) = sup { d(x,Y) | x in X } 0012 % The Hausdorff distance between sets X and Y distance is defined as the 0013 % maximum of d(X,Y) and d(Y,X): 0014 % HD(X,Y) = max { d(X,Y), d(Y,X) } 0015 % 0016 % 0017 % Example 0018 % % Compute Hausdorff distance between an ellipse and a rectangle 0019 % % first define two shapes 0020 % rect = resamplePolygon(orientedBoxToPolygon([20 30 80 40 30]), 60); 0021 % poly = ellipseToPolygon([20 30 40 20 30], 500); 0022 % % display the shapes 0023 % figure; hold on 0024 % drawPolygon(poly, 'b'); 0025 % drawPolygon(rect, 'g'); 0026 % axis equal; 0027 % % compute hausdorff distance 0028 % [hd ind1 ind2] = hausdorffDistance(poly, rect); 0029 % p1h = poly(ind1, :); 0030 % p2h = rect(ind2, :); 0031 % drawPoint([p1h;p2h], 'mo'); 0032 % drawEdge([p1h p2h], 'm') 0033 % 0034 % See also 0035 % points2d, minDistancePoints 0036 % 0037 % References 0038 % http://en.wikipedia.org/wiki/Hausdorff_distance 0039 % 0040 0041 % ------ 0042 % Author: David Legland 0043 % e-mail: david.legland@inrae.fr 0044 % Created: 2012-05-04, using Matlab 7.9.0.529 (R2009b) 0045 % Copyright 2012 INRAE - Cepia Software Platform. 0046 0047 % distance from pts1 to pts2 0048 [dists1, ind12] = minDistancePoints(pts1, pts2); 0049 [max1, ind11] = max(dists1); 0050 0051 % distance from pts2 to pts1 0052 [dists2, ind22] = minDistancePoints(pts2, pts1); 0053 [max2, ind21] = max(dists2); 0054 0055 % keep the max of the two distances 0056 hd = max(max1, max2); 0057 0058 % keep the rigt indices 0059 if max1 > max2 0060 ind1 = ind11; 0061 ind2 = ind12(ind11); 0062 else 0063 ind1 = ind22(ind21); 0064 ind2 = ind21; 0065 end