计算N维数据的平均值meanStdDev(均值及标准差)
方差
协方差calcCovarMatrix
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat src;
src = imread("../path.jpg");
if (src.empty())
{
cout << "could not load image..." << endl;
return -1;
}
namedWindow("src", WINDOW_AUTOSIZE);
imshow("src", src);
Mat mean, stddev;
//计算矩阵的均值及标准差//标准差只是协方差矩阵的对角元素,如果要计算协方差矩阵,要使用calcCovarMatrix()
meanStdDev(src, mean, stddev);
//3通道
cout << "均值矩阵行数: " << mean.rows << endl;
cout << "均值矩阵列数: " << mean.cols << endl;
cout << "标准差矩阵行数: " << stddev.rows << endl;
cout << "标准差矩阵列数: " << stddev.cols << endl;
cout << "均值mean矩阵为: " << endl;
for (int row = 0; row < mean.rows ; row++)
{
cout << mean.at(row) << endl;
}
cout << "标准差stddev矩阵为: " << endl;
for (int row = 0; row < stddev.rows; row++)
{
cout << stddev.at(row) << endl;
}
Mat samples = (Mat_(5,3) < 变量相同则为方差
calcCovarMatrix(samples, covar, means, COVAR_NORMAL | COVAR_ROWS);
cout << endl;
cout << "矩阵协方差矩阵covar: " << endl << covar/5 << endl;
cout << "矩阵均值mean: " << endl << means << endl;
waitKey(0);
return 0;
}
输出结果: