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@inrae.fr 0026 % Created: 2004-02-24 0027 % Copyright 2004-2024 INRA - Cepia Software Platform 0028 0029 %% Setup options 0030 0031 % default values 0032 diag = false; 0033 norm = 2; 0034 0035 % check first argument: norm or diag 0036 if ~isempty(varargin) 0037 var = varargin{1}; 0038 if isnumeric(var) 0039 norm = var; 0040 elseif strncmp('diag', var, 4) 0041 diag = true; 0042 end 0043 varargin(1) = []; 0044 end 0045 0046 % check last argument: diag 0047 if ~isempty(varargin) 0048 var = varargin{1}; 0049 if strncmp('diag', var, 4) 0050 diag = true; 0051 end 0052 end 0053 0054 0055 % number of points in each array and their dimension 0056 n1 = size(p1, 1); 0057 n2 = size(p2, 1); 0058 d = size(p1, 2); 0059 0060 if diag 0061 % compute distance only for apparied couples of pixels 0062 dist = zeros(n1, 1); 0063 0064 if norm == 2 0065 % Compute euclidian distance. this is the default case 0066 % Compute difference of coordinate for each pair of point 0067 % and for each dimension. -> dist is a [n1*n2] array. 0068 for i = 1:d 0069 dist = dist + (p2(:,i)-p1(:,i)).^2; 0070 end 0071 dist = sqrt(dist); 0072 0073 elseif norm == inf 0074 % infinite norm corresponds to maximal difference of coordinate 0075 for i = 1:d 0076 dist = max(dist, abs(p2(:,i)-p1(:,i))); 0077 end 0078 0079 else 0080 % compute distance using the specified norm. 0081 for i = 1:d 0082 dist = dist + power((abs(p2(:,i)-p1(:,i))), norm); 0083 end 0084 dist = power(dist, 1/norm); 0085 end 0086 else 0087 % compute distance for all couples of pixels 0088 dist = zeros(n1, n2); 0089 0090 if norm == 2 0091 % Compute euclidian distance. This is the default case. 0092 % Compute difference of coordinate for each pair of point 0093 % and for each dimension. -> dist is a [n1*n2] array. 0094 for i = 1:d 0095 % equivalent to: 0096 % dist = dist + ... 0097 % (repmat(p1(:,i), [1 n2])-repmat(p2(:,i)', [n1 1])).^2; 0098 dist = dist + bsxfun (@minus, p1(:,i), p2(:, i)').^2; 0099 end 0100 dist = sqrt(dist); 0101 0102 elseif norm == inf 0103 % infinite norm corresponds to maximal difference of coordinate 0104 for i = 1:d 0105 dist = max(dist, abs(bsxfun (@minus, p1(:,i), p2(:, i)'))); 0106 end 0107 0108 else 0109 % compute distance using the specified norm. 0110 for i = 1:d 0111 % equivalent to: 0112 % dist = dist + power((abs(repmat(p1(:,i), [1 n2]) - ... 0113 % repmat(p2(:,i)', [n1 1]))), norm); 0114 dist = dist + power(abs(bsxfun(@minus, p1(:,i), p2(:, i)')), norm); 0115 end 0116 dist = power(dist, 1/norm); 0117 end 0118 end 0119