mexopencv  0.1
mex interface for opencv library
FastGlobalSmootherFilter_.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<FastGlobalSmootherFilter> > obj_;
20 
22 struct OptionsParser
23 {
24  double lambda;
25  double sigma_color;
26  double lambda_attenuation;
27  int num_iter;
28 
33  OptionsParser(vector<MxArray>::const_iterator first,
34  vector<MxArray>::const_iterator last)
35  : lambda(100.0),
36  sigma_color(5.0),
37  lambda_attenuation(0.25),
38  num_iter(3)
39  {
40  ptrdiff_t len = std::distance(first, last);
41  nargchk((len%2)==0);
42  for (; first != last; first += 2) {
43  string key(first->toString());
44  const MxArray& val = *(first + 1);
45  if (key == "Lambda")
46  lambda = val.toDouble();
47  else if (key == "SigmaColor")
48  sigma_color = val.toDouble();
49  else if (key == "LambdaAttenuation")
50  lambda_attenuation = val.toDouble();
51  else if (key == "NumIter")
52  num_iter = val.toInt();
53  else
54  mexErrMsgIdAndTxt("mexopencv:error",
55  "Unrecognized option %s", key.c_str());
56  }
57  }
58 };
59 }
60 
68 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
69 {
70  // Check the number of arguments
71  nargchk(nrhs>=2 && nlhs<=1);
72 
73  // Argument vector
74  vector<MxArray> rhs(prhs, prhs+nrhs);
75  int id = rhs[0].toInt();
76  string method(rhs[1].toString());
77 
78  // Constructor is called. Create a new object from argument
79  if (method == "new") {
80  nargchk(nrhs>=3 && nlhs<=1);
81  Mat guide(rhs[2].toMat(CV_8U));
82  OptionsParser opts(rhs.begin() + 3, rhs.end());
83  obj_[++last_id] = createFastGlobalSmootherFilter(guide,
84  opts.lambda, opts.sigma_color,
85  opts.lambda_attenuation, opts.num_iter);
86  plhs[0] = MxArray(last_id);
87  return;
88  }
89  // static method call
90  else if (method == "fastGlobalSmootherFilter") {
91  nargchk(nrhs>=4 && nlhs<=1);
92  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
93  (rhs[2].isInt16() ? CV_16S : CV_32F))),
94  guide(rhs[3].toMat(CV_8U)),
95  dst;
96  OptionsParser opts(rhs.begin() + 4, rhs.end());
97  fastGlobalSmootherFilter(guide, src, dst,
98  opts.lambda, opts.sigma_color,
99  opts.lambda_attenuation, opts.num_iter);
100  plhs[0] = MxArray(dst);
101  return;
102  }
103 
104  // Big operation switch
105  Ptr<FastGlobalSmootherFilter> obj = obj_[id];
106  if (method == "delete") {
107  nargchk(nrhs==2 && nlhs==0);
108  obj_.erase(id);
109  }
110  else if (method == "clear") {
111  nargchk(nrhs==2 && nlhs==0);
112  obj->clear();
113  }
114  else if (method == "load") {
115  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
116  string objname;
117  bool loadFromString = false;
118  for (int i=3; i<nrhs; i+=2) {
119  string key(rhs[i].toString());
120  if (key == "ObjName")
121  objname = rhs[i+1].toString();
122  else if (key == "FromString")
123  loadFromString = rhs[i+1].toBool();
124  else
125  mexErrMsgIdAndTxt("mexopencv:error",
126  "Unrecognized option %s", key.c_str());
127  }
128  /*
129  obj_[id] = (loadFromString ?
130  Algorithm::loadFromString<FastGlobalSmootherFilter>(rhs[2].toString(), objname) :
131  Algorithm::load<FastGlobalSmootherFilter>(rhs[2].toString(), objname));
132  */
134  // HACK: workaround for missing FastGlobalSmootherFilter::create()
135  FileStorage fs(rhs[2].toString(), FileStorage::READ +
136  (loadFromString ? FileStorage::MEMORY : 0));
137  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
138  if (obj.empty())
139  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
140  //*/
141  }
142  else if (method == "save") {
143  nargchk(nrhs==3 && nlhs==0);
144  obj->save(rhs[2].toString());
145  }
146  else if (method == "empty") {
147  nargchk(nrhs==2 && nlhs<=1);
148  plhs[0] = MxArray(obj->empty());
149  }
150  else if (method == "getDefaultName") {
151  nargchk(nrhs==2 && nlhs<=1);
152  plhs[0] = MxArray(obj->getDefaultName());
153  }
154  else if (method == "filter") {
155  nargchk(nrhs==3 && nlhs<=1);
156  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
157  (rhs[2].isInt16() ? CV_16S : CV_32F))),
158  dst;
159  obj->filter(src, dst);
160  plhs[0] = MxArray(dst);
161  }
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized operation %s", method.c_str());
165 }
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
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.