mexopencv  0.1
mex interface for opencv library
GuidedFilter_.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<GuidedFilter> > obj_;
20 
22 struct OptionsParser
23 {
24  int radius;
25  double eps;
26 
31  OptionsParser(vector<MxArray>::const_iterator first,
32  vector<MxArray>::const_iterator last)
33  : radius(7),
34  eps(500.0)
35  {
36  ptrdiff_t len = std::distance(first, last);
37  nargchk((len%2)==0);
38  for (; first != last; first += 2) {
39  string key(first->toString());
40  const MxArray& val = *(first + 1);
41  if (key == "Radius")
42  radius = val.toInt();
43  else if (key == "EPS")
44  eps = val.toDouble();
45  else
46  mexErrMsgIdAndTxt("mexopencv:error",
47  "Unrecognized option %s", key.c_str());
48  }
49  }
50 };
51 }
52 
60 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
61 {
62  // Check the number of arguments
63  nargchk(nrhs>=2 && nlhs<=1);
64 
65  // Argument vector
66  vector<MxArray> rhs(prhs, prhs+nrhs);
67  int id = rhs[0].toInt();
68  string method(rhs[1].toString());
69 
70  // Constructor is called. Create a new object from argument
71  if (method == "new") {
72  nargchk(nrhs>=3 && nlhs<=1);
73  Mat guide(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
74  (rhs[2].isUint16() ? CV_16U : CV_32F)));
75  OptionsParser opts(rhs.begin() + 3, rhs.end());
76  obj_[++last_id] = createGuidedFilter(guide, opts.radius, opts.eps);
77  plhs[0] = MxArray(last_id);
78  return;
79  }
80  // static method call
81  else if (method == "guidedFilter") {
82  nargchk(nrhs>=4 && nlhs<=1);
83  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
84  guide(rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
85  (rhs[3].isUint16() ? CV_16U : CV_32F))),
86  dst;
87  OptionsParser opts(rhs.begin() + 4, rhs.end());
88  guidedFilter(guide, src, dst, opts.radius, opts.eps);
89  plhs[0] = MxArray(dst);
90  return;
91  }
92 
93  // Big operation switch
94  Ptr<GuidedFilter> obj = obj_[id];
95  if (method == "delete") {
96  nargchk(nrhs==2 && nlhs==0);
97  obj_.erase(id);
98  }
99  else if (method == "clear") {
100  nargchk(nrhs==2 && nlhs==0);
101  obj->clear();
102  }
103  else if (method == "load") {
104  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
105  string objname;
106  bool loadFromString = false;
107  for (int i=3; i<nrhs; i+=2) {
108  string key(rhs[i].toString());
109  if (key == "ObjName")
110  objname = rhs[i+1].toString();
111  else if (key == "FromString")
112  loadFromString = rhs[i+1].toBool();
113  else
114  mexErrMsgIdAndTxt("mexopencv:error",
115  "Unrecognized option %s", key.c_str());
116  }
117  /*
118  obj_[id] = (loadFromString ?
119  Algorithm::loadFromString<GuidedFilter>(rhs[2].toString(), objname) :
120  Algorithm::load<GuidedFilter>(rhs[2].toString(), objname));
121  */
123  // HACK: workaround for missing GuidedFilter::create()
124  FileStorage fs(rhs[2].toString(), FileStorage::READ +
125  (loadFromString ? FileStorage::MEMORY : 0));
126  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
127  if (obj.empty())
128  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
129  //*/
130  }
131  else if (method == "save") {
132  nargchk(nrhs==3 && nlhs==0);
133  obj->save(rhs[2].toString());
134  }
135  else if (method == "empty") {
136  nargchk(nrhs==2 && nlhs<=1);
137  plhs[0] = MxArray(obj->empty());
138  }
139  else if (method == "getDefaultName") {
140  nargchk(nrhs==2 && nlhs<=1);
141  plhs[0] = MxArray(obj->getDefaultName());
142  }
143  else if (method == "filter") {
144  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
145  int dDepth = -1;
146  for (int i=3; i<nrhs; i+=2) {
147  string key(rhs[i].toString());
148  if (key == "DDepth")
149  dDepth = (rhs[i+1].isChar()) ?
150  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
151  else
152  mexErrMsgIdAndTxt("mexopencv:error",
153  "Unrecognized option %s", key.c_str());
154  }
155  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
156  dst;
157  obj->filter(src, dst, dDepth);
158  plhs[0] = MxArray(dst);
159  }
160  else
161  mexErrMsgIdAndTxt("mexopencv:error",
162  "Unrecognized operation %s", method.c_str());
163 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
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.