Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slhistmetric_pw
Home > sltoolbox > discrete > slhistmetric_pw.m

slhistmetric_pw

PURPOSE ^

SLHISTMETRIC_PW Computes distance metrics between histograms pairwisely

SYNOPSIS ^

function D = slhistmetric_pw(H1, H2, mtype, varargin)

DESCRIPTION ^

SLHISTMETRIC_PW Computes distance metrics between histograms pairwisely

 $ Syntax $
   - D = slhistmetric_pw(H1, H2, mtype, ...)

 $ Arguments $
   - H1, H2:       The matrices of histogram sets (d1 x n1, d2 x n2)
   - mtype:        The metric type
   - D:            The resulting pairwise metric matrix (n1 x n2)

 $ Description $
   - D = slhistmetric_cp(H1, H2, mtype, ...) computes the distance metrics
     between all histograms in H1 and H2 pairwisely. If H1 and H2 have
     n1 and n2 bins respectively, then D will be an n1 x n2 matrix.
     The function support following types of histogram distances:
     \*
     \t   Table.  The Histogram Metrics
     \h      name     &          description
          'L1dist'    &  The sum of absolute differences: 
                         d = sum |h1(i) - h2(i)|               \\
          'L2dist'    &  Euclidean distance: 
                         d = sqrt(sum( (h1(i) - h2(i))^2 ))    \\
          'quaddist'  &  Quadratic-Form distance: 
                         d = sqrt((h1 - h2)^T * Q * (h1 - h2)) \\
                         use Q (d x d matrix) as the first extra param.
          'hamming'   &  Hamming distance with threshold
                         ht1 = h1 > t
                         ht2 = h2 > t
                         d = sum(ht1 ~= ht2)                  
                         use threshold t as the first extra param.\\
          'intersect' &  Histogram Intersection: 
                         d = 1 - 
                         sum min(h1(i), h2(i))) / sum h2(i) \\
          'chisq'     &  Chi-Square Distance:
                         d = sum (h1(i) - h2(i))^2 / (2 * (h1(i)+h2(i)) \\
          'kolsm'     &  Kolmogorov-Smirnov distance: 
                         d = max |F1(i) - F2(i)| 
                         only suitable for scalar histogram.   \\
          'kramvon'   &  Kramer/Von Mises:
                         d = sum (F1(i) - F2(i))^2              \\
          'kldiv'     &  Kull-back Leibler divergence
                         d = sum h1(i) log h1(i) / h2(i)        \\
          'jeffrey'   &  Jeffrey divergence
                         d = KL(h1, (h1+h2)/2) + KL(h2, (h1+h2)/2) \\
     \*     
          
 $ History $
   - Created by Dahua Lin, on Sep 18, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdvec SLADDVEC adds a vector to columns or rows of a matrix
  • sldiff_pw SLDIFF_PW Measures the pair-wise difference
  • slmetric_pw SLMETRIC_PW Compute the metric between column vectors pairwisely
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function D = slhistmetric_pw(H1, H2, mtype, varargin)
0002 %SLHISTMETRIC_PW Computes distance metrics between histograms pairwisely
0003 %
0004 % $ Syntax $
0005 %   - D = slhistmetric_pw(H1, H2, mtype, ...)
0006 %
0007 % $ Arguments $
0008 %   - H1, H2:       The matrices of histogram sets (d1 x n1, d2 x n2)
0009 %   - mtype:        The metric type
0010 %   - D:            The resulting pairwise metric matrix (n1 x n2)
0011 %
0012 % $ Description $
0013 %   - D = slhistmetric_cp(H1, H2, mtype, ...) computes the distance metrics
0014 %     between all histograms in H1 and H2 pairwisely. If H1 and H2 have
0015 %     n1 and n2 bins respectively, then D will be an n1 x n2 matrix.
0016 %     The function support following types of histogram distances:
0017 %     \*
0018 %     \t   Table.  The Histogram Metrics
0019 %     \h      name     &          description
0020 %          'L1dist'    &  The sum of absolute differences:
0021 %                         d = sum |h1(i) - h2(i)|               \\
0022 %          'L2dist'    &  Euclidean distance:
0023 %                         d = sqrt(sum( (h1(i) - h2(i))^2 ))    \\
0024 %          'quaddist'  &  Quadratic-Form distance:
0025 %                         d = sqrt((h1 - h2)^T * Q * (h1 - h2)) \\
0026 %                         use Q (d x d matrix) as the first extra param.
0027 %          'hamming'   &  Hamming distance with threshold
0028 %                         ht1 = h1 > t
0029 %                         ht2 = h2 > t
0030 %                         d = sum(ht1 ~= ht2)
0031 %                         use threshold t as the first extra param.\\
0032 %          'intersect' &  Histogram Intersection:
0033 %                         d = 1 -
0034 %                         sum min(h1(i), h2(i))) / sum h2(i) \\
0035 %          'chisq'     &  Chi-Square Distance:
0036 %                         d = sum (h1(i) - h2(i))^2 / (2 * (h1(i)+h2(i)) \\
0037 %          'kolsm'     &  Kolmogorov-Smirnov distance:
0038 %                         d = max |F1(i) - F2(i)|
0039 %                         only suitable for scalar histogram.   \\
0040 %          'kramvon'   &  Kramer/Von Mises:
0041 %                         d = sum (F1(i) - F2(i))^2              \\
0042 %          'kldiv'     &  Kull-back Leibler divergence
0043 %                         d = sum h1(i) log h1(i) / h2(i)        \\
0044 %          'jeffrey'   &  Jeffrey divergence
0045 %                         d = KL(h1, (h1+h2)/2) + KL(h2, (h1+h2)/2) \\
0046 %     \*
0047 %
0048 % $ History $
0049 %   - Created by Dahua Lin, on Sep 18, 2006
0050 %
0051 
0052 %% parse and verify input arguments
0053 
0054 if nargin < 3
0055     raise_lackinput('slhistmetric_cp', 3);
0056 end
0057 if ~isnumeric(H1) || ~isnumeric(H2) || ndims(H1) ~= 2 || ndims(H2) ~= 2
0058     error('sltoolbox:invalidarg', ...
0059         'H1 and H2 should be 2D numeric matrices');
0060 end
0061 
0062 
0063 %% main delegation
0064 
0065 switch mtype
0066     case 'L1dist'
0067         checkhistdim(H1, H2);
0068         D = slmetric_pw(H1, H2, 'cityblk');
0069         
0070     case 'L2dist'
0071         checkhistdim(H1, H2);
0072         D = slmetric_pw(H1, H2, 'eucdist');
0073                 
0074     case 'quaddist'
0075         if length(varargin) ~= 1
0076             error('sltoolbox:invalidarg', ...
0077                 'quaddist has one extra parameter Q');
0078         end
0079         checkhistdim(H1, H2);
0080         D = slmetric_pw(H1, H2, 'quaddiff', varargin{1});
0081         D = sqrt(D);
0082         
0083     case 'hamming'
0084         checkhistdim(H1, H2);
0085         if length(varargin) ~= 1
0086             error('sltoolbox:invalidarg', ...
0087                 'quaddist has one extra parameter t');
0088         end
0089         t = varargin{1};
0090         D = sldiff_pw(double(H1 > t), double(H2 > t), 'abssum');
0091         
0092     case 'intersect'
0093         checkhistdim(H1, H2);
0094         D = histmetricpw_core(H1, H2, 1);
0095                 
0096     case 'chisq'
0097         checkhistdim(H1, H2);
0098         D = histmetricpw_core(H1, H2, 2);        
0099         
0100     case 'kolsm'
0101         checkhistdim(H1, H2);
0102         F1 = cumsum(H1, 1);
0103         F2 = cumsum(H2, 1);
0104         D = sldiff_pw(F1, F2, 'maxdiff');
0105         
0106     case 'kramvon'
0107         checkhistdim(H1, H2);
0108         F1 = cumsum(H1, 1);
0109         F2 = cumsum(H2, 1);
0110         D = slmetric_pw(F1, F2, 'sqdist');        
0111         
0112     case 'kldiv'
0113         checkhistdim(H1, H2);
0114         D = kldivergence(H1, H2);
0115         
0116     case 'jeffrey'
0117         checkhistdim(H1, H2);
0118         D = histmetricpw_core(H1, H2, 3);
0119                       
0120     otherwise
0121         error('sltoolbox:invalidarg', ...
0122             'Invalid histogram metric type: %s', mtype);
0123 end
0124         
0125 
0126 %% Auxiliary functions
0127 
0128 function d = checkhistdim(H1, H2)
0129 
0130 d = size(H1, 1);
0131 if size(H2, 1) ~= d
0132     error('sltoolbox:sizmismatch', 'H1 and H2 have different dimensions.');
0133 end
0134 
0135 function D = kldivergence(H1, H2)
0136 
0137 V = zeros(size(H1));
0138 not_zero = H1 > 0;
0139 V(not_zero) = H1(not_zero) .* log(H1(not_zero));
0140 v1 = sum(V, 1)';
0141 clear V;
0142 
0143 H2(~not_zero) = 1;
0144 L2 = log(H2);
0145 D = -H1' * L2;
0146 D = sladdvec(D, v1, 1);
0147 
0148 
0149 
0150

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com