mexopencv  3.4.1
MEX interface for OpenCV library
DISOpticalFlow_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/optflow.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::optflow;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 
26 }
27 
35 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
36 {
37  // Check the number of arguments
38  nargchk(nrhs>=2 && nlhs<=1);
39 
40  // Argument vector
41  vector<MxArray> rhs(prhs, prhs+nrhs);
42  int id = rhs[0].toInt();
43  string method(rhs[1].toString());
44 
45  // constructor call
46  if (method == "new") {
47  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
49  for (int i=2; i<nrhs; i+=2) {
50  string key(rhs[i].toString());
51  if (key == "Preset")
52  preset = DISPresetMap[rhs[i+1].toString()];
53  else
54  mexErrMsgIdAndTxt("mexopencv:error",
55  "Unrecognized option %s", key.c_str());
56  }
57  obj_[++last_id] = createOptFlow_DIS(preset);
58  plhs[0] = MxArray(last_id);
59  mexLock();
60  return;
61  }
62 
63  // Big operation switch
64  Ptr<DISOpticalFlow> obj = obj_[id];
65  if (obj.empty())
66  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
67  if (method == "delete") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj_.erase(id);
70  mexUnlock();
71  }
72  else if (method == "clear") {
73  nargchk(nrhs==2 && nlhs==0);
74  obj->clear();
75  }
76  else if (method == "save") {
77  nargchk(nrhs==3 && nlhs==0);
78  obj->save(rhs[2].toString());
79  }
80  else if (method == "load") {
81  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
82  string objname;
83  bool loadFromString = false;
84  for (int i=3; i<nrhs; i+=2) {
85  string key(rhs[i].toString());
86  if (key == "ObjName")
87  objname = rhs[i+1].toString();
88  else if (key == "FromString")
89  loadFromString = rhs[i+1].toBool();
90  else
91  mexErrMsgIdAndTxt("mexopencv:error",
92  "Unrecognized option %s", key.c_str());
93  }
94  /*
95  obj_[id] = (loadFromString ?
96  Algorithm::loadFromString<DISOpticalFlow>(rhs[2].toString(), objname) :
97  Algorithm::load<DISOpticalFlow>(rhs[2].toString(), objname));
98  */
100  // HACK: workaround for missing DISOpticalFlow::create()
101  FileStorage fs(rhs[2].toString(), FileStorage::READ +
102  (loadFromString ? FileStorage::MEMORY : 0));
103  if (!fs.isOpened())
104  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
105  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
106  if (fn.empty())
107  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
108  obj->read(fn);
109  //*/
110  }
111  else if (method == "empty") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(obj->empty());
114  }
115  else if (method == "getDefaultName") {
116  nargchk(nrhs==2 && nlhs<=1);
117  plhs[0] = MxArray(obj->getDefaultName());
118  }
119  else if (method == "calc") {
120  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
121  Mat flow;
122  for (int i=4; i<nrhs; i+=2) {
123  string key(rhs[i].toString());
124  if (key == "InitialFlow")
125  flow = rhs[i+1].toMat(CV_32F);
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  Mat I0(rhs[2].toMat(CV_8U)),
131  I1(rhs[3].toMat(CV_8U));
132  obj->calc(I0, I1, flow);
133  plhs[0] = MxArray(flow);
134  }
135  else if (method == "collectGarbage") {
136  nargchk(nrhs==2 && nlhs==0);
137  obj->collectGarbage();
138  }
139  else if (method == "get") {
140  nargchk(nrhs==3 && nlhs<=1);
141  string prop(rhs[2].toString());
142  if (prop == "FinestScale")
143  plhs[0] = MxArray(obj->getFinestScale());
144  else if (prop == "PatchSize")
145  plhs[0] = MxArray(obj->getPatchSize());
146  else if (prop == "PatchStride")
147  plhs[0] = MxArray(obj->getPatchStride());
148  else if (prop == "GradientDescentIterations")
149  plhs[0] = MxArray(obj->getGradientDescentIterations());
150  else if (prop == "VariationalRefinementIterations")
152  else if (prop == "VariationalRefinementAlpha")
153  plhs[0] = MxArray(obj->getVariationalRefinementAlpha());
154  else if (prop == "VariationalRefinementDelta")
155  plhs[0] = MxArray(obj->getVariationalRefinementDelta());
156  else if (prop == "VariationalRefinementGamma")
157  plhs[0] = MxArray(obj->getVariationalRefinementGamma());
158  else if (prop == "UseMeanNormalization")
159  plhs[0] = MxArray(obj->getUseMeanNormalization());
160  else if (prop == "UseSpatialPropagation")
161  plhs[0] = MxArray(obj->getUseSpatialPropagation());
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized property %s", prop.c_str());
165  }
166  else if (method == "set") {
167  nargchk(nrhs==4 && nlhs==0);
168  string prop(rhs[2].toString());
169  if (prop == "FinestScale")
170  obj->setFinestScale(rhs[3].toInt());
171  else if (prop == "PatchSize")
172  obj->setPatchSize(rhs[3].toInt());
173  else if (prop == "PatchStride")
174  obj->setPatchStride(rhs[3].toInt());
175  else if (prop == "GradientDescentIterations")
176  obj->setGradientDescentIterations(rhs[3].toInt());
177  else if (prop == "VariationalRefinementIterations")
178  obj->setVariationalRefinementIterations(rhs[3].toInt());
179  else if (prop == "VariationalRefinementAlpha")
180  obj->setVariationalRefinementAlpha(rhs[3].toFloat());
181  else if (prop == "VariationalRefinementDelta")
182  obj->setVariationalRefinementDelta(rhs[3].toFloat());
183  else if (prop == "VariationalRefinementGamma")
184  obj->setVariationalRefinementGamma(rhs[3].toFloat());
185  else if (prop == "UseMeanNormalization")
186  obj->setUseMeanNormalization(rhs[3].toBool());
187  else if (prop == "UseSpatialPropagation")
188  obj->setUseSpatialPropagation(rhs[3].toBool());
189  else
190  mexErrMsgIdAndTxt("mexopencv:error",
191  "Unrecognized property %s", prop.c_str());
192  }
193  else
194  mexErrMsgIdAndTxt("mexopencv:error",
195  "Unrecognized operation %s", method.c_str());
196 }
T empty(T... args)
virtual void setUseSpatialPropagation(bool val)=0
virtual void setVariationalRefinementDelta(float 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.
int last_id
Last object id to allocate.
#define CV_8U
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual bool getUseSpatialPropagation() const=0
STL namespace.
virtual float getVariationalRefinementDelta() const=0
virtual bool isOpened() const
virtual int getPatchStride() const=0
Ptr< DISOpticalFlow > createOptFlow_DIS(int preset=DISOpticalFlow::PRESET_FAST)
virtual void setPatchSize(int val)=0
virtual int getVariationalRefinementIterations() const=0
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual bool getUseMeanNormalization() const=0
virtual int getFinestScale() const=0
virtual void setUseMeanNormalization(bool val)=0
virtual void setPatchStride(int val)=0
virtual void clear()
virtual void setFinestScale(int val)=0
#define CV_32F
virtual void setVariationalRefinementAlpha(float val)=0
virtual void read(const FileNode &fn)
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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual float getVariationalRefinementGamma() const=0
virtual float getVariationalRefinementAlpha() const=0
virtual void setVariationalRefinementIterations(int val)=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
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
virtual int getGradientDescentIterations() const=0
virtual int getPatchSize() const=0
virtual void setGradientDescentIterations(int val)=0
T c_str(T... args)
map< int, Ptr< DISOpticalFlow > > obj_
Object container.
virtual void save(const String &filename) const
virtual bool empty() const
virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow)=0
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
virtual void setVariationalRefinementGamma(float val)=0
const ConstMap< string, int > DISPresetMap
DIS preset types.
cv::Mat toMat() const