# Issue with meanStdDev() result and Mat channels

meanStdDev() is not giving the correct calculation of mean and standard. I want it to give mean/stdev for each corresponding column. The code follows,

Mat_<int> C = (Mat_<int>(2,6) << 1, 0, 3, 1, 0, 5, 6, 2, 0, 4, 0, 9), E=Mat::zeros(9, 9, CV_8UC1);
Mat mn, stdev;
meanStdDev(C.reshape(C.cols),mn,stdev);
std::cout << C.reshape(C.cols) << endl<< mn << std::endl;


It gives the following results,

[1, 0, 3, 1, 0, 5;
6, 2, 0, 4, 0, 9]

[2.5;
1;
0;
0;
0;
0]


You can see that the calculated mean value is corrent only for the first 2 positions

edit retag close merge delete

your usage of Mat::reshape() looks broken. why is it even there ? please explain.

( 2016-08-30 08:00:09 -0500 )edit
1

@berak Don't know where my initial comment went to.. I'll repeat it. I used reshape to convert the matrix to a 6-channel 2x6, which makes the function calculate the mean/standard deviation for each column individually. If I leave it out then it seems that the matrix is taken as a 1-channel 2x6 matrix and that gives me one value for the mean and standard deviation values calculated across all matrix columns and rows.

( 2016-08-30 08:38:19 -0500 )edit

may i ask: which beast requires a 6 channel mat ?

( 2016-08-30 11:55:50 -0500 )edit
1

@berak Maybe let me put it this way, how I can get the mean value per column for a given square matrix using meanStdDev() or a similar function?

( 2016-08-30 12:15:19 -0500 )edit

Sort by ยป oldest newest most voted

Change to a for loop, where you use meanStdDev(C.col(i), mn, stdev) and store the results in an array of appropriate size.

meanStdDev only works up to 4 channels. Any more and the results wrap (I believe). With six channels you'd get (0+4, 1+5, 2, 3) (Again, I think). Either way, the documentation tells you that it only works up to 4.

more

I don't know why they couldn't implement such a function like with Matlab, who allow you to calculate the mean/stdev along columns or rows. I'll close this thread but if someone can chip in please use the comments if possible

( 2016-08-31 05:27:40 -0500 )edit

Well, OpenCV is an image processing library, not a matrix math library. 99% of the time, you want the Std of the whole image, or a single sub-image. 99.99% of the time, that image has 4 channels or less.

If you want the mean/std of each column, make a sub-image of each column (ex, the Mat::col(int) function is mentioned) and calculate it for that.

( 2016-08-31 23:01:02 -0500 )edit

Official site

GitHub

Wiki

Documentation