18 bool isa(
const FileNode& _node,
const string& _type_name)
20 if (_node.node->info && _node.node->info->type_name)
21 return string(_node.node->info->type_name) == _type_name;
30 void read(FileStorage& fs,
MxArray& x,
const FileNode& node)
32 if (node.type()==FileNode::SEQ) {
34 vector<MxArray> v(n,
MxArray(static_cast<mxArray*>(NULL)));
35 for (
int i=0; i<n; ++i) {
36 const FileNode& elem = node[i];
37 int type = elem.type();
38 if (type==FileNode::INT)
39 v[i] =
MxArray(static_cast<int>(elem));
40 else if (type==FileNode::REAL)
41 v[i] =
MxArray(static_cast<double>(elem));
42 else if (type==FileNode::STR)
43 v[i] =
MxArray(static_cast<string>(elem));
44 else if (type==FileNode::SEQ) {
45 MxArray y(static_cast<mxArray*>(NULL));
49 else if (type==FileNode::MAP) {
50 if (isa(elem,
"opencv-matrix")) {
55 else if (isa(elem,
"opencv-nd-matrix")) {
69 else if (node.type()==FileNode::MAP) {
70 for (FileNodeIterator it=node.begin(); it!=node.end(); ++it) {
71 const FileNode& elem = (*it);
72 int type = elem.type();
73 if (type==FileNode::INT)
74 x.
set(elem.name(),
static_cast<int>(elem));
75 else if (type==FileNode::REAL)
76 x.
set(elem.name(),
static_cast<double>(elem));
77 else if (type==FileNode::STR)
78 x.
set(elem.name(),
static_cast<string>(elem));
79 else if (type==FileNode::SEQ) {
80 MxArray y(static_cast<mxArray*>(NULL));
82 x.
set(elem.name(), y);
84 else if (type==FileNode::MAP) {
85 if (isa(elem,
"opencv-matrix")) {
88 x.
set(elem.name(), m);
90 else if (isa(elem,
"opencv-nd-matrix")) {
93 x.
set(elem.name(), m);
98 x.
set(elem.name(), y);
110 void write(FileStorage& fs,
const MxArray& x,
bool root=
false)
112 mxClassID classid = x.
classID();
113 if (classid == mxFUNCTION_CLASS || classid==mxUNKNOWN_CLASS)
114 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid MxArray");
115 if (classid == mxSTRUCT_CLASS) {
119 for (
int i=0; i<n; ++i) {
120 if (!root) fs <<
"{";
121 for (vector<string>::iterator it=fields.begin(); it<fields.end(); ++it) {
125 if (!root) fs <<
"}";
129 else if (classid == mxCELL_CLASS) {
132 for (vector<MxArray>::iterator it=array.begin(); it<array.end(); ++it)
136 else if (classid == mxCHAR_CLASS) {
163 int nrhs,
const mxArray *prhs[] )
166 if (!(nrhs>=2&&nlhs==0)&&!(nrhs==1&&nlhs<=1))
167 mexErrMsgIdAndTxt(
"mexopencv:error",
"Wrong number of arguments");
169 string filename(
MxArray(prhs[0]).toString());
172 FileStorage fs(filename, FileStorage::READ);
173 FileNode node = fs.root();
180 vector<MxArray> rhs(prhs+1,prhs+nrhs);
181 FileStorage fs(filename, FileStorage::WRITE);
182 if (rhs[0].isStruct() && rhs[0].numel()==1)
184 write(fs, rhs[0],
true);
187 string nodeName(FileStorage::getDefaultObjectName(filename));
190 s.
set(nodeName, rhs[0].clone());
193 for (
int i=0; i<rhs.size(); ++i)
194 cell.
set(i, rhs[i].clone());
195 s.
set(nodeName, cell);
void destroy()
Deallocate memory occupied by mxArray.
bool isSingle() const
Determine whether array represents data as single-precision, floating-point numbers.
std::vector< std::string > fieldnames() const
Get field names of a struct array.
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
double toDouble() const
Convert MxArray to double.
int toInt() const
Convert MxArray to int.
mxClassID classID() const
Class ID of mxArray.
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
mxArray object wrapper for data conversion and manipulation.
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
static MxArray Cell(mwSize m=1, mwSize n=1)
Create a new cell array.
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mwSize numel() const
Number of elements in an array.
bool isDouble() const
Determine whether mxArray represents data as double-precision, floating-point numbers.
T at(mwIndex index) const
Template for numeric array element accessor.
std::string toString() const
Convert MxArray to std::string.
cv::Mat toMat(int depth=CV_USRTYPE1, bool transpose=true) const
Convert MxArray to cv::Mat.