mexopencv  0.1
mex interface for opencv library
AdaptiveManifoldFilter_.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<AdaptiveManifoldFilter> > obj_;
20 
22 struct OptionsParser
23 {
24  double sigma_s;
25  double sigma_r;
26  bool adjust_outliers;
27 
32  OptionsParser(vector<MxArray>::const_iterator first,
33  vector<MxArray>::const_iterator last)
34  : sigma_s(16.0),
35  sigma_r(0.2),
36  adjust_outliers(false)
37  {
38  ptrdiff_t len = std::distance(first, last);
39  nargchk((len%2)==0);
40  for (; first != last; first += 2) {
41  string key(first->toString());
42  const MxArray& val = *(first + 1);
43  if (key == "SigmaS")
44  sigma_s = val.toDouble();
45  else if (key == "SigmaR")
46  sigma_r = val.toDouble();
47  else if (key == "AdjustOutliers")
48  adjust_outliers = val.toBool();
49  else
50  mexErrMsgIdAndTxt("mexopencv:error",
51  "Unrecognized option %s", key.c_str());
52  }
53  }
54 };
55 }
56 
64 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
65 {
66  // Check the number of arguments
67  nargchk(nrhs>=2 && nlhs<=1);
68 
69  // Argument vector
70  vector<MxArray> rhs(prhs, prhs+nrhs);
71  int id = rhs[0].toInt();
72  string method(rhs[1].toString());
73 
74  // Constructor is called. Create a new object from argument
75  if (method == "new") {
76  nargchk(nrhs>=2 && nlhs<=1);
77  OptionsParser opts(rhs.begin() + 2, rhs.end());
78  obj_[++last_id] = createAMFilter(
79  opts.sigma_s, opts.sigma_r, opts.adjust_outliers);
80  plhs[0] = MxArray(last_id);
81  return;
82  }
83  // static method call
84  else if (method == "amFilter") {
85  nargchk(nrhs>=4 && nlhs<=1);
86  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
87  joint(rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
88  (rhs[3].isUint16() ? CV_16U : CV_32F))),
89  dst;
90  OptionsParser opts(rhs.begin() + 4, rhs.end());
91  amFilter(joint, src, dst,
92  opts.sigma_s, opts.sigma_r, opts.adjust_outliers);
93  plhs[0] = MxArray(dst);
94  return;
95  }
96 
97  // Big operation switch
98  Ptr<AdaptiveManifoldFilter> obj = obj_[id];
99  if (method == "delete") {
100  nargchk(nrhs==2 && nlhs==0);
101  obj_.erase(id);
102  }
103  else if (method == "clear") {
104  nargchk(nrhs==2 && nlhs==0);
105  obj->clear();
106  }
107  else if (method == "load") {
108  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
109  string objname;
110  bool loadFromString = false;
111  for (int i=3; i<nrhs; i+=2) {
112  string key(rhs[i].toString());
113  if (key == "ObjName")
114  objname = rhs[i+1].toString();
115  else if (key == "FromString")
116  loadFromString = rhs[i+1].toBool();
117  else
118  mexErrMsgIdAndTxt("mexopencv:error",
119  "Unrecognized option %s", key.c_str());
120  }
121  /*
122  obj_[id] = (loadFromString ?
123  Algorithm::loadFromString<AdaptiveManifoldFilter>(rhs[2].toString(), objname) :
124  Algorithm::load<AdaptiveManifoldFilter>(rhs[2].toString(), objname));
125  */
127  // HACK: workaround for missing AdaptiveManifoldFilter::create()
128  FileStorage fs(rhs[2].toString(), FileStorage::READ +
129  (loadFromString ? FileStorage::MEMORY : 0));
130  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
131  if (obj.empty())
132  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
133  //*/
134  }
135  else if (method == "save") {
136  nargchk(nrhs==3 && nlhs==0);
137  obj->save(rhs[2].toString());
138  }
139  else if (method == "empty") {
140  nargchk(nrhs==2 && nlhs<=1);
141  plhs[0] = MxArray(obj->empty());
142  }
143  else if (method == "getDefaultName") {
144  nargchk(nrhs==2 && nlhs<=1);
145  plhs[0] = MxArray(obj->getDefaultName());
146  }
147  else if (method == "collectGarbage") {
148  nargchk(nrhs==2 && nlhs==0);
149  obj->collectGarbage();
150  }
151  else if (method == "filter") {
152  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
153  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
154  joint, dst;
155  if (nrhs == 4)
156  joint = rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
157  (rhs[3].isUint16() ? CV_16U : CV_32F));
158  obj->filter(src, dst, (nrhs==4 ? joint : noArray()));
159  plhs[0] = MxArray(dst);
160  }
161  else if (method == "get") {
162  nargchk(nrhs==3 && nlhs<=1);
163  string prop(rhs[2].toString());
164  if (prop == "SigmaS")
165  plhs[0] = MxArray(obj->getSigmaS());
166  else if (prop == "SigmaR")
167  plhs[0] = MxArray(obj->getSigmaR());
168  else if (prop == "TreeHeight")
169  plhs[0] = MxArray(obj->getTreeHeight());
170  else if (prop == "PCAIterations")
171  plhs[0] = MxArray(obj->getPCAIterations());
172  else if (prop == "AdjustOutliers")
173  plhs[0] = MxArray(obj->getAdjustOutliers());
174  else if (prop == "UseRNG")
175  plhs[0] = MxArray(obj->getUseRNG());
176  else
177  mexErrMsgIdAndTxt("mexopencv:error",
178  "Unrecognized property %s", prop.c_str());
179  }
180  else if (method == "set") {
181  nargchk(nrhs==4 && nlhs==0);
182  string prop(rhs[2].toString());
183  if (prop == "SigmaS")
184  obj->setSigmaS(rhs[3].toDouble());
185  else if (prop == "SigmaR")
186  obj->setSigmaR(rhs[3].toDouble());
187  else if (prop == "TreeHeight")
188  obj->setTreeHeight(rhs[3].toInt());
189  else if (prop == "PCAIterations")
190  obj->setPCAIterations(rhs[3].toInt());
191  else if (prop == "AdjustOutliers")
192  obj->setAdjustOutliers(rhs[3].toBool());
193  else if (prop == "UseRNG")
194  obj->setUseRNG(rhs[3].toBool());
195  else
196  mexErrMsgIdAndTxt("mexopencv:error",
197  "Unrecognized property %s", prop.c_str());
198  }
199  else
200  mexErrMsgIdAndTxt("mexopencv:error",
201  "Unrecognized operation %s", method.c_str());
202 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
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.