diff --git a/openbr/core/eigenutils.cpp b/openbr/core/eigenutils.cpp index c054155..f927b33 100644 --- a/openbr/core/eigenutils.cpp +++ b/openbr/core/eigenutils.cpp @@ -67,3 +67,12 @@ void printEigen(Eigen::MatrixXf X) { void printSize(Eigen::MatrixXf X) { qDebug() << "Rows=" << X.rows() << "\tCols=" << X.cols(); } + +float eigMean(const Eigen::MatrixXf& x) { + return x.array().sum() / (x.rows() * x.cols()); +} + +float eigStd(const Eigen::MatrixXf& x) { + float mean = eigMean(x); + return sqrt((x.array() - mean).pow(2).sum() / (x.cols() * x.rows())); +} diff --git a/openbr/core/eigenutils.h b/openbr/core/eigenutils.h index 1ca8fa7..fa9a075 100644 --- a/openbr/core/eigenutils.h +++ b/openbr/core/eigenutils.h @@ -67,4 +67,55 @@ inline QDataStream &operator>>(QDataStream &stream, Eigen::Matrix< _Scalar, _Row return stream; } +/*Compute the mean of the each column (dim == 1) or row (dim == 2) + of the matrix*/ +template +Eigen::MatrixBase eigMean(const Eigen::MatrixBase& x,int dim) +{ + if (dim == 1) { + Eigen::MatrixBase y(1,x.cols()); + for (int i = 0; i < x.cols(); i++) + y(i) = x.col(i).sum() / x.rows(); + return y; + } else if (dim == 2) { + Eigen::MatrixBase y(x.rows(),1); + for (int i = 0; i < x.rows(); i++) + y(i) = x.row(i).sum() / x.cols(); + return y; + } + qFatal("A matrix can only have two dimensions"); +} + +/*Compute the element-wise mean*/ +float eigMean(const Eigen::MatrixXf& x); +/*Compute the element-wise mean*/ +float eigStd(const Eigen::MatrixXf& x); + +/*Compute the std dev of the each column (dim == 1) or row (dim == 2) + of the matrix*/ +template +Eigen::MatrixBase eigStd(const Eigen::MatrixBase& x,int dim) +{ + Eigen::MatrixBase mean = eigMean(x, dim); + if (dim == 1) { + Eigen::MatrixBase y(1,x.cols()); + for (int i = 0; i < x.cols(); i++) { + T value = 0; + for (int j = 0; j < x.rows(); j++) + value += pow(y(j, i) - mean(i), 2); + y(i) = sqrt(value / (x.rows() - 1)); + } + return y; + } else if (dim == 2) { + Eigen::MatrixBase y(x.rows(),1); + for (int i = 0; i < x.rows(); i++) { + T value = 0; + for (int j = 0; j < x.cols(); j++) + value += pow(y(i, j) - mean(j), 2); + y(i) = sqrt(value / (x.cols() - 1)); + } + return y; + } + qFatal("A matrix can only have two dimensions"); +} #endif // EIGENUTILS_H