mexopencv  0.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;
17 map<int,Ptr<PCA> > obj_;
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  return;
48  }
49 
50  // Big operation switch
51  Ptr<PCA> obj = obj_[id];
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  }
56  else if (method == "read") {
57  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
58  bool loadFromString = false;
59  for (int i=3; i<nrhs; i+=2) {
60  string key(rhs[i].toString());
61  if (key=="FromString")
62  loadFromString = rhs[i+1].toBool();
63  else
64  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
65  }
66  FileStorage fs(rhs[2].toString(), FileStorage::READ +
67  (loadFromString ? FileStorage::MEMORY : 0));
68  if (!fs.isOpened())
69  mexErrMsgIdAndTxt("mexopencv:error","Failed to open file");
70  obj->read(fs.root());
71  }
72  else if (method == "write") {
73  nargchk(nrhs==3 && nlhs==0);
74  FileStorage fs(rhs[2].toString(), FileStorage::WRITE);
75  obj->write(fs);
76  }
77  else if (method == "compute") {
78  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
79  Mat mean;
80  int flags = PCA::DATA_AS_ROW;
81  int maxComponents = 0;
82  double retainedVariance = 1.0;
83  bool use_second_variant = false;
84  for (int i=3; i<nrhs; i+=2) {
85  string key(rhs[i].toString());
86  if (key=="Mean")
87  mean = rhs[i+1].toMat();
88  else if (key=="DataAs")
89  flags = DataAs[rhs[i+1].toString()];
90  else if (key=="MaxComponents")
91  maxComponents = rhs[i+1].toInt();
92  else if (key=="RetainedVariance") {
93  retainedVariance = rhs[i+1].toDouble();
94  use_second_variant = true;
95  }
96  else
97  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
98  }
99  Mat data(rhs[2].toMat());
100  if (use_second_variant)
101  obj->operator()(data, mean, flags, retainedVariance);
102  else
103  obj->operator()(data, mean, flags, maxComponents);
104  }
105  else if (method == "project") {
106  nargchk(nrhs==3 && nlhs<=1);
107  plhs[0] = MxArray(obj->project(rhs[2].toMat()));
108  }
109  else if (method == "backProject") {
110  nargchk(nrhs==3 && nlhs<=1);
111  plhs[0] = MxArray(obj->backProject(rhs[2].toMat()));
112  }
113  else if (method == "get") {
114  nargchk(nrhs==3 && nlhs<=1);
115  string prop(rhs[2].toString());
116  if (prop == "eigenvectors")
117  plhs[0] = MxArray(obj->eigenvectors);
118  else if (prop == "eigenvalues")
119  plhs[0] = MxArray(obj->eigenvalues);
120  else if (prop == "mean")
121  plhs[0] = MxArray(obj->mean);
122  else
123  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
124  }
125  else if (method == "set") {
126  nargchk(nrhs==4 && nlhs==0);
127  string prop(rhs[2].toString());
128  if (prop == "eigenvectors")
129  obj->eigenvectors = rhs[3].toMat();
130  else if (prop == "eigenvalues")
131  obj->eigenvalues = rhs[3].toMat();
132  else if (prop == "mean")
133  obj->mean = rhs[3].toMat();
134  else
135  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
136  }
137  else
138  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
139 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: PCA_.cpp:32
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.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927