Ask Your Question
0

trouble with calcCovarMatrix

asked 2014-02-19 04:56:10 -0600

berak gravatar image

updated 2014-02-19 07:20:46 -0600

i'm trying to port some matlab code, part of it involves getting the covariance matrix


octave example:


octave:1> A=[-1, 1, 2; -2, 3, 1; 4, 0, 3;]
A =

  -1   1   2
  -2   3   1
   4   0   3

octave:2> cov(A)
ans =

   10.3333   -4.1667    3.0000
   -4.1667    2.3333   -1.5000
    3.0000   -1.5000    1.0000

opencv version:


Mat_<double> A(3,3);  A << -1, 1, 2, -2, 3, 1 , 4, 0, 3;
Mat Sw, mea;
calcCovarMatrix( A, Sw, mea, cv::COVAR_NORMAL|cv::COVAR_ROWS );
cerr << Sw << endl;

[20.66666666666666, -8.333333333333334, 6;
 -8.333333333333334, 4.666666666666667, -3;
 6, -3, 2]

as you can see, similar, but a factor 2 in the opencv version .

any obvious mistake here ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-07-02 22:26:47 -0600

ganggang gravatar image

updated 2015-07-03 03:10:11 -0600

Eduardo gravatar image
Mat_<double> A(3,3);  A << -1, 1, 2, -2, 3, 1 , 4, 0, 3;
Mat Sw, mea;
calcCovarMatrix( A, Sw, mea, cv::COVAR_NORMAL|cv::COVAR_ROWS );
cerr << Sw << endl;

[20.66666666666666, -8.333333333333334, 6;
 -8.333333333333334, 4.666666666666667, -3;
 6, -3, 2]

sw  = sw / (A.rows - 1)
edit flag offensive delete link more

Comments

oh, thanks, so - opencv's version is just not normalized, and the normalization factor is 1/(rows-1) .

berak gravatar imageberak ( 2015-07-03 04:03:55 -0600 )edit

Question Tools

Stats

Asked: 2014-02-19 04:56:10 -0600

Seen: 411 times

Last updated: Jul 03 '15