mexopencv  0.1
mex interface for opencv library
SVD_.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<SVD> > 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<=3);
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 && nlhs<=1);
40  obj_[++last_id] = makePtr<SVD>();
41  plhs[0] = MxArray(last_id);
42  return;
43  }
44  else if (method == "backSubst_static") {
45  nargchk(nrhs==6 && nlhs<=1);
46  Mat w(rhs[2].toMat()),
47  u(rhs[3].toMat()),
48  vt(rhs[4].toMat()),
49  src(rhs[5].toMat()),
50  dst;
51  SVD::backSubst(w, u, vt, src, dst);
52  plhs[0] = MxArray(dst);
53  return;
54  }
55  else if (method == "solveZ_static") {
56  nargchk(nrhs==3 && nlhs<=1);
57  Mat src(rhs[2].toMat()), dst;
58  SVD::solveZ(src, dst);
59  plhs[0] = MxArray(dst);
60  return;
61  }
62  else if (method == "compute_static") {
63  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=3);
64  int flags = 0;
65  for (int i=3; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key=="Flags")
68  flags = rhs[i+1].toInt();
69  else if (key=="ModifyA")
70  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::MODIFY_A);
71  else if (key=="NoUV")
72  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::NO_UV);
73  else if (key=="FullUV")
74  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::FULL_UV);
75  else
76  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
77  }
78  Mat src(rhs[2].toMat()), w, u, vt;
79  SVD::compute(src, w, u, vt, flags);
80  plhs[0] = MxArray(w);
81  if (nlhs>1)
82  plhs[1] = MxArray(u);
83  if (nlhs>2)
84  plhs[2] = MxArray(vt);
85  return;
86  }
87 
88  // Big operation switch
89  Ptr<SVD> obj = obj_[id];
90  if (method == "delete") {
91  nargchk(nrhs==2 && nlhs==0);
92  obj_.erase(id);
93  }
94  else if (method == "compute") {
95  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
96  int flags = 0;
97  for (int i=3; i<nrhs; i+=2) {
98  string key(rhs[i].toString());
99  if (key=="Flags")
100  flags = rhs[i+1].toInt();
101  else if (key=="ModifyA")
102  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::MODIFY_A);
103  else if (key=="NoUV")
104  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::NO_UV);
105  else if (key=="FullUV")
106  UPDATE_FLAG(flags, rhs[i+1].toBool(), SVD::FULL_UV);
107  else
108  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
109  }
110  Mat src(rhs[2].toMat());
111  obj->operator()(src, flags);
112  }
113  else if (method == "backSubst") {
114  nargchk(nrhs==3 && nlhs<=1);
115  Mat src(rhs[2].toMat()), dst;
116  obj->backSubst(src, dst);
117  plhs[0] = MxArray(dst);
118  }
119  else if (method == "get") {
120  nargchk(nrhs==3 && nlhs<=1);
121  string prop(rhs[2].toString());
122  if (prop == "u")
123  plhs[0] = MxArray(obj->u);
124  else if (prop == "vt")
125  plhs[0] = MxArray(obj->vt);
126  else if (prop == "w")
127  plhs[0] = MxArray(obj->w);
128  else
129  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
130  }
131  else if (method == "set") {
132  nargchk(nrhs==4 && nlhs==0);
133  string prop(rhs[2].toString());
134  if (prop == "u")
135  obj->u = rhs[3].toMat();
136  else if (prop == "vt")
137  obj->vt = rhs[3].toMat();
138  else if (prop == "w")
139  obj->w = rhs[3].toMat();
140  else
141  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
142  }
143  else
144  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
145 }
#define UPDATE_FLAG(NUM, TF, BIT)
set or clear a bit in flag depending on bool value
Definition: mexopencv.hpp:159
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: SVD_.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.