mexopencv  3.4.1
MEX interface for OpenCV library
VariationalRefinement_.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 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // constructor call
40  if (method == "new") {
41  nargchk(nrhs==2 && nlhs<=1);
43  plhs[0] = MxArray(last_id);
44  mexLock();
45  return;
46  }
47 
48  // Big operation switch
50  if (obj.empty())
51  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  mexUnlock();
56  }
57  else if (method == "clear") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj->clear();
60  }
61  else if (method == "save") {
62  nargchk(nrhs==3 && nlhs==0);
63  obj->save(rhs[2].toString());
64  }
65  else if (method == "load") {
66  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
67  string objname;
68  bool loadFromString = false;
69  for (int i=3; i<nrhs; i+=2) {
70  string key(rhs[i].toString());
71  if (key == "ObjName")
72  objname = rhs[i+1].toString();
73  else if (key == "FromString")
74  loadFromString = rhs[i+1].toBool();
75  else
76  mexErrMsgIdAndTxt("mexopencv:error",
77  "Unrecognized option %s", key.c_str());
78  }
79  /*
80  obj_[id] = (loadFromString ?
81  Algorithm::loadFromString<VariationalRefinement>(rhs[2].toString(), objname) :
82  Algorithm::load<VariationalRefinement>(rhs[2].toString(), objname));
83  */
85  // HACK: workaround for missing VariationalRefinement::create()
86  FileStorage fs(rhs[2].toString(), FileStorage::READ +
87  (loadFromString ? FileStorage::MEMORY : 0));
88  if (!fs.isOpened())
89  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
90  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
91  if (fn.empty())
92  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
93  obj->read(fn);
94  //*/
95  }
96  else if (method == "empty") {
97  nargchk(nrhs==2 && nlhs<=1);
98  plhs[0] = MxArray(obj->empty());
99  }
100  else if (method == "getDefaultName") {
101  nargchk(nrhs==2 && nlhs<=1);
102  plhs[0] = MxArray(obj->getDefaultName());
103  }
104  else if (method == "calc") {
105  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
106  Mat flow;
107  for (int i=4; i<nrhs; i+=2) {
108  string key(rhs[i].toString());
109  if (key == "InitialFlow")
110  flow = rhs[i+1].toMat(CV_32F);
111  else
112  mexErrMsgIdAndTxt("mexopencv:error",
113  "Unrecognized option %s", key.c_str());
114  }
115  Mat I0(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
116  I1(rhs[3].toMat(rhs[3].isFloat() ? CV_32F : CV_8U));
117  //HACK: function expects Mat to be allocated
118  if (flow.empty()) flow.create(I0.size(), CV_32FC2);
119  obj->calc(I0, I1, flow);
120  plhs[0] = MxArray(flow);
121  }
122  else if (method == "calcUV") {
123  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=2);
124  Mat flow_u, flow_v;
125  for (int i=4; i<nrhs; i+=2) {
126  string key(rhs[i].toString());
127  if (key == "InitialFlowU")
128  flow_u = rhs[i+1].toMat(CV_32F);
129  else if (key == "InitialFlowV")
130  flow_v = rhs[i+1].toMat(CV_32F);
131  else
132  mexErrMsgIdAndTxt("mexopencv:error",
133  "Unrecognized option %s", key.c_str());
134  }
135  Mat I0(rhs[2].toMat(rhs[2].isFloat() ? CV_32F : CV_8U)),
136  I1(rhs[3].toMat(rhs[3].isFloat() ? CV_32F : CV_8U));
137  //HACK: function expects Mat to be allocated
138  if (flow_u.empty()) flow_u.create(I0.size(), CV_32FC1);
139  if (flow_v.empty()) flow_v.create(I0.size(), CV_32FC1);
140  obj->calcUV(I0, I1, flow_u, flow_v);
141  plhs[0] = MxArray(flow_u);
142  if (nlhs > 1)
143  plhs[1] = MxArray(flow_v);
144  }
145  else if (method == "collectGarbage") {
146  nargchk(nrhs==2 && nlhs==0);
147  obj->collectGarbage();
148  }
149  else if (method == "get") {
150  nargchk(nrhs==3 && nlhs<=1);
151  string prop(rhs[2].toString());
152  if (prop == "FixedPointIterations")
153  plhs[0] = MxArray(obj->getFixedPointIterations());
154  else if (prop == "SorIterations")
155  plhs[0] = MxArray(obj->getSorIterations());
156  else if (prop == "Omega")
157  plhs[0] = MxArray(obj->getOmega());
158  else if (prop == "Alpha")
159  plhs[0] = MxArray(obj->getAlpha());
160  else if (prop == "Delta")
161  plhs[0] = MxArray(obj->getDelta());
162  else if (prop == "Gamma")
163  plhs[0] = MxArray(obj->getGamma());
164  else
165  mexErrMsgIdAndTxt("mexopencv:error",
166  "Unrecognized property %s", prop.c_str());
167  }
168  else if (method == "set") {
169  nargchk(nrhs==4 && nlhs==0);
170  string prop(rhs[2].toString());
171  if (prop == "FixedPointIterations")
172  obj->setFixedPointIterations(rhs[3].toInt());
173  else if (prop == "SorIterations")
174  obj->setSorIterations(rhs[3].toInt());
175  else if (prop == "Omega")
176  obj->setOmega(rhs[3].toFloat());
177  else if (prop == "Alpha")
178  obj->setAlpha(rhs[3].toFloat());
179  else if (prop == "Delta")
180  obj->setDelta(rhs[3].toFloat());
181  else if (prop == "Gamma")
182  obj->setGamma(rhs[3].toFloat());
183  else
184  mexErrMsgIdAndTxt("mexopencv:error",
185  "Unrecognized property %s", prop.c_str());
186  }
187  else
188  mexErrMsgIdAndTxt("mexopencv:error",
189  "Unrecognized operation %s", method.c_str());
190 }
virtual int getFixedPointIterations() const=0
virtual void setDelta(float val)=0
virtual void setAlpha(float val)=0
T empty(T... args)
map< int, Ptr< VariationalRefinement > > obj_
Object container.
virtual void collectGarbage()=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
#define CV_32FC2
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual void setOmega(float 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...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
virtual float getAlpha() const=0
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
virtual float getGamma() const=0
FileNode getFirstTopLevelNode() const
virtual void setFixedPointIterations(int val)=0
virtual void calcUV(InputArray I0, InputArray I1, InputOutputArray flow_u, InputOutputArray flow_v)=0
virtual void setGamma(float val)=0
STL class.
bool empty() const
virtual int getSorIterations() const=0
Ptr< VariationalRefinement > createVariationalFlowRefinement()
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
void create(int rows, int cols, int type)
virtual float getDelta() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow)=0
#define CV_32FC1
virtual float getOmega() const=0
virtual void setSorIterations(int val)=0
bool empty() const
cv::Mat toMat() const