Ask Your Question

Issue with meanStdDev() result and Mat channels

asked 2016-08-30 07:46:17 -0600

darelet gravatar image

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


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

edit retag flag offensive close merge delete


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

berak gravatar imageberak ( 2016-08-30 08:00:09 -0600 )edit

@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.

darelet gravatar imagedarelet ( 2016-08-30 08:38:19 -0600 )edit

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

berak gravatar imageberak ( 2016-08-30 11:55:50 -0600 )edit

@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?

darelet gravatar imagedarelet ( 2016-08-30 12:15:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-08-30 19:03:08 -0600

Tetragramm gravatar image

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.

edit flag offensive delete link 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

darelet gravatar imagedarelet ( 2016-08-31 05:27:40 -0600 )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.

Tetragramm gravatar imageTetragramm ( 2016-08-31 23:01:02 -0600 )edit

Question Tools

1 follower


Asked: 2016-08-30 07:46:17 -0600

Seen: 1,401 times

Last updated: Aug 30 '16