有没有直接的方法来计算opencv中矩阵的逐列标准差?类似于Matlab中的std。我找到了一个平均值:
cv::Mat col_mean;
reduce(A, col_mean, 1, CV_REDUCE_AVG);
但是我找不到标准偏差这样的功能。
答案 0 :(得分:5)
这里是您正在寻找的快速答案。我添加了每列的标准偏差和平均值。可以轻松地为行修改代码。
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);
}
答案 1 :(得分:2)
以下不是一行,而是另一个没有循环的版本:
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