Commit 80f91a7ce7f0106c319bdd475d340ee2edc691b1
1 parent
56866e7f
EigenUtils is now a namespace
Showing
4 changed files
with
45 additions
and
96 deletions
openbr/core/eigenutils.cpp
| ... | ... | @@ -5,7 +5,7 @@ using namespace Eigen; |
| 5 | 5 | using namespace cv; |
| 6 | 6 | |
| 7 | 7 | //Helper function to quickly write eigen matrix to disk. Not efficient. |
| 8 | -void writeEigen(MatrixXf X, QString filename) { | |
| 8 | +void EigenUtils::writeEigen(MatrixXf X, QString filename) { | |
| 9 | 9 | Mat m(X.rows(),X.cols(),CV_32FC1); |
| 10 | 10 | for (int i = 0; i < X.rows(); i++) { |
| 11 | 11 | for (int j = 0; j < X.cols(); j++) { |
| ... | ... | @@ -16,7 +16,7 @@ void writeEigen(MatrixXf X, QString filename) { |
| 16 | 16 | format->write(br::Template(m)); |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | -void writeEigen(MatrixXd X, QString filename) { | |
| 19 | +void EigenUtils::writeEigen(MatrixXd X, QString filename) { | |
| 20 | 20 | Mat m(X.rows(),X.cols(),CV_32FC1); |
| 21 | 21 | for (int i = 0; i < X.rows(); i++) { |
| 22 | 22 | for (int j = 0; j < X.cols(); j++) { |
| ... | ... | @@ -27,7 +27,7 @@ void writeEigen(MatrixXd X, QString filename) { |
| 27 | 27 | format->write(br::Template(m)); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | -void writeEigen(VectorXd X, QString filename) { | |
| 30 | +void EigenUtils::writeEigen(VectorXd X, QString filename) { | |
| 31 | 31 | Mat m(X.size(),1,CV_32FC1); |
| 32 | 32 | for (int i = 0; i < X.rows(); i++) { |
| 33 | 33 | m.at<float>(i,0) = (float)X(i); |
| ... | ... | @@ -36,7 +36,7 @@ void writeEigen(VectorXd X, QString filename) { |
| 36 | 36 | format->write(br::Template(m)); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | -void writeEigen(VectorXf X, QString filename) { | |
| 39 | +void EigenUtils::writeEigen(VectorXf X, QString filename) { | |
| 40 | 40 | Mat m(X.size(),1,CV_32FC1); |
| 41 | 41 | for (int i = 0; i < X.rows(); i++) { |
| 42 | 42 | m.at<float>(i,0) = X(i); |
| ... | ... | @@ -45,7 +45,7 @@ void writeEigen(VectorXf X, QString filename) { |
| 45 | 45 | format->write(br::Template(m)); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | -void printEigen(Eigen::MatrixXd X) { | |
| 48 | +void EigenUtils::printEigen(Eigen::MatrixXd X) { | |
| 49 | 49 | for (int i = 0; i < X.rows(); i++) { |
| 50 | 50 | QString str; |
| 51 | 51 | for (int j = 0; j < X.cols(); j++) { |
| ... | ... | @@ -54,7 +54,7 @@ void printEigen(Eigen::MatrixXd X) { |
| 54 | 54 | qDebug() << str; |
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | -void printEigen(Eigen::MatrixXf X) { | |
| 57 | +void EigenUtils::printEigen(Eigen::MatrixXf X) { | |
| 58 | 58 | for (int i = 0; i < X.rows(); i++) { |
| 59 | 59 | QString str; |
| 60 | 60 | for (int j = 0; j < X.cols(); j++) { |
| ... | ... | @@ -64,20 +64,15 @@ void printEigen(Eigen::MatrixXf X) { |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | -void printSize(Eigen::MatrixXf X) { | |
| 67 | +void EigenUtils::printSize(Eigen::MatrixXf X) { | |
| 68 | 68 | qDebug() << "Rows=" << X.rows() << "\tCols=" << X.cols(); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | -float eigMean(const Eigen::MatrixXf& x) { | |
| 72 | - return x.array().sum() / (x.rows() * x.cols()); | |
| 71 | +float EigenUtils::eigStd(const Eigen::MatrixXf& x) { | |
| 72 | + return sqrt((x.array() - x.mean()).pow(2).sum() / (x.cols() * x.rows())); | |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | -float eigStd(const Eigen::MatrixXf& x) { | |
| 76 | - float mean = eigMean(x); | |
| 77 | - return sqrt((x.array() - mean).pow(2).sum() / (x.cols() * x.rows())); | |
| 78 | -} | |
| 79 | - | |
| 80 | -MatrixXf removeRowCol(const MatrixXf X, int row, int col) { | |
| 75 | +MatrixXf EigenUtils::removeRowCol(const MatrixXf X, int row, int col) { | |
| 81 | 76 | MatrixXf Y(X.rows() - 1,X.cols() - 1); |
| 82 | 77 | |
| 83 | 78 | for (int i1 = 0, i2 = 0; i1 < X.rows(); i1++) { |
| ... | ... | @@ -96,7 +91,7 @@ MatrixXf removeRowCol(const MatrixXf X, int row, int col) { |
| 96 | 91 | return Y; |
| 97 | 92 | } |
| 98 | 93 | |
| 99 | -MatrixXf pointsToMatrix(const QList<QPointF> points, bool isAffine) { | |
| 94 | +MatrixXf EigenUtils::pointsToMatrix(const QList<QPointF> points, bool isAffine) { | |
| 100 | 95 | MatrixXf P(points.size(), isAffine ? 3 : 2); |
| 101 | 96 | for (int i = 0; i < points.size(); i++) { |
| 102 | 97 | P(i, 0) = points[i].x(); |
| ... | ... | @@ -107,7 +102,7 @@ MatrixXf pointsToMatrix(const QList<QPointF> points, bool isAffine) { |
| 107 | 102 | return P; |
| 108 | 103 | } |
| 109 | 104 | |
| 110 | -QList<QPointF> matrixToPoints(const Eigen::MatrixXf P) { | |
| 105 | +QList<QPointF> EigenUtils::matrixToPoints(const Eigen::MatrixXf P) { | |
| 111 | 106 | QList<QPointF> points; |
| 112 | 107 | for (int i = 0; i < P.rows(); i++) |
| 113 | 108 | points.append(QPointF(P(i, 0), P(i, 1))); |
| ... | ... | @@ -115,7 +110,7 @@ QList<QPointF> matrixToPoints(const Eigen::MatrixXf P) { |
| 115 | 110 | } |
| 116 | 111 | |
| 117 | 112 | //Converts x y points in a single vector to two column matrix |
| 118 | -Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector) { | |
| 113 | +Eigen::MatrixXf EigenUtils::vectorToMatrix(const Eigen::MatrixXf vector) { | |
| 119 | 114 | int n = vector.rows(); |
| 120 | 115 | Eigen::MatrixXf matrix(n / 2, 2); |
| 121 | 116 | for (int i = 0; i < n / 2; i++) { |
| ... | ... | @@ -126,7 +121,7 @@ Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector) { |
| 126 | 121 | return matrix; |
| 127 | 122 | } |
| 128 | 123 | |
| 129 | -Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix) { | |
| 124 | +Eigen::MatrixXf EigenUtils::matrixToVector(const Eigen::MatrixXf matrix) { | |
| 130 | 125 | int n2 = matrix.rows(); |
| 131 | 126 | Eigen::MatrixXf vector(n2 * 2, 1); |
| 132 | 127 | for (int i = 0; i < n2; i++) { |
| ... | ... | @@ -137,7 +132,7 @@ Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix) { |
| 137 | 132 | return vector; |
| 138 | 133 | } |
| 139 | 134 | |
| 140 | -Eigen::MatrixXf toEigen(const Mat m) { | |
| 135 | +Eigen::MatrixXf EigenUtils::toEigen(const Mat m) { | |
| 141 | 136 | if (m.type() != CV_32F) |
| 142 | 137 | qFatal("Mat to Eigen Converstation only supports CV_32F"); |
| 143 | 138 | ... | ... |
openbr/core/eigenutils.h
| ... | ... | @@ -23,27 +23,33 @@ |
| 23 | 23 | |
| 24 | 24 | #include <opencv2/core/core.hpp> |
| 25 | 25 | |
| 26 | -void writeEigen(Eigen::MatrixXf X, QString filename); | |
| 27 | -void writeEigen(Eigen::MatrixXd X, QString filename); | |
| 28 | -void writeEigen(Eigen::VectorXd X, QString filename); | |
| 29 | -void writeEigen(Eigen::VectorXf X, QString filename); | |
| 30 | -void printEigen(Eigen::MatrixXd X); | |
| 31 | -void printEigen(Eigen::MatrixXf X); | |
| 32 | -void printSize(Eigen::MatrixXf X); | |
| 33 | - | |
| 34 | -//Converts x y points in a single vector to two column matrix | |
| 35 | -Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector); | |
| 36 | -Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix); | |
| 37 | - | |
| 38 | -//Remove row and column from the matrix: | |
| 39 | -Eigen::MatrixXf removeRowCol(const Eigen::MatrixXf X, int row, int col); | |
| 40 | - | |
| 41 | -//Convert a point list into a matrix: | |
| 42 | -Eigen::MatrixXf pointsToMatrix(const QList<QPointF> points, bool isAffine=false); | |
| 43 | -QList<QPointF> matrixToPoints(const Eigen::MatrixXf P); | |
| 44 | - | |
| 45 | -//Convert cv::Mat to Eigen | |
| 46 | -Eigen::MatrixXf toEigen(const cv::Mat m); | |
| 26 | +namespace EigenUtils | |
| 27 | +{ | |
| 28 | + void writeEigen(Eigen::MatrixXf X, QString filename); | |
| 29 | + void writeEigen(Eigen::MatrixXd X, QString filename); | |
| 30 | + void writeEigen(Eigen::VectorXd X, QString filename); | |
| 31 | + void writeEigen(Eigen::VectorXf X, QString filename); | |
| 32 | + void printEigen(Eigen::MatrixXd X); | |
| 33 | + void printEigen(Eigen::MatrixXf X); | |
| 34 | + void printSize(Eigen::MatrixXf X); | |
| 35 | + | |
| 36 | + // Converts x y points in a single vector to two column matrix | |
| 37 | + Eigen::MatrixXf vectorToMatrix(const Eigen::MatrixXf vector); | |
| 38 | + Eigen::MatrixXf matrixToVector(const Eigen::MatrixXf matrix); | |
| 39 | + | |
| 40 | + // Remove row and column from the matrix: | |
| 41 | + Eigen::MatrixXf removeRowCol(const Eigen::MatrixXf X, int row, int col); | |
| 42 | + | |
| 43 | + // Convert a point list into a matrix: | |
| 44 | + Eigen::MatrixXf pointsToMatrix(const QList<QPointF> points, bool isAffine=false); | |
| 45 | + QList<QPointF> matrixToPoints(const Eigen::MatrixXf P); | |
| 46 | + | |
| 47 | + // Compute the element-wise standard deviation | |
| 48 | + float eigStd(const Eigen::MatrixXf& x); | |
| 49 | + | |
| 50 | + // Convert cv::Mat to Eigen | |
| 51 | + Eigen::MatrixXf toEigen(const cv::Mat m); | |
| 52 | +} | |
| 47 | 53 | |
| 48 | 54 | template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> |
| 49 | 55 | 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 |
| 83 | 89 | return stream; |
| 84 | 90 | } |
| 85 | 91 | |
| 86 | -/*Compute the mean of the each column (dim == 1) or row (dim == 2) | |
| 87 | - of the matrix*/ | |
| 88 | -template<typename T> | |
| 89 | -Eigen::MatrixBase<T> eigMean(const Eigen::MatrixBase<T>& x,int dim) | |
| 90 | -{ | |
| 91 | - if (dim == 1) { | |
| 92 | - Eigen::MatrixBase<T> y(1,x.cols()); | |
| 93 | - for (int i = 0; i < x.cols(); i++) | |
| 94 | - y(i) = x.col(i).sum() / x.rows(); | |
| 95 | - return y; | |
| 96 | - } else if (dim == 2) { | |
| 97 | - Eigen::MatrixBase<T> y(x.rows(),1); | |
| 98 | - for (int i = 0; i < x.rows(); i++) | |
| 99 | - y(i) = x.row(i).sum() / x.cols(); | |
| 100 | - return y; | |
| 101 | - } | |
| 102 | - qFatal("A matrix can only have two dimensions"); | |
| 103 | -} | |
| 104 | - | |
| 105 | -/*Compute the element-wise mean*/ | |
| 106 | -float eigMean(const Eigen::MatrixXf& x); | |
| 107 | -/*Compute the element-wise mean*/ | |
| 108 | -float eigStd(const Eigen::MatrixXf& x); | |
| 109 | - | |
| 110 | -/*Compute the std dev of the each column (dim == 1) or row (dim == 2) | |
| 111 | - of the matrix*/ | |
| 112 | -template<typename T> | |
| 113 | -Eigen::MatrixBase<T> eigStd(const Eigen::MatrixBase<T>& x,int dim) | |
| 114 | -{ | |
| 115 | - Eigen::MatrixBase<T> mean = eigMean(x, dim); | |
| 116 | - if (dim == 1) { | |
| 117 | - Eigen::MatrixBase<T> y(1,x.cols()); | |
| 118 | - for (int i = 0; i < x.cols(); i++) { | |
| 119 | - T value = 0; | |
| 120 | - for (int j = 0; j < x.rows(); j++) | |
| 121 | - value += pow(y(j, i) - mean(i), 2); | |
| 122 | - y(i) = sqrt(value / (x.rows() - 1)); | |
| 123 | - } | |
| 124 | - return y; | |
| 125 | - } else if (dim == 2) { | |
| 126 | - Eigen::MatrixBase<T> y(x.rows(),1); | |
| 127 | - for (int i = 0; i < x.rows(); i++) { | |
| 128 | - T value = 0; | |
| 129 | - for (int j = 0; j < x.cols(); j++) | |
| 130 | - value += pow(y(i, j) - mean(j), 2); | |
| 131 | - y(i) = sqrt(value / (x.cols() - 1)); | |
| 132 | - } | |
| 133 | - return y; | |
| 134 | - } | |
| 135 | - qFatal("A matrix can only have two dimensions"); | |
| 136 | -} | |
| 137 | - | |
| 138 | 92 | #endif // EIGENUTILS_H | ... | ... |
openbr/plugins/eigen3.cpp
| ... | ... | @@ -600,7 +600,7 @@ class SparseLDATransform : public Transform |
| 600 | 600 | |
| 601 | 601 | //Only works on binary class problems for now |
| 602 | 602 | assert(ldaOrig.projection.cols() == 1); |
| 603 | - float ldaStd = eigStd(ldaOrig.projection); | |
| 603 | + float ldaStd = EigenUtils::eigStd(ldaOrig.projection); | |
| 604 | 604 | for (int i = 0; i < ldaOrig.projection.rows(); i++) |
| 605 | 605 | if (abs(ldaOrig.projection(i)) > varThreshold * ldaStd) |
| 606 | 606 | selections.append(i); | ... | ... |
openbr/plugins/stasm4.cpp
| ... | ... | @@ -180,7 +180,7 @@ private: |
| 180 | 180 | void project(const Template &src, Template &dst) const |
| 181 | 181 | { |
| 182 | 182 | QList<float> paramList = src.file.getList<float>("affineParameters"); |
| 183 | - Eigen::MatrixXf points = pointsToMatrix(src.file.points(), true); | |
| 183 | + Eigen::MatrixXf points = EigenUtils::pointsToMatrix(src.file.points(), true); | |
| 184 | 184 | Eigen::MatrixXf affine = Eigen::MatrixXf::Zero(3, 3); |
| 185 | 185 | for (int i = 0, cnt = 0; i < 2; i++) |
| 186 | 186 | for (int j = 0; j < 3; j++, cnt++) |
| ... | ... | @@ -192,7 +192,7 @@ private: |
| 192 | 192 | points = affineInv * pointsT; |
| 193 | 193 | dst = src; |
| 194 | 194 | dst.file.clearPoints(); |
| 195 | - dst.file.setPoints(matrixToPoints(points.transpose())); | |
| 195 | + dst.file.setPoints(EigenUtils::matrixToPoints(points.transpose())); | |
| 196 | 196 | } |
| 197 | 197 | }; |
| 198 | 198 | ... | ... |