Column standard deviation in OpenCV

Is there a direct way to calculate the standard deviation by column for a matrix in opencv? Like std in matlab. I found one for the middle:

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

but I cannot find such a function for standard deviation.

+4
source share
2 answers

Here is a quick answer to what you are looking for. I added both standard deviation and average value for each column. Code can be easily modified for strings.

    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);
    }
+5
source

Below is not one line, but 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 elements to get single row
    Mat stdMeanF;
    cv::sqrt(varMeanF, stdMeanF); // get standard deviation
+2
source

All Articles