mexopencv  3.4.1
MEX interface for OpenCV library
LDA_.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 }
19 
27 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
28 {
29  // Check the number of arguments
30  nargchk(nrhs>=2 && nlhs<=1);
31 
32  // Argument vector
33  vector<MxArray> rhs(prhs, prhs+nrhs);
34  int id = rhs[0].toInt();
35  string method(rhs[1].toString());
36 
37  // Constructor call
38  if (method == "new") {
39  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
40  int num_components = 0;
41  for (int i=2; i<nrhs; i+=2) {
42  string key(rhs[i].toString());
43  if (key == "NumComponents")
44  num_components = rhs[i+1].toInt();
45  else
46  mexErrMsgIdAndTxt("mexopencv:error",
47  "Unrecognized option %s", key.c_str());
48  }
49  obj_[++last_id] = makePtr<LDA>(num_components);
50  plhs[0] = MxArray(last_id);
51  mexLock();
52  return;
53  }
54  // static method calls
55  else if (method == "subspaceProject") {
56  nargchk(nrhs==5 && nlhs<=1);
57  Mat W(rhs[2].toMat(CV_64F)),
58  mean(rhs[3].toMat(CV_64F)),
59  src(rhs[4].toMat(CV_64F)),
60  dst;
61  dst = LDA::subspaceProject(W, mean, src);
62  plhs[0] = MxArray(dst);
63  return;
64  }
65  else if (method == "subspaceReconstruct") {
66  nargchk(nrhs==5 && nlhs<=1);
67  Mat W(rhs[2].toMat(CV_64F)),
68  mean(rhs[3].toMat(CV_64F)),
69  src(rhs[4].toMat(CV_64F)),
70  dst;
71  dst = LDA::subspaceReconstruct(W, mean, src);
72  plhs[0] = MxArray(dst);
73  return;
74  }
75 
76  // Big operation switch
77  Ptr<LDA> obj = obj_[id];
78  if (obj.empty())
79  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
80  if (method == "delete") {
81  nargchk(nrhs==2 && nlhs==0);
82  obj_.erase(id);
83  mexUnlock();
84  }
85  else if (method == "load") {
86  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
87  bool loadFromString = false;
88  for (int i=3; i<nrhs; i+=2) {
89  string key(rhs[i].toString());
90  if (key == "FromString")
91  loadFromString = rhs[i+1].toBool();
92  else
93  mexErrMsgIdAndTxt("mexopencv:error",
94  "Unrecognized option %s", key.c_str());
95  }
96  string fname(rhs[2].toString());
97  if (loadFromString) {
98  FileStorage fs(fname, FileStorage::READ + FileStorage::MEMORY);
99  if (!fs.isOpened())
100  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
101  obj->load(fs);
102  }
103  else
104  obj->load(fname);
105  }
106  else if (method == "save") {
107  nargchk(nrhs==3 && nlhs<=1);
108  string fname(rhs[2].toString());
109  if (nlhs > 0) {
110  // write to memory, and return string
111  FileStorage fs(fname, FileStorage::WRITE + FileStorage::MEMORY);
112  if (!fs.isOpened())
113  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
114  obj->save(fs);
115  plhs[0] = MxArray(fs.releaseAndGetString());
116  }
117  else
118  // write to disk
119  obj->save(fname);
120  }
121  else if (method == "compute") {
122  nargchk(nrhs==4 && nlhs==0);
123  Mat labels(rhs[3].toMat(CV_32S));
124  if (rhs[2].isCell()) {
125  vector<Mat> src(rhs[2].toVector<Mat>());
126  obj->compute(src, labels);
127  }
128  else {
129  Mat src(rhs[2].toMat(CV_64F));
130  obj->compute(src, labels);
131  }
132  }
133  else if (method == "project") {
134  nargchk(nrhs==3 && nlhs<=1);
135  Mat src(rhs[2].toMat(CV_64F));
136  plhs[0] = MxArray(obj->project(src));
137  }
138  else if (method == "reconstruct") {
139  nargchk(nrhs==3 && nlhs<=1);
140  Mat src(rhs[2].toMat(CV_64F));
141  plhs[0] = MxArray(obj->reconstruct(src));
142  }
143  else if (method == "get") {
144  nargchk(nrhs==3 && nlhs<=1);
145  string prop(rhs[2].toString());
146  if (prop == "eigenvalues")
147  plhs[0] = MxArray(obj->eigenvalues());
148  else if (prop == "eigenvectors")
149  plhs[0] = MxArray(obj->eigenvectors());
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized property %s", prop.c_str());
153  }
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized operation %s", method.c_str());
157 }
Scalar mean(InputArray src, InputArray mask=noArray())
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void load(const String &filename)
int last_id
Last object id to allocate.
Definition: LDA_.cpp:15
Mat eigenvalues() const
virtual String releaseAndGetString()
#define CV_64F
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...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: LDA_.cpp:27
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
void save(const String &filename) const
STL class.
bool empty() const
void compute(InputArrayOfArrays src, InputArray labels)
#define CV_32S
Global constant definitions.
T c_str(T... args)
Mat eigenvectors() const
Mat project(InputArray src)
Mat reconstruct(InputArray src)
map< int, Ptr< LDA > > obj_
Object container.
Definition: LDA_.cpp:17
cv::Mat toMat() const