Ask Your Question
0

calcCovarMatrix crashes

asked 2017-01-30 23:36:29 -0500

Nbb gravatar image

updated 2017-01-31 02:51:20 -0500

I am trying to compute the covariance and mean of my data matrix where each row is a sample of 3 dimensions. I have printed temp_rgb_mat to verify that it has X rows and 3 columns. The code I have below keeps crashing though and even after googling for various threads I still cant seem to figure out the reason behind it.

 //compute mean and covariance matrix of node
 cv::Mat temp_rgb_mat(cv::Size(3, nodes[i].rgb.size()), CV_8UC1, nodes[i].rgb.data());
 cv::calcCovarMatrix(&temp_rgb_mat, 1, nodes[i].covar_rgb, nodes[i].mean_rgb, CV_COVAR_NORMAL | CV_COVAR_ROWS);

nodes[i].rgb.data is a vector< Vec3b >. This is the error I receive

OpenCV Error: Assertion failed (data[i].size() == size && data[i].type() == type) in cv::calcCovarMatrix, file C:\Bin\opencv-master\source\modules\core\src\matmul.cpp, line 2525
edit retag flag offensive close merge delete

Comments

error msg, please..

berak gravatar imageberak ( 2017-01-31 01:36:43 -0500 )edit

do i have to duplicate temp_rgb_mat then pass in the both of them ? it seems like that is what is required based on the docs...

http://docs.opencv.org/2.4/modules/co... calcCovarMatrix(const Mat* samples, int nsamples, Mat& covar, Mat& mean, int flags, int ctype)

Nbb gravatar imageNbb ( 2017-01-31 02:29:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-01-31 03:21:04 -0500

berak gravatar image

updated 2017-01-31 08:21:19 -0500

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:]

imho, the other overload (with Mat* and nsamples) is meant to be used, if you have your 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]
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-01-30 23:36:29 -0500

Seen: 264 times

Last updated: Jan 31 '17