19#include <opencv2/core/core.hpp>
20#include <Core/NeMatlabIOContainer.h>
21#include <Core/NeEFStream.h>
22#include <Core/NeTypetraits.h>
71 std::string
Filename(
void) {
return std::string(filename_); }
83 bool Open(std::string filename, std::string mode);
114 std::vector<MatlabIOContainer>
Read(
void);
128 void Whos(std::vector<MatlabIOContainer> variables)
const;
132 T Find(std::vector<MatlabIOContainer>& variables, std::string name)
const
134 for (
unsigned int n = 0; n < variables.size(); ++n) {
135 if (variables[n].Name().compare(name) == 0) {
136 if (isPrimitiveType<T>()) {
137 return variables[n].Data<cv::Mat>().at<T>(0);
140 return variables[n].Data<T>();
144 throw new std::exception();
147 MatlabIOContainer Find(std::vector<MatlabIOContainer>& variables, std::string name)
const
149 for (
unsigned int n = 0; n < variables.size(); ++n) {
150 if (variables[n].Name().compare(name) == 0)
return variables[n];
152 throw new std::exception();
156 bool TypeEquals(std::vector<MatlabIOContainer>& variables, std::string name)
const
158 for (
unsigned int n = 0; n < variables.size(); ++n) {
159 if (variables[n].Name().compare(name) == 0)
return variables[n].TypeEquals<T>();
165 bool IsPrimitiveType(
void)
const
167 if (
typeid(T) ==
typeid(uint8_t) ||
typeid(T) ==
typeid(int8_t) ||
168 typeid(T) ==
typeid(uint16_t) ||
typeid(T) ==
typeid(int16_t) ||
169 typeid(T) ==
typeid(uint32_t) ||
typeid(T) ==
typeid(int32_t) ||
170 typeid(T) ==
typeid(
float) ||
typeid(T) ==
typeid(
double) ||
171 typeid(T) ==
typeid(uchar) ||
typeid(T) ==
typeid(
char) ||
172 typeid(T) ==
typeid(
bool)) {
198 bool HasVariable(
void) {
return fid_.peek() != EOF; }
284 const char*
ReadVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes,
const char *data);
326 std::vector<char>
UncompressVariable(uint32_t& data_type, uint32_t& dbytes, uint32_t& wbytes,
const std::vector<char> &data);
393 template<
class T1,
class T2> std::vector<T2> ConvertPrimitiveType(
const std::vector<char>& in);
408 template<
typename T> T Product(
const std::vector<T>& vec);
434 template<
class T>
MatlabIOContainer ConstructMatrix(std::vector<char>& name, std::vector<uint32_t>& dims, std::vector<char>& real, std::vector<char>& imag, uint32_t stor_type);
437 static const int HEADER_LENGTH = 116;
438 static const int SUBSYS_LENGTH = 8;
439 static const int ENDIAN_LENGTH = 2;
440 char header_[HEADER_LENGTH + 1];
441 char subsys_[SUBSYS_LENGTH + 1];
442 char endian_[ENDIAN_LENGTH + 1];
446 std::string filename_;
450 template<
class T1,
class T2>
456 const unsigned int T1_size = in.size() /
sizeof(T1);
460 const T1* in_ptr =
reinterpret_cast<const T1*
>(&(in[0]));
463 std::vector<T2> tmp(in_ptr, in_ptr + T1_size);
475 for (
unsigned int n = 0; n < vec.size(); ++n) acc *= vec[n];
482 std::vector<T> vec_real;
483 std::vector<T> vec_imag;
484 std::vector<cv::Mat> vec_mat;
489 vec_real = ConvertPrimitiveType<int8_t, T>(real);
490 vec_imag = ConvertPrimitiveType<int8_t, T>(imag);
493 vec_real = ConvertPrimitiveType<uint8_t, T>(real);
494 vec_imag = ConvertPrimitiveType<uint8_t, T>(imag);
497 vec_real = ConvertPrimitiveType<int16_t, T>(real);
498 vec_imag = ConvertPrimitiveType<int16_t, T>(imag);
501 vec_real = ConvertPrimitiveType<uint16_t, T>(real);
502 vec_imag = ConvertPrimitiveType<uint16_t, T>(imag);
505 vec_real = ConvertPrimitiveType<int32_t, T>(real);
506 vec_imag = ConvertPrimitiveType<int32_t, T>(imag);
509 vec_real = ConvertPrimitiveType<uint32_t, T>(real);
510 vec_imag = ConvertPrimitiveType<uint32_t, T>(imag);
513 vec_real = ConvertPrimitiveType<int64_t, T>(real);
514 vec_imag = ConvertPrimitiveType<int64_t, T>(imag);
517 vec_real = ConvertPrimitiveType<uint64_t, T>(real);
518 vec_imag = ConvertPrimitiveType<uint64_t, T>(imag);
521 vec_real = ConvertPrimitiveType<float, T>(real);
522 vec_imag = ConvertPrimitiveType<float, T>(imag);
525 vec_real = ConvertPrimitiveType<double, T>(real);
526 vec_imag = ConvertPrimitiveType<double, T>(imag);
529 vec_real = ConvertPrimitiveType<char, T>(real);
530 vec_imag = ConvertPrimitiveType<char, T>(imag);
538 for (
unsigned int n = 0; n < dims.size(); ++n) numel *= dims[n];
539 assert(vec_real.size() == numel);
545 const unsigned int channels = dims.size() == 3 ? dims[2] : 1;
546 bool complx = vec_imag.size() != 0;
549 std::vector<cv::Mat> sflat;
550 flat = cv::Mat(vec_real,
true);
551 for (
unsigned int n = 0; n < channels; ++n)
552 sflat.push_back(flat(cv::Range(dims[0] * dims[1] * n, dims[0] * dims[1] * (n + 1)), cv::Range::all()));
553 flat = cv::Mat(vec_imag,
true);
554 for (
unsigned int n = 0; n < channels*complx; ++n)
555 sflat.push_back(flat(cv::Range(dims[0] * dims[1] * n, dims[0] * dims[1] * (n + 1)), cv::Range::all()));
561 mat = flat.reshape(flat.channels(), dims[1]);
A container class for storing type agnostic variables.
Matlab Mat file parser for C++ OpenCV.
MatlabIOContainer ConstructStruct(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real)
Constructs a structure.
MatlabIOContainer ConstructSparse(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real, std::vector< char > &imag)
Construct a sparse matrix.
MatlabIOContainer ReadVariable(uint32_t data_type, uint32_t nbytes, const std::vector< char > &data)
Interpret a variable from a binary block of data.
std::vector< char > UncompressVariable(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const std::vector< char > &data)
Uncompress a variable.
const char * ReadVariableTag(uint32_t &data_type, uint32_t &dbytes, uint32_t &wbytes, const char *data)
Interpret the variable header information.
std::vector< T2 > ConvertPrimitiveType(const std::vector< char > &in)
Convert the type of a variable.
std::string Filename(void)
Gets the filename.
bool Close(void)
Close the filestream and release all resources.
MatlabIOContainer ConstructCell(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real)
Constructs a cell array.
virtual ~MatlabIO()
Destructor.
void Whos(std::vector< MatlabIOContainer > variables) const
Print a formatted list of the contents of a file.
std::vector< MatlabIOContainer > Read(void)
Read all variables from a file.
MatlabIOContainer ConstructString(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real)
Constructs a string from an extracted set of fields.
MatlabIOContainer CollateMatrixFields(uint32_t data_type, uint32_t nbytes, std::vector< char > data)
Interpret all fields of a matrix.
void TransposeMat(const cv::Mat &src, cv::Mat &dst)
Transpose a multi-channel matrix.
MatlabIO()
Default constructor.
void GetHeader(void)
Gets the .Mat file header information.
MatlabIOContainer ConstructMatrix(std::vector< char > &name, std::vector< uint32_t > &dims, std::vector< char > &real, std::vector< char > &imag, uint32_t stor_type)
Construct a matrix from an extracted set of fields.
bool Open(std::string filename, std::string mode)
Open a filestream for reading or writing.
MatlabIOContainer ReadBlock(void)
Reads a block of data from the file being parsed.
T Product(const std::vector< T > &vec)
Product of the elements of a vector.