Ask Your Question
0

Issue with meanStdDev() result and Mat channels

asked Aug 30 '16

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

Preview: (hide)

Comments

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

berak gravatar imageberak (Aug 30 '16)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.

darelet gravatar imagedarelet (Aug 30 '16)edit

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

berak gravatar imageberak (Aug 30 '16)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?

darelet gravatar imagedarelet (Aug 30 '16)edit

1 answer

Sort by » oldest newest most voted
0

answered Aug 31 '16

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.

Preview: (hide)

Comments

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 (Aug 31 '16)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 (Sep 1 '16)edit

Question Tools

1 follower

Stats

Asked: Aug 30 '16

Seen: 1,647 times

Last updated: Aug 30 '16