%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%          Singular Value Decomposition Stereo Matching                
%
% C=svdmatch(point_corr,point_dist,sigma,corr_th);
%  point_corr: m x n matrix  of feature correlations
%  point_dist: m x n matrix  of feature interdistance
%  sigma:      adjust point interaction (expected displacem.)
%  corr_th:    min acceptable correlation for a match
%  C: Feature mapping matrix;  C(i,j)=1 when 1:1 corresp.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function C=svdmatch(point_corr,point_dist,sigma,corr_th);
% Get number of features in both images
    [m, n] = size(point_corr); 
% Build correspondence strength matrix      
   G =  exp(-((point_corr-1).^2)/(2*0.4^2)) .* 
           exp(-(point_dist).^2/(2*sigma^2));   
% Perform SVD and extract orthogonal  matrix P
    [T,D,U] = svd(G);
    D = diag(ones(min(n,m),1));  D(m,n)=0;  % D is m x n  
    P = T*D*U';
% Find dominant of each line and column     
    [V, I] = max(P');  [V, J] = max(P);    
% Initialize correspondence matrix  C    
    C = zeros(m, n);     
% Set a one-to-one correspondence between Ii and Jj 
% only if  P(i,j) is MAX of  both row i and column j 
% AND  their correlation is  above corr_th
    for i=1:length(J)      
       if I(J(i))==i   
          if (point_corr(i,J(i))>corr_th)   
             C(i,J(i)) = 1;   
          end
       end
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
