如何从二进制文件写入/读取一个特征矩阵 [英] How to write/read an Eigen matrix from binary file

查看:1045
本文介绍了如何从二进制文件写入/读取一个特征矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要写Eigen :: Matrix到文件我真的很喜欢使用以下:

  typedef Eigen :: Matrix  Matrix_MxN J = Matrix_MxN :: Zeros(10,10); 
std :: ofstream(matrix.txt)<< J;但是不幸的是,可以做相反的事情没有定义:



  std :: ifstream(matrix.txt)>> J; 

为了避免这个问题,如何读取/写入Eigen :: Matrix到二进制




 命名空间Eigen {
template< class Matrix>
void write_binary(const char * filename,const Matrix& matrix){
std :: ofstream out(filename,ios :: out | ios :: binary | ios :: trunc);
typename Matrix :: Index rows = matrix.rows(),cols = matrix.cols();
out.write((char *)(& rows),sizeof(typename Matrix :: Index));
out.write((char *)(& cols),sizeof(typename Matrix :: Index));
out.write((char *)matrix.data(),rows * cols * sizeof(typename Matrix :: Scalar));
out.close();
}
template< class Matrix>
void read_binary(const char * filename,Matrix& matrix){
std :: ifstream in(filename,ios :: in | std :: ios :: binary);
typename Matrix :: Index rows = 0,cols = 0;
in.read((char *)(& rows),sizeof(typename Matrix :: Index));
in.read((char *)(& cols),sizeof(typename Matrix :: Index));
matrix.resize(rows,cols);
in.read((char *)matrix.data(),rows * cols * sizeof(typename Matrix :: Scalar));
in.close();
}
} // Eigen ::

具有:

  typedef Eigen :: Matrix< float,Eigen :: Dynamic,Eigen :: Dynamic,Eigen :: ColMajor> Matrix_MxN; 
Matrix_MxN J = Matrix_MxN :: Random(10,5);
Eigen :: write_binary(matrix.dat,J);
std :: cout<< \\\
original \\\
<< J < std :: endl;
Matrix_MxN J_copy;
Eigen :: read_binary(matrix.dat,J_copy);
std :: cout<< \\\
copy \\\
< J_copy<< std :: endl;
cout.flush();'

如果你知道更好的方法,建议是欢迎!


To write Eigen::Matrix to file I really like to use the following:

typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix_MxN;
Matrix_MxN J = Matrix_MxN::Zeros(10,10);      
std::ofstream("matrix.txt") << J;

But unfortunately, something that can do the opposite is not defined:

std::ifstream("matrix.txt") >> J;

To circumvent this problem, how can you read/write an Eigen::Matrix to binary file instead?

解决方案

You can define these methods:

namespace Eigen{
template<class Matrix>
void write_binary(const char* filename, const Matrix& matrix){
    std::ofstream out(filename,ios::out | ios::binary | ios::trunc);
    typename Matrix::Index rows=matrix.rows(), cols=matrix.cols();
    out.write((char*) (&rows), sizeof(typename Matrix::Index));
    out.write((char*) (&cols), sizeof(typename Matrix::Index));
    out.write((char*) matrix.data(), rows*cols*sizeof(typename Matrix::Scalar) );
    out.close();
}
template<class Matrix>
void read_binary(const char* filename, Matrix& matrix){
    std::ifstream in(filename,ios::in | std::ios::binary);
    typename Matrix::Index rows=0, cols=0;
    in.read((char*) (&rows),sizeof(typename Matrix::Index));
    in.read((char*) (&cols),sizeof(typename Matrix::Index));
    matrix.resize(rows, cols);
    in.read( (char *) matrix.data() , rows*cols*sizeof(typename Matrix::Scalar) );
    in.close();
}
} // Eigen::

and you can test their usage with:

typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> Matrix_MxN; 
Matrix_MxN J = Matrix_MxN::Random(10,5);
Eigen::write_binary("matrix.dat",J);
std::cout << "\n original \n" << J << std::endl;
Matrix_MxN J_copy;
Eigen::read_binary("matrix.dat",J_copy);
std::cout << "\n copy \n" << J_copy << std::endl;
cout.flush();'

If you know of a better way, suggestions are welcome!

这篇关于如何从二进制文件写入/读取一个特征矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆