DISTANCEPOINTS Compute distance between two points. D = distancePoints(P1, P2) Return the Euclidean distance between points P1 and P2. If P1 and P2 are two arrays of points, result is a N1-by-N2 array containing distance between each point of P1 and each point of P2. D = distancePoints(P1, P2, NORM) Compute distance using the specified norm. NORM=2 corresponds to usual euclidean distance, NORM=1 corresponds to Manhattan distance, NORM=inf is assumed to correspond to maximum difference in coordinate. Other values (>0) can be specified. D = distancePoints(..., 'diag') compute only distances between P1(i,:) and P2(i,:). See also: points2d, minDistancePoints, nndist, hausdorffDistance
0001 function dist = distancePoints(p1, p2, varargin) 0002 %DISTANCEPOINTS Compute distance between two points. 0003 % 0004 % D = distancePoints(P1, P2) 0005 % Return the Euclidean distance between points P1 and P2. 0006 % 0007 % If P1 and P2 are two arrays of points, result is a N1-by-N2 array 0008 % containing distance between each point of P1 and each point of P2. 0009 % 0010 % D = distancePoints(P1, P2, NORM) 0011 % Compute distance using the specified norm. NORM=2 corresponds to usual 0012 % euclidean distance, NORM=1 corresponds to Manhattan distance, NORM=inf 0013 % is assumed to correspond to maximum difference in coordinate. Other 0014 % values (>0) can be specified. 0015 % 0016 % D = distancePoints(..., 'diag') 0017 % compute only distances between P1(i,:) and P2(i,:). 0018 % 0019 % See also: 0020 % points2d, minDistancePoints, nndist, hausdorffDistance 0021 % 0022 0023 % ------ 0024 % Author: David Legland 0025 % e-mail: david.legland@nantes.inra.fr 0026 % Copyright 2009 INRA - Cepia Software Platform. 0027 % created the 24/02/2004. 0028 % 0029 0030 % HISTORY : 0031 % 25/05/2004: manage 2 array of points 0032 % 07/04/2004: add option for computing only diagonal. 0033 % 30/10/2006: generalize to any dimension, and manage different norms 0034 % 03/01/2007: bug for arbitrary norm, and update doc 0035 % 28/08/2007: fix bug for norms 2 and infinite, in diagonal case 0036 0037 0038 %% Setup options 0039 0040 % default values 0041 diag = false; 0042 norm = 2; 0043 0044 % check first argument: norm or diag 0045 if ~isempty(varargin) 0046 var = varargin{1}; 0047 if isnumeric(var) 0048 norm = var; 0049 elseif strncmp('diag', var, 4) 0050 diag = true; 0051 end 0052 varargin(1) = []; 0053 end 0054 0055 % check last argument: diag 0056 if ~isempty(varargin) 0057 var = varargin{1}; 0058 if strncmp('diag', var, 4) 0059 diag = true; 0060 end 0061 end 0062 0063 0064 % number of points in each array and their dimension 0065 n1 = size(p1, 1); 0066 n2 = size(p2, 1); 0067 d = size(p1, 2); 0068 0069 if diag 0070 % compute distance only for apparied couples of pixels 0071 dist = zeros(n1, 1); 0072 0073 if norm == 2 0074 % Compute euclidian distance. this is the default case 0075 % Compute difference of coordinate for each pair of point 0076 % and for each dimension. -> dist is a [n1*n2] array. 0077 for i = 1:d 0078 dist = dist + (p2(:,i)-p1(:,i)).^2; 0079 end 0080 dist = sqrt(dist); 0081 0082 elseif norm == inf 0083 % infinite norm corresponds to maximal difference of coordinate 0084 for i = 1:d 0085 dist = max(dist, abs(p2(:,i)-p1(:,i))); 0086 end 0087 0088 else 0089 % compute distance using the specified norm. 0090 for i = 1:d 0091 dist = dist + power((abs(p2(:,i)-p1(:,i))), norm); 0092 end 0093 dist = power(dist, 1/norm); 0094 end 0095 else 0096 % compute distance for all couples of pixels 0097 dist = zeros(n1, n2); 0098 0099 if norm == 2 0100 % Compute euclidian distance. This is the default case. 0101 % Compute difference of coordinate for each pair of point 0102 % and for each dimension. -> dist is a [n1*n2] array. 0103 for i = 1:d 0104 % equivalent to: 0105 % dist = dist + ... 0106 % (repmat(p1(:,i), [1 n2])-repmat(p2(:,i)', [n1 1])).^2; 0107 dist = dist + bsxfun (@minus, p1(:,i), p2(:, i)').^2; 0108 end 0109 dist = sqrt(dist); 0110 0111 elseif norm == inf 0112 % infinite norm corresponds to maximal difference of coordinate 0113 for i = 1:d 0114 dist = max(dist, abs(bsxfun (@minus, p1(:,i), p2(:, i)'))); 0115 end 0116 0117 else 0118 % compute distance using the specified norm. 0119 for i = 1:d 0120 % equivalent to: 0121 % dist = dist + power((abs(repmat(p1(:,i), [1 n2]) - ... 0122 % repmat(p2(:,i)', [n1 1]))), norm); 0123 dist = dist + power(abs(bsxfun(@minus, p1(:,i), p2(:, i)')), norm); 0124 end 0125 dist = power(dist, 1/norm); 0126 end 0127 end 0128