Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column-Wise Standard Deviation in OpenCV

Tags:

opencv

mean

Is there a direct way to compute the column-wise standard deviation for a matrix in opencv? Similar to std in Matlab. I've found one for the mean:

cv::Mat col_mean;
reduce(A, col_mean, 1, CV_REDUCE_AVG);

but I cannot find such a function for the standard deviation.

like image 572
Armin Meisterhirn Avatar asked May 18 '15 00:05

Armin Meisterhirn


2 Answers

Here's a quick answer to what you're looking for. I added both the standard deviation and mean for each column. The code can easily be modified for rows.

    cv::Mat A = ...; // FILL IN THE DATA FOR YOUR INPUT MATRIX
    cv::Mat meanValue, stdValue;
    cv::Mat colSTD(1, A.cols, CV_64FC1);
    cv::Mat colMEAN(1, A.cols, CV_64FC1);       

    for (int i = 0; i < A.cols; i++){           
        cv::meanStdDev(A.col(i), meanValue, stdValue);
        colSTD.at<double>(i) = stdValue.at<double>(0);
        colMEAN.at<double>(i) = meanValue.at<double>(0);
    }
like image 145
CV_User Avatar answered Jan 01 '23 19:01

CV_User


The following is not in a single line,but it is another version without loops:

    reduce(A, meanOfEachCol, 0, CV_REDUCE_AVG); // produces single row of columnar means
    Mat repColMean; 
    cv::repeat(meanOfEachCol, rows, 1, repColMean); // repeat mean vector 'rows' times

    Mat diffMean = A - repColMean; // get difference
    Mat diffMean2 = diffMean.mul(diffMean); // per element square

    Mat varMeanF;
    cv::reduce(diffMean2, varMeanF, 0, CV_REDUCE_AVG); // sum each column's elements to get single row
    Mat stdMeanF;
    cv::sqrt(varMeanF, stdMeanF); // get standard deviation
like image 45
Eagle Avatar answered Jan 01 '23 18:01

Eagle