mexopencv  3.4.1
MEX interface for OpenCV library
DualTVL1OpticalFlow_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/video.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // constructor call
39  if (method == "new") {
40  nargchk(nrhs==2 && nlhs<=1);
42  plhs[0] = MxArray(last_id);
43  mexLock();
44  return;
45  }
46 
47  // Big operation switch
49  if (obj.empty())
50  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
51  if (method == "delete") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj_.erase(id);
54  mexUnlock();
55  }
56  else if (method == "clear") {
57  nargchk(nrhs==2 && nlhs==0);
58  obj->clear();
59  }
60  else if (method == "save") {
61  nargchk(nrhs==3 && nlhs==0);
62  obj->save(rhs[2].toString());
63  }
64  else if (method == "load") {
65  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
66  string objname;
67  bool loadFromString = false;
68  for (int i=3; i<nrhs; i+=2) {
69  string key(rhs[i].toString());
70  if (key == "ObjName")
71  objname = rhs[i+1].toString();
72  else if (key == "FromString")
73  loadFromString = rhs[i+1].toBool();
74  else
75  mexErrMsgIdAndTxt("mexopencv:error",
76  "Unrecognized option %s", key.c_str());
77  }
78  /*
79  obj_[id] = (loadFromString ?
80  Algorithm::loadFromString<DualTVL1OpticalFlow>(rhs[2].toString(), objname) :
81  Algorithm::load<DualTVL1OpticalFlow>(rhs[2].toString(), objname));
82  */
84  // HACK: workaround for missing DualTVL1OpticalFlow::create()
85  FileStorage fs(rhs[2].toString(), FileStorage::READ +
86  (loadFromString ? FileStorage::MEMORY : 0));
87  if (!fs.isOpened())
88  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
89  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
90  if (fn.empty())
91  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
92  obj->read(fn);
93  //*/
94  }
95  else if (method == "empty") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->empty());
98  }
99  else if (method == "getDefaultName") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getDefaultName());
102  }
103  else if (method == "calc") {
104  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
105  Mat flow;
106  for (int i=4; i<nrhs; i+=2) {
107  string key(rhs[i].toString());
108  if (key == "InitialFlow")
109  flow = rhs[i+1].toMat(CV_32F);
110  else
111  mexErrMsgIdAndTxt("mexopencv:error",
112  "Unrecognized option %s", key.c_str());
113  }
114  Mat I0(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
115  I1(rhs[3].toMat(rhs[3].isFloat() ? CV_32F : CV_8U));
116  obj->calc(I0, I1, flow);
117  plhs[0] = MxArray(flow);
118  }
119  else if (method == "collectGarbage") {
120  nargchk(nrhs==2 && nlhs==0);
121  obj->collectGarbage();
122  }
123  else if (method == "get") {
124  nargchk(nrhs==3 && nlhs<=1);
125  string prop(rhs[2].toString());
126  if (prop == "Epsilon")
127  plhs[0] = MxArray(obj->getEpsilon());
128  else if (prop == "Gamma")
129  plhs[0] = MxArray(obj->getGamma());
130  else if (prop == "InnerIterations")
131  plhs[0] = MxArray(obj->getInnerIterations());
132  else if (prop == "Lambda")
133  plhs[0] = MxArray(obj->getLambda());
134  else if (prop == "MedianFiltering")
135  plhs[0] = MxArray(obj->getMedianFiltering());
136  else if (prop == "OuterIterations")
137  plhs[0] = MxArray(obj->getOuterIterations());
138  else if (prop == "ScalesNumber")
139  plhs[0] = MxArray(obj->getScalesNumber());
140  else if (prop == "ScaleStep")
141  plhs[0] = MxArray(obj->getScaleStep());
142  else if (prop == "Tau")
143  plhs[0] = MxArray(obj->getTau());
144  else if (prop == "Theta")
145  plhs[0] = MxArray(obj->getTheta());
146  else if (prop == "UseInitialFlow")
147  plhs[0] = MxArray(obj->getUseInitialFlow());
148  else if (prop == "WarpingsNumber")
149  plhs[0] = MxArray(obj->getWarpingsNumber());
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized property %s", prop.c_str());
153  }
154  else if (method == "set") {
155  nargchk(nrhs==4 && nlhs==0);
156  string prop(rhs[2].toString());
157  if (prop == "Epsilon")
158  obj->setEpsilon(rhs[3].toDouble());
159  else if (prop == "Gamma")
160  obj->setGamma(rhs[3].toDouble());
161  else if (prop == "InnerIterations")
162  obj->setInnerIterations(rhs[3].toInt());
163  else if (prop == "Lambda")
164  obj->setLambda(rhs[3].toDouble());
165  else if (prop == "MedianFiltering")
166  obj->setMedianFiltering(rhs[3].toInt());
167  else if (prop == "OuterIterations")
168  obj->setOuterIterations(rhs[3].toInt());
169  else if (prop == "ScalesNumber")
170  obj->setScalesNumber(rhs[3].toInt());
171  else if (prop == "ScaleStep")
172  obj->setScaleStep(rhs[3].toDouble());
173  else if (prop == "Tau")
174  obj->setTau(rhs[3].toDouble());
175  else if (prop == "Theta")
176  obj->setTheta(rhs[3].toDouble());
177  else if (prop == "UseInitialFlow")
178  obj->setUseInitialFlow(rhs[3].toBool());
179  else if (prop == "WarpingsNumber")
180  obj->setWarpingsNumber(rhs[3].toInt());
181  else
182  mexErrMsgIdAndTxt("mexopencv:error",
183  "Unrecognized property %s", prop.c_str());
184  }
185  else
186  mexErrMsgIdAndTxt("mexopencv:error",
187  "Unrecognized operation %s", method.c_str());
188 }
T empty(T... args)
virtual double getGamma() const=0
virtual bool getUseInitialFlow() const=0
virtual void setEpsilon(double val)=0
virtual void collectGarbage()=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getWarpingsNumber() const=0
virtual void setGamma(double val)=0
#define CV_8U
virtual void setInnerIterations(int val)=0
virtual void setOuterIterations(int val)=0
virtual double getScaleStep() const=0
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual int getInnerIterations() const=0
virtual double getLambda() const=0
virtual void setMedianFiltering(int val)=0
virtual int getMedianFiltering() const=0
virtual void clear()
virtual void setUseInitialFlow(bool val)=0
virtual int getScalesNumber() const=0
#define CV_32F
virtual void read(const FileNode &fn)
virtual void setScalesNumber(int val)=0
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...
map< int, Ptr< DualTVL1OpticalFlow > > obj_
Object container.
virtual void setWarpingsNumber(int val)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual double getEpsilon() const=0
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
virtual double getTau() const=0
FileNode getFirstTopLevelNode() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL class.
bool empty() const
virtual void setTau(double val)=0
virtual String getDefaultName() const
Global constant definitions.
virtual void setTheta(double val)=0
T c_str(T... args)
virtual void setLambda(double val)=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow)=0
virtual int getOuterIterations() const=0
virtual double getTheta() const=0
virtual void setScaleStep(double val)=0
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const