Color Decorrelation

asked 2014-01-31 04:11:25 -0600

residentelvio gravatar image

Hi people. Im doing a translation from Matlab to C++. That' s code in Matlab I have to put in C++

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Color Decorrelation
 % Input Parameters:
 %  im           - image matrix
 %  numColComp   - number of image channels
 %
 % Output value:
 %  Xdecorr  - decorrelated image, same dimensions as the original one
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 function Xdecorr=colorDecorrelation(X,numColComp)

 if numColComp>1  %color images
 mu = mean(X); %mean of each feature of the original data
 X = bsxfun(@minus, X, mu);
 A = X'*X;   %cavariance matrix
 [V,D,notused] = svd(A);

 Xdecorr=X*V;

 else % in case of a grayscale image
    Xdecorr = X;
 end

My translation in C++

 Mat colorDecorrelation(Mat img,int numColComp){

 Mat V,D,A,Xdecorr,img_tr,img_f;
 Scalar mu;
 SVD svd;

 if(numColComp>1){

    //I have to convert because of gemm function need a float
    img.convertTo(img_f,CV_32FC3);
    img_f=img_f.reshape(1,img.rows*img.cols); // 1 channel

     mu=mean(img_f); 
     subtract(img_f,mu,img_f);
    transpose(img_f,img_tr);
     A = img_tr * img_f;
    V = svd(A).u; 
    Xdecorr = img_f*V;

 }
  else 
   Xdecorr=img;

 return Xdecorr;
 }

Do you think is it ok?

edit retag flag offensive close merge delete