mexopencv  3.4.1
MEX interface for OpenCV library
PCA_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
13 // Persistent objects
15 int last_id = 0;
18 
21  ("Row", PCA::DATA_AS_ROW)
22  ("Col", PCA::DATA_AS_COL);
23 }
24 
32 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
33 {
34  // Check the number of arguments
35  nargchk(nrhs>=2 && nlhs<=1);
36 
37  // Argument vector
38  vector<MxArray> rhs(prhs, prhs+nrhs);
39  int id = rhs[0].toInt();
40  string method(rhs[1].toString());
41 
42  // Constructor call
43  if (method == "new") {
44  nargchk(nrhs==2 && nlhs<=1);
45  obj_[++last_id] = makePtr<PCA>();
46  plhs[0] = MxArray(last_id);
47  mexLock();
48  return;
49  }
50 
51  // Big operation switch
52  Ptr<PCA> obj = obj_[id];
53  if (obj.empty())
54  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
55  if (method == "delete") {
56  nargchk(nrhs==2 && nlhs==0);
57  obj_.erase(id);
58  mexUnlock();
59  }
60  else if (method == "read") {
61  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
62  bool loadFromString = false;
63  for (int i=3; i<nrhs; i+=2) {
64  string key(rhs[i].toString());
65  if (key == "FromString")
66  loadFromString = rhs[i+1].toBool();
67  else
68  mexErrMsgIdAndTxt("mexopencv:error",
69  "Unrecognized option %s", key.c_str());
70  }
71  FileStorage fs(rhs[2].toString(), FileStorage::READ +
72  (loadFromString ? FileStorage::MEMORY : 0));
73  if (!fs.isOpened())
74  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
75  obj->read(fs.root());
76  }
77  else if (method == "write") {
78  nargchk(nrhs==3 && nlhs<=1);
79  FileStorage fs(rhs[2].toString(), FileStorage::WRITE +
80  ((nlhs > 0) ? FileStorage::MEMORY : 0));
81  if (!fs.isOpened())
82  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
83  obj->write(fs);
84  if (nlhs > 0)
85  plhs[0] = MxArray(fs.releaseAndGetString());
86  }
87  else if (method == "compute") {
88  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
89  Mat mean;
90  int flags = PCA::DATA_AS_ROW;
91  int maxComponents = 0;
92  double retainedVariance = 1.0;
93  bool use_second_variant = false;
94  for (int i=3; i<nrhs; i+=2) {
95  string key(rhs[i].toString());
96  if (key == "Mean")
97  mean = rhs[i+1].toMat();
98  else if (key == "DataAs")
99  flags = DataAs[rhs[i+1].toString()];
100  else if (key == "MaxComponents")
101  maxComponents = rhs[i+1].toInt();
102  else if (key == "RetainedVariance") {
103  retainedVariance = rhs[i+1].toDouble();
104  use_second_variant = true;
105  }
106  else
107  mexErrMsgIdAndTxt("mexopencv:error",
108  "Unrecognized option %s", key.c_str());
109  }
110  Mat data(rhs[2].toMat());
111  if (use_second_variant)
112  obj->operator()(data, mean, flags, retainedVariance);
113  else
114  obj->operator()(data, mean, flags, maxComponents);
115  }
116  else if (method == "project") {
117  nargchk(nrhs==3 && nlhs<=1);
118  plhs[0] = MxArray(obj->project(rhs[2].toMat()));
119  }
120  else if (method == "backProject") {
121  nargchk(nrhs==3 && nlhs<=1);
122  plhs[0] = MxArray(obj->backProject(rhs[2].toMat()));
123  }
124  else if (method == "get") {
125  nargchk(nrhs==3 && nlhs<=1);
126  string prop(rhs[2].toString());
127  if (prop == "eigenvectors")
128  plhs[0] = MxArray(obj->eigenvectors);
129  else if (prop == "eigenvalues")
130  plhs[0] = MxArray(obj->eigenvalues);
131  else if (prop == "mean")
132  plhs[0] = MxArray(obj->mean);
133  else
134  mexErrMsgIdAndTxt("mexopencv:error",
135  "Unrecognized property %s", prop.c_str());
136  }
137  else if (method == "set") {
138  nargchk(nrhs==4 && nlhs==0);
139  string prop(rhs[2].toString());
140  if (prop == "eigenvectors")
141  obj->eigenvectors = rhs[3].toMat();
142  else if (prop == "eigenvalues")
143  obj->eigenvalues = rhs[3].toMat();
144  else if (prop == "mean")
145  obj->mean = rhs[3].toMat();
146  else
147  mexErrMsgIdAndTxt("mexopencv:error",
148  "Unrecognized property %s", prop.c_str());
149  }
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized operation %s", method.c_str());
153 }
Scalar mean(InputArray src, InputArray mask=noArray())
Mat project(InputArray vec) const
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: PCA_.cpp:32
void read(const FileNode &fn)
FileNode root(int streamidx=0) const
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
int last_id
Last object id to allocate.
Definition: PCA_.cpp:15
Mat eigenvectors
map< int, Ptr< PCA > > obj_
Object container.
Definition: PCA_.cpp:17
virtual String releaseAndGetString()
Mat backProject(InputArray vec) const
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
STL class.
bool empty() const
Global constant definitions.
T c_str(T... args)
Mat eigenvalues
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
void write(FileStorage &fs) const
cv::Mat toMat() const
const ConstMap< std::string, int > DataAs
Data arrangement options.
Definition: PCA_.cpp:20