mexopencv  0.1
mex interface for opencv library
EdgeAwareInterpolator_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/ximgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::ximgproc;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<EdgeAwareInterpolator> > obj_;
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 is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs==2 && nlhs<=1);
42  obj_[++last_id] = createEdgeAwareInterpolator();
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<EdgeAwareInterpolator> obj = obj_[id];
49  if (method == "delete") {
50  nargchk(nrhs==2 && nlhs==0);
51  obj_.erase(id);
52  }
53  else if (method == "clear") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj->clear();
56  }
57  else if (method == "load") {
58  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
59  string objname;
60  bool loadFromString = false;
61  for (int i=3; i<nrhs; i+=2) {
62  string key(rhs[i].toString());
63  if (key == "ObjName")
64  objname = rhs[i+1].toString();
65  else if (key == "FromString")
66  loadFromString = rhs[i+1].toBool();
67  else
68  mexErrMsgIdAndTxt("mexopencv:error",
69  "Unrecognized option %s", key.c_str());
70  }
71  /*
72  obj_[id] = (loadFromString ?
73  Algorithm::loadFromString<EdgeAwareInterpolator>(rhs[2].toString(), objname) :
74  Algorithm::load<EdgeAwareInterpolator>(rhs[2].toString(), objname));
75  */
77  // HACK: workaround for missing EdgeAwareInterpolator::create()
78  FileStorage fs(rhs[2].toString(), FileStorage::READ +
79  (loadFromString ? FileStorage::MEMORY : 0));
80  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
81  if (obj.empty())
82  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
83  //*/
84  }
85  else if (method == "save") {
86  nargchk(nrhs==3 && nlhs==0);
87  obj->save(rhs[2].toString());
88  }
89  else if (method == "empty") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->empty());
92  }
93  else if (method == "getDefaultName") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(obj->getDefaultName());
96  }
97  else if (method == "interpolate") {
98  nargchk(nrhs==6 && nlhs<=1);
99  Mat from_image(rhs[2].toMat(CV_8U)),
100  to_image(rhs[4].toMat(CV_8U)),
101  dense_flow;
102  vector<Point2f> from_points(rhs[3].toVector<Point2f>()),
103  to_points(rhs[5].toVector<Point2f>());
104  obj->interpolate(from_image, from_points,
105  to_image, to_points, dense_flow);
106  plhs[0] = MxArray(dense_flow);
107  }
108  else if (method == "get") {
109  nargchk(nrhs==3 && nlhs<=1);
110  string prop(rhs[2].toString());
111  if (prop == "K")
112  plhs[0] = MxArray(obj->getK());
113  else if (prop == "Sigma")
114  plhs[0] = MxArray(obj->getSigma());
115  else if (prop == "Lambda")
116  plhs[0] = MxArray(obj->getLambda());
117  else if (prop == "UsePostProcessing")
118  plhs[0] = MxArray(obj->getUsePostProcessing());
119  else if (prop == "FGSLambda")
120  plhs[0] = MxArray(obj->getFGSLambda());
121  else if (prop == "FGSSigma")
122  plhs[0] = MxArray(obj->getFGSSigma());
123  else
124  mexErrMsgIdAndTxt("mexopencv:error",
125  "Unrecognized property %s", prop.c_str());
126  }
127  else if (method == "set") {
128  nargchk(nrhs==4 && nlhs==0);
129  string prop(rhs[2].toString());
130  if (prop == "K")
131  obj->setK(rhs[3].toInt());
132  else if (prop == "Sigma")
133  obj->setSigma(rhs[3].toFloat());
134  else if (prop == "Lambda")
135  obj->setLambda(rhs[3].toFloat());
136  else if (prop == "UsePostProcessing")
137  obj->setUsePostProcessing(rhs[3].toBool());
138  else if (prop == "FGSLambda")
139  obj->setFGSLambda(rhs[3].toFloat());
140  else if (prop == "FGSSigma")
141  obj->setFGSSigma(rhs[3].toFloat());
142  else
143  mexErrMsgIdAndTxt("mexopencv:error",
144  "Unrecognized property %s", prop.c_str());
145  }
146  else
147  mexErrMsgIdAndTxt("mexopencv:error",
148  "Unrecognized operation %s", method.c_str());
149 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.