Reputation: 15
I am trying a example of PCA and I find the eigenvalues using the MATLAB are different from the values using OpenCV, while the eigenvectors are same. Does anyone know why? What's the difference between this two algorithms?
My MATLAB code is as follows:
a=[-14.8271317103068,-3.00108550936016,1.52090778549498,3.95534842970601;...
-16.2288612441648,-2.80187433749996,-0.410815700402130,1.47546694457079;...
-15.1242838039605,-2.59871263957451,-0.359965674446737,1.34583763509479;...
-15.7031424565913,-2.53005662064257,0.255003254103276,-0.179334985754377;...
-17.7892158910100,-3.32842422986555,0.255791146332054,1.65118282449042;...
-17.8126324036279,-4.09719527953407,-0.879821957489877,-0.196675865428539;...
-14.9958877514765,-3.90753364293621,-0.418298866141441,-0.278063876667954;...
-15.5246706309866,-2.08905845264568,-1.16425848541704,-1.16976057326753;];
[covEigvec, ~,covEigval] = princomp(a, 'econ');
My OpenCV code is as follows:
cv::Mat sampleset(nums,dim,CV_32FC1,data);
cv::PCA *pca = new cv::PCA(sampleset,cv::Mat(),CV_PCA_DATA_AS_ROW,redDim);
Upvotes: 1
Views: 837
Reputation: 1349
Yes, those eigenvalues are different, up to a scale.
because opencv scales the data while computing the covariance matrix.
see core/src/matmul.cpp:2226
(roughly here)
mulTransposed( data, _covar, ((flags & CV_COVAR_NORMAL) == 0) ^ takeRows,
mean, (flags & CV_COVAR_SCALE) != 0 ? 1./nsamples : 1, ctype );
this function will eventually call gemm
, with its fifth argument as scaling factor
Upvotes: 1