Commit 66db5e4b4c5e310838caf641b9138e838257e324

Authored by Scott Klum
1 parent 18c305f1

Cleaned up writeMatrix

openbr/core/eigenutils.cpp
@@ -4,47 +4,6 @@ @@ -4,47 +4,6 @@
4 using namespace Eigen; 4 using namespace Eigen;
5 using namespace cv; 5 using namespace cv;
6 6
7 -//Helper function to quickly write eigen matrix to disk. Not efficient.  
8 -void EigenUtils::writeEigen(MatrixXf X, QString filename) {  
9 - Mat m(X.rows(),X.cols(),CV_32FC1);  
10 - for (int i = 0; i < X.rows(); i++) {  
11 - for (int j = 0; j < X.cols(); j++) {  
12 - m.at<float>(i,j) = X(i,j);  
13 - }  
14 - }  
15 - QScopedPointer<br::Format> format(br::Factory<br::Format>::make(filename));  
16 - format->write(br::Template(m));  
17 -}  
18 -  
19 -void EigenUtils::writeEigen(MatrixXd X, QString filename) {  
20 - Mat m(X.rows(),X.cols(),CV_32FC1);  
21 - for (int i = 0; i < X.rows(); i++) {  
22 - for (int j = 0; j < X.cols(); j++) {  
23 - m.at<float>(i,j) = (float)X(i,j);  
24 - }  
25 - }  
26 - QScopedPointer<br::Format> format(br::Factory<br::Format>::make(filename));  
27 - format->write(br::Template(m));  
28 -}  
29 -  
30 -void EigenUtils::writeEigen(VectorXd X, QString filename) {  
31 - Mat m(X.size(),1,CV_32FC1);  
32 - for (int i = 0; i < X.rows(); i++) {  
33 - m.at<float>(i,0) = (float)X(i);  
34 - }  
35 - QScopedPointer<br::Format> format(br::Factory<br::Format>::make(filename));  
36 - format->write(br::Template(m));  
37 -}  
38 -  
39 -void EigenUtils::writeEigen(VectorXf X, QString filename) {  
40 - Mat m(X.size(),1,CV_32FC1);  
41 - for (int i = 0; i < X.rows(); i++) {  
42 - m.at<float>(i,0) = X(i);  
43 - }  
44 - QScopedPointer<br::Format> format(br::Factory<br::Format>::make(filename));  
45 - format->write(br::Template(m));  
46 -}  
47 -  
48 void EigenUtils::printSize(Eigen::MatrixXf X) { 7 void EigenUtils::printSize(Eigen::MatrixXf X) {
49 qDebug() << "Rows=" << X.rows() << "\tCols=" << X.cols(); 8 qDebug() << "Rows=" << X.rows() << "\tCols=" << X.cols();
50 } 9 }
openbr/core/eigenutils.h
@@ -22,8 +22,11 @@ @@ -22,8 +22,11 @@
22 #include <Eigen/Core> 22 #include <Eigen/Core>
23 #include <assert.h> 23 #include <assert.h>
24 24
  25 +#include <opencv2/core/eigen.hpp>
25 #include <opencv2/core/core.hpp> 26 #include <opencv2/core/core.hpp>
26 27
  28 +#include "openbr/core/qtutils.h"
  29 +
27 namespace EigenUtils 30 namespace EigenUtils
28 { 31 {
29 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols> 32 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
@@ -45,10 +48,21 @@ namespace EigenUtils @@ -45,10 +48,21 @@ namespace EigenUtils
45 return result; 48 return result;
46 } 49 }
47 50
48 - void writeEigen(Eigen::MatrixXf X, QString filename);  
49 - void writeEigen(Eigen::MatrixXd X, QString filename);  
50 - void writeEigen(Eigen::VectorXd X, QString filename);  
51 - void writeEigen(Eigen::VectorXf X, QString filename); 51 + template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
  52 + void writeMatrix(const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &mat, const QString &filename)
  53 + {
  54 + int r = mat.rows();
  55 + int c = mat.cols();
  56 +
  57 + _Scalar *data = new _Scalar[r*c];
  58 + for (int i=0; i<r; i++)
  59 + for (int j=0; j<c; j++)
  60 + data[i*c+j] = mat(i, j);
  61 + int bytes = r*c*sizeof(_Scalar);
  62 + QByteArray byteArray((const char*)data,bytes);
  63 + QtUtils::writeFile(filename,byteArray);
  64 + }
  65 +
52 void printSize(Eigen::MatrixXf X); 66 void printSize(Eigen::MatrixXf X);
53 67
54 // Converts x y points in a single vector to two column matrix 68 // Converts x y points in a single vector to two column matrix