Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

you should try another calcCovarMatrix() overload, like this:

Mat_<uchar> bgr(5,3);
bgr << 2,3,4,5,6, 7,8,9,8,7, 5,4,5,6,7;

Mat mean,covs;
calcCovarMatrix(bgr, covs, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);

cerr << bgr << endl;
cerr << covs << endl;
cerr << mean << endl;

[  2,   3,   4;
   5,   6,   7;
   8,   9,   8;
   7,   5,   4;
   5,   6,   7]
[21.2, 16.4, 8;
 16.4, 18.8, 14;
 8, 14, 14]
[5.4, 5.800000000000001, 6]

(i have NO idea at all, why your former version produces different results, or what assumptions are behind it, but i think, - you want the latter !)

you should try another calcCovarMatrix() overload, like this:

Mat_<uchar> bgr(5,3);
bgr << 2,3,4,5,6, 7,8,9,8,7, 5,4,5,6,7;

Mat mean,covs;
calcCovarMatrix(bgr, covs, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);

cerr << bgr << endl;
cerr << covs << endl;
cerr << mean << endl;

[  2,   3,   4;
   5,   6,   7;
   8,   9,   8;
   7,   5,   4;
   5,   6,   7]
[21.2, 16.4, 8;
 16.4, 18.8, 14;
 8, 14, 14]
[5.4, 5.800000000000001, 6]

[update:]

(i imho, the other overload (with Mat* and nsamples) is meant to be used, if you have NO idea at all, why your former version produces different results, or what assumptions are behind it, but i think, - you want the latter !)samples in seperate row-Mat's., like this:

vector<Mat> rows;
for (size_t i=0; i<bgr.rows; i++) {
    rows.push_back(bgr.row(i));
}
calcCovarMatrix(&rows[0], rows.size(), covs, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);
cerr << covs << endl;
cerr << mean << endl;

[21.2, 16.4, 8;
 16.4, 18.8, 14;
 8, 14, 14]
[5.4, 5.800000000000001, 6]