mexopencv  0.1
mex interface for opencv library
DualTVL1OpticalFlow_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 // Persistent objects
13 namespace {
15 int last_id = 0;
17 map<int,Ptr<DualTVL1OpticalFlow> > 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 && nlhs<=1);
40  obj_[++last_id] = createOptFlow_DualTVL1();
41  plhs[0] = MxArray(last_id);
42  return;
43  }
44 
45  // Big operation switch
46  Ptr<DualTVL1OpticalFlow> obj = obj_[id];
47  if (method == "delete") {
48  nargchk(nrhs==2 && nlhs==0);
49  obj_.erase(id);
50  }
51  else if (method == "clear") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj->clear();
54  }
55  else if (method == "save") {
56  nargchk(nrhs==3 && nlhs==0);
57  obj->save(rhs[2].toString());
58  }
59  else if (method == "load") {
60  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
61  string objname;
62  bool loadFromString = false;
63  for (int i=3; i<nrhs; i+=2) {
64  string key(rhs[i].toString());
65  if (key=="ObjName")
66  objname = rhs[i+1].toString();
67  else if (key=="FromString")
68  loadFromString = rhs[i+1].toBool();
69  else
70  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
71  }
72  /*
73  obj_[id] = (loadFromString ?
74  Algorithm::loadFromString<DualTVL1OpticalFlow>(rhs[2].toString(), objname) :
75  Algorithm::load<DualTVL1OpticalFlow>(rhs[2].toString(), objname));
76  */
78  // HACK: workaround for missing DualTVL1OpticalFlow::create()
79  FileStorage fs(rhs[2].toString(), FileStorage::READ +
80  (loadFromString ? FileStorage::MEMORY : 0));
81  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
82  if (obj.empty())
83  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
84  //*/
85  }
86  else if (method == "empty") {
87  nargchk(nrhs==2 && nlhs<=1);
88  plhs[0] = MxArray(obj->empty());
89  }
90  else if (method == "getDefaultName") {
91  nargchk(nrhs==2 && nlhs<=1);
92  plhs[0] = MxArray(obj->getDefaultName());
93  }
94  else if (method == "calc") {
95  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
96  Mat flow;
97  for (int i=4; i<nrhs; i+=2) {
98  string key(rhs[i].toString());
99  if (key == "InitialFlow")
100  flow = rhs[i+1].toMat(CV_32F);
101  else
102  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
103  }
104  Mat I0(rhs[2].toMat(rhs[2].isSingle() ? CV_32F : CV_8U)),
105  I1(rhs[3].toMat(rhs[3].isSingle() ? CV_32F : CV_8U));
106  obj->calc(I0, I1, flow);
107  plhs[0] = MxArray(flow);
108  }
109  else if (method == "collectGarbage") {
110  nargchk(nrhs==2 && nlhs==0);
111  obj->collectGarbage();
112  }
113  else if (method == "get") {
114  nargchk(nrhs==3 && nlhs<=1);
115  string prop(rhs[2].toString());
116  if (prop == "Epsilon")
117  plhs[0] = MxArray(obj->getEpsilon());
118  else if (prop == "Gamma")
119  plhs[0] = MxArray(obj->getGamma());
120  else if (prop == "InnerIterations")
121  plhs[0] = MxArray(obj->getInnerIterations());
122  else if (prop == "Lambda")
123  plhs[0] = MxArray(obj->getLambda());
124  else if (prop == "MedianFiltering")
125  plhs[0] = MxArray(obj->getMedianFiltering());
126  else if (prop == "OuterIterations")
127  plhs[0] = MxArray(obj->getOuterIterations());
128  else if (prop == "ScalesNumber")
129  plhs[0] = MxArray(obj->getScalesNumber());
130  else if (prop == "ScaleStep")
131  plhs[0] = MxArray(obj->getScaleStep());
132  else if (prop == "Tau")
133  plhs[0] = MxArray(obj->getTau());
134  else if (prop == "Theta")
135  plhs[0] = MxArray(obj->getTheta());
136  else if (prop == "UseInitialFlow")
137  plhs[0] = MxArray(obj->getUseInitialFlow());
138  else if (prop == "WarpingsNumber")
139  plhs[0] = MxArray(obj->getWarpingsNumber());
140  else
141  mexErrMsgIdAndTxt("mexopencv:error",
142  "Unrecognized property %s", prop.c_str());
143  }
144  else if (method == "set") {
145  nargchk(nrhs==4 && nlhs==0);
146  string prop(rhs[2].toString());
147  if (prop == "Epsilon")
148  obj->setEpsilon(rhs[3].toDouble());
149  else if (prop == "Gamma")
150  obj->setGamma(rhs[3].toDouble());
151  else if (prop == "InnerIterations")
152  obj->setInnerIterations(rhs[3].toInt());
153  else if (prop == "Lambda")
154  obj->setLambda(rhs[3].toDouble());
155  else if (prop == "MedianFiltering")
156  obj->setMedianFiltering(rhs[3].toInt());
157  else if (prop == "OuterIterations")
158  obj->setOuterIterations(rhs[3].toInt());
159  else if (prop == "ScalesNumber")
160  obj->setScalesNumber(rhs[3].toInt());
161  else if (prop == "ScaleStep")
162  obj->setScaleStep(rhs[3].toDouble());
163  else if (prop == "Tau")
164  obj->setTau(rhs[3].toDouble());
165  else if (prop == "Theta")
166  obj->setTheta(rhs[3].toDouble());
167  else if (prop == "UseInitialFlow")
168  obj->setUseInitialFlow(rhs[3].toBool());
169  else if (prop == "WarpingsNumber")
170  obj->setWarpingsNumber(rhs[3].toInt());
171  else
172  mexErrMsgIdAndTxt("mexopencv:error",
173  "Unrecognized property %s", prop.c_str());
174  }
175  else
176  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
177 }
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
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.