From 80f91a7ce7f0106c319bdd475d340ee2edc691b1 Mon Sep 17 00:00:00 2001 From: Scott Klum Date: Wed, 28 Jan 2015 14:41:58 -0500 Subject: [PATCH] EigenUtils is now a namespace --- openbr/core/eigenutils.cpp | 35 +++++++++++++++-------------------- openbr/core/eigenutils.h | 100 +++++++++++++++++++++++++++------------------------------------------------------------------------- openbr/plugins/eigen3.cpp | 2 +- openbr/plugins/stasm4.cpp | 4 ++-- 4 files changed, 45 insertions(+), 96 deletions(-) diff --git a/openbr/core/eigenutils.cpp b/openbr/core/eigenutils.cpp index c701d44..85c49f1 100644 --- a/openbr/core/eigenutils.cpp +++ b/openbr/core/eigenutils.cpp @@ -5,7 +5,7 @@ using namespace Eigen; using namespace cv; //Helper function to quickly write eigen matrix to disk. Not efficient. -void writeEigen(MatrixXf X, QString filename) { +void EigenUtils::writeEigen(MatrixXf X, QString filename) { Mat m(X.rows(),X.cols(),CV_32FC1); for (int i = 0; i < X.rows(); i++) { for (int j = 0; j < X.cols(); j++) { @@ -16,7 +16,7 @@ void writeEigen(MatrixXf X, QString filename) { format->write(br::Template(m)); } -void writeEigen(MatrixXd X, QString filename) { +void EigenUtils::writeEigen(MatrixXd X, QString filename) { Mat m(X.rows(),X.cols(),CV_32FC1); for (int i = 0; i < X.rows(); i++) { for (int j = 0; j < X.cols(); j++) { @@ -27,7 +27,7 @@ void writeEigen(MatrixXd X, QString filename) { format->write(br::Template(m)); } -void writeEigen(VectorXd X, QString filename) { +void EigenUtils::writeEigen(VectorXd X, QString filename) { Mat m(X.size(),1,CV_32FC1); for (int i = 0; i < X.rows(); i++) { m.at(i,0) = (float)X(i); @@ -36,7 +36,7 @@ void writeEigen(VectorXd X, QString filename) { format->write(br::Template(m)); } -void writeEigen(VectorXf X, QString filename) { +void EigenUtils::writeEigen(VectorXf X, QString filename) { Mat m(X.size(),1,CV_32FC1); for (int i = 0; i < X.rows(); i++) { m.at(i,0) = X(i); @@ -45,7 +45,7 @@ void writeEigen(VectorXf X, QString filename) { format->write(br::Template(m)); } -void printEigen(Eigen::MatrixXd X) { +void EigenUtils::printEigen(Eigen::MatrixXd X) { for (int i = 0; i < X.rows(); i++) { QString str; for (int j = 0; j < X.cols(); j++) { @@ -54,7 +54,7 @@ void printEigen(Eigen::MatrixXd X) { qDebug() << str; } } -void printEigen(Eigen::MatrixXf X) { +void EigenUtils::printEigen(Eigen::MatrixXf X) { for (int i = 0; i < X.rows(); i++) { QString str; for (int j = 0; j < X.cols(); j++) { @@ -64,20 +64,15 @@ void printEigen(Eigen::MatrixXf X) { } } -void printSize(Eigen::MatrixXf X) { +void EigenUtils::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 EigenUtils::eigStd(const Eigen::MatrixXf& x) { + return sqrt((x.array() - x.mean()).pow(2).sum() / (x.cols() * x.rows())); } -float eigStd(const Eigen::MatrixXf& x) { - float mean = eigMean(x); - return sqrt((x.array() - mean).pow(2).sum() / (x.cols() * x.rows())); -} - -MatrixXf removeRowCol(const MatrixXf X, int row, int col) { +MatrixXf EigenUtils::removeRowCol(const MatrixXf X, int row, int col) { MatrixXf Y(X.rows() - 1,X.cols() - 1); for (int i1 = 0, i2 = 0; i1 < X.rows(); i1++) { @@ -96,7 +91,7 @@ MatrixXf removeRowCol(const MatrixXf X, int row, int col) { return Y; } -MatrixXf pointsToMatrix(const QList points, bool isAffine) { +MatrixXf EigenUtils::pointsToMatrix(const QList points, bool isAffine) { MatrixXf P(points.size(), isAffine ? 3 : 2); for (int i = 0; i < points.size(); i++) { P(i, 0) = points[i].x(); @@ -107,7 +102,7 @@ MatrixXf pointsToMatrix(const QList points, bool isAffine) { return P; } -QList matrixToPoints(const Eigen::MatrixXf P) { +QList EigenUtils::matrixToPoints(const Eigen::MatrixXf P) { QList points; for (int i = 0; i < P.rows(); i++) points.append(QPointF(P(i, 0), P(i, 1))); @@ -115,7 +110,7 @@ QList matrixToPoints(const Eigen::MatrixXf P) { } //Converts x y points in a single vector to two column matrix -Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector) { +Eigen::MatrixXf EigenUtils::vectorToMatrix(const Eigen::MatrixXf vector) { int n = vector.rows(); Eigen::MatrixXf matrix(n / 2, 2); for (int i = 0; i < n / 2; i++) { @@ -126,7 +121,7 @@ Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector) { return matrix; } -Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix) { +Eigen::MatrixXf EigenUtils::matrixToVector(const Eigen::MatrixXf matrix) { int n2 = matrix.rows(); Eigen::MatrixXf vector(n2 * 2, 1); for (int i = 0; i < n2; i++) { @@ -137,7 +132,7 @@ Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix) { return vector; } -Eigen::MatrixXf toEigen(const Mat m) { +Eigen::MatrixXf EigenUtils::toEigen(const Mat m) { if (m.type() != CV_32F) qFatal("Mat to Eigen Converstation only supports CV_32F"); diff --git a/openbr/core/eigenutils.h b/openbr/core/eigenutils.h index 73a72e0..2ac14f3 100644 --- a/openbr/core/eigenutils.h +++ b/openbr/core/eigenutils.h @@ -23,27 +23,33 @@ #include -void writeEigen(Eigen::MatrixXf X, QString filename); -void writeEigen(Eigen::MatrixXd X, QString filename); -void writeEigen(Eigen::VectorXd X, QString filename); -void writeEigen(Eigen::VectorXf X, QString filename); -void printEigen(Eigen::MatrixXd X); -void printEigen(Eigen::MatrixXf X); -void printSize(Eigen::MatrixXf X); - -//Converts x y points in a single vector to two column matrix -Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector); -Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix); - -//Remove row and column from the matrix: -Eigen::MatrixXf removeRowCol(const Eigen::MatrixXf X, int row, int col); - -//Convert a point list into a matrix: -Eigen::MatrixXf pointsToMatrix(const QList points, bool isAffine=false); -QList matrixToPoints(const Eigen::MatrixXf P); - -//Convert cv::Mat to Eigen -Eigen::MatrixXf toEigen(const cv::Mat m); +namespace EigenUtils +{ + void writeEigen(Eigen::MatrixXf X, QString filename); + void writeEigen(Eigen::MatrixXd X, QString filename); + void writeEigen(Eigen::VectorXd X, QString filename); + void writeEigen(Eigen::VectorXf X, QString filename); + void printEigen(Eigen::MatrixXd X); + void printEigen(Eigen::MatrixXf X); + void printSize(Eigen::MatrixXf X); + + // Converts x y points in a single vector to two column matrix + Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector); + Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix); + + // Remove row and column from the matrix: + Eigen::MatrixXf removeRowCol(const Eigen::MatrixXf X, int row, int col); + + // Convert a point list into a matrix: + Eigen::MatrixXf pointsToMatrix(const QList points, bool isAffine=false); + QList matrixToPoints(const Eigen::MatrixXf P); + + // Compute the element-wise standard deviation + float eigStd(const Eigen::MatrixXf& x); + + // Convert cv::Mat to Eigen + Eigen::MatrixXf toEigen(const cv::Mat m); +} template inline QDataStream &operator<<(QDataStream &stream, const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &mat) @@ -83,56 +89,4 @@ 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 diff --git a/openbr/plugins/eigen3.cpp b/openbr/plugins/eigen3.cpp index 37f3e68..b9db883 100644 --- a/openbr/plugins/eigen3.cpp +++ b/openbr/plugins/eigen3.cpp @@ -600,7 +600,7 @@ class SparseLDATransform : public Transform //Only works on binary class problems for now assert(ldaOrig.projection.cols() == 1); - float ldaStd = eigStd(ldaOrig.projection); + float ldaStd = EigenUtils::eigStd(ldaOrig.projection); for (int i = 0; i < ldaOrig.projection.rows(); i++) if (abs(ldaOrig.projection(i)) > varThreshold * ldaStd) selections.append(i); diff --git a/openbr/plugins/stasm4.cpp b/openbr/plugins/stasm4.cpp index 0a350e3..98bbad6 100644 --- a/openbr/plugins/stasm4.cpp +++ b/openbr/plugins/stasm4.cpp @@ -180,7 +180,7 @@ private: void project(const Template &src, Template &dst) const { QList paramList = src.file.getList("affineParameters"); - Eigen::MatrixXf points = pointsToMatrix(src.file.points(), true); + Eigen::MatrixXf points = EigenUtils::pointsToMatrix(src.file.points(), true); Eigen::MatrixXf affine = Eigen::MatrixXf::Zero(3, 3); for (int i = 0, cnt = 0; i < 2; i++) for (int j = 0; j < 3; j++, cnt++) @@ -192,7 +192,7 @@ private: points = affineInv * pointsT; dst = src; dst.file.clearPoints(); - dst.file.setPoints(matrixToPoints(points.transpose())); + dst.file.setPoints(EigenUtils::matrixToPoints(points.transpose())); } }; -- libgit2 0.21.4