Ask Your Question
0

Calculate covariance in Opencv

asked 2015-02-20 07:12:36 -0600

RiSaMa gravatar image

updated 2015-02-20 07:50:27 -0600

theodore gravatar image

Dear forum followers I have a issue which I don't know how solve it.

I have a Mat like:

500.0   350.2
500.5   355.8
498.7   352.0
............

And I need calculate the covariance. The result would be something like:

0.8633    1.2167
1.2167    8.1733

Of course, the function which I need is calcCovarMatrix.... BUT if I execute these code:

cv::Mat a = (cv::Mat_<double>(3, 2) << 500.0, 350.2, 500.5, 355.8, 498.7, 352.0);
cv::Mat mu, new_covs;
cv::calcCovarMatrix(a, new_covs, mu, CV_COVAR_NORMAL | CV_COVAR_COLS);
The result is a incomprehensible 3x3 matrix...
new_covs=
[11220.02, 10838.03, 10987.83;
  10838.03, 10469.045, 10613.745;
  10987.83, 10613.745, 10760.445]

Another question:

If I have a set of clusters could I get the weight of these clusters like Expectation Maximization does it. How?: (http://docs.opencv.org/modules/ml/doc...)

I hope you can help my with my problem!! Thank you in advanced!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-02-20 07:44:18 -0600

theodore gravatar image

updated 2015-02-20 07:44:44 -0600

for the covariance:

Mat_<float> samples = (Mat_<float>(3, 2) << 500.0, 350.2,
                                            500.5, 355.8,
                                            498.7, 352.0);

Mat cov, mu;
cv::calcCovarMatrix(samples, cov, mu, CV_COVAR_NORMAL | CV_COVAR_ROWS);

cov = cov / (samples.rows - 1);

cout << "cov: " << endl;
cout << cov << endl;

cout << "mu: " << endl;
cout << mu << endl;

result:

cov: 
[0.8633207194507122, 1.216659545898438;
 1.216659545898438, 8.173264974107346]
mu: 
[499.7333374023438, 352.6666666666666]
edit flag offensive delete link more

Comments

Exactly!!! We need to divide between number of rows -1!!

You are a genius!!!

RiSaMa gravatar imageRiSaMa ( 2015-02-20 07:54:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-20 07:12:36 -0600

Seen: 6,441 times

Last updated: Feb 20 '15