mexopencv  0.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;
17 map<int,Ptr<LDA> > obj_;
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","Unrecognized option");
47  }
48  obj_[++last_id] = makePtr<LDA>(num_components);
49  plhs[0] = MxArray(last_id);
50  return;
51  }
52 
53  // Big operation switch
54  Ptr<LDA> obj = obj_[id];
55  if (method == "delete") {
56  nargchk(nrhs==2 && nlhs==0);
57  obj_.erase(id);
58  }
59  else if (method == "load") {
60  nargchk(nrhs==3 && nlhs==0);
61  obj->load(rhs[2].toString());
62  }
63  else if (method == "save") {
64  nargchk(nrhs==3 && nlhs==0);
65  obj->save(rhs[2].toString());
66  }
67  else if (method == "compute") {
68  nargchk(nrhs==4 && nlhs==0);
69  Mat labels(rhs[3].toMat(CV_32S));
70  if (rhs[2].isCell()) {
71  vector<Mat> src(rhs[2].toVector<Mat>());
72  obj->compute(src, labels);
73  }
74  else {
75  Mat src(rhs[2].toMat(CV_64F));
76  obj->compute(src, labels);
77  }
78  }
79  else if (method == "project") {
80  nargchk(nrhs==3 && nlhs<=1);
81  Mat src(rhs[2].toMat(CV_64F));
82  plhs[0] = MxArray(obj->project(src));
83  }
84  else if (method == "reconstruct") {
85  nargchk(nrhs==3 && nlhs<=1);
86  Mat src(rhs[2].toMat(CV_64F));
87  plhs[0] = MxArray(obj->reconstruct(src));
88  }
89  else if (method == "get") {
90  nargchk(nrhs==3 && nlhs<=1);
91  string prop(rhs[2].toString());
92  if (prop == "eigenvalues")
93  plhs[0] = MxArray(obj->eigenvalues());
94  else if (prop == "eigenvectors")
95  plhs[0] = MxArray(obj->eigenvectors());
96  else
97  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
98  }
99  else
100  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
101 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: LDA_.cpp:27
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.