mexopencv  0.1
mex interface for opencv library
DTFilter_.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<DTFilter> > obj_;
20 
22 const ConstMap<string, int> EdgeAwareFiltersListMap = ConstMap<string, int>
23  ("NC", cv::ximgproc::DTF_NC)
24  ("IC", cv::ximgproc::DTF_IC)
25  ("RF", cv::ximgproc::DTF_RF);
26 
28 struct OptionsParser
29 {
30  double sigmaSpatial;
31  double sigmaColor;
32  int mode;
33  int numIters;
34 
39  OptionsParser(vector<MxArray>::const_iterator first,
40  vector<MxArray>::const_iterator last)
41  : sigmaSpatial(10.0),
42  sigmaColor(25.0),
43  mode(cv::ximgproc::DTF_NC),
44  numIters(3)
45  {
46  ptrdiff_t len = std::distance(first, last);
47  nargchk((len%2)==0);
48  for (; first != last; first += 2) {
49  string key(first->toString());
50  const MxArray& val = *(first + 1);
51  if (key == "SigmaSpatial")
52  sigmaSpatial = val.toDouble();
53  else if (key == "SigmaColor")
54  sigmaColor = val.toDouble();
55  else if (key == "Mode")
56  mode = EdgeAwareFiltersListMap[val.toString()];
57  else if (key == "NumIters")
58  numIters = val.toInt();
59  else
60  mexErrMsgIdAndTxt("mexopencv:error",
61  "Unrecognized option %s", key.c_str());
62  }
63  }
64 };
65 }
66 
74 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
75 {
76  // Check the number of arguments
77  nargchk(nrhs>=2 && nlhs<=1);
78 
79  // Argument vector
80  vector<MxArray> rhs(prhs, prhs+nrhs);
81  int id = rhs[0].toInt();
82  string method(rhs[1].toString());
83 
84  // Constructor is called. Create a new object from argument
85  if (method == "new") {
86  nargchk(nrhs>=3 && nlhs<=1);
87  Mat guide(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F));
88  OptionsParser opts(rhs.begin() + 3, rhs.end());
89  obj_[++last_id] = createDTFilter(guide,
90  opts.sigmaSpatial, opts.sigmaColor, opts.mode, opts.numIters);
91  plhs[0] = MxArray(last_id);
92  return;
93  }
94  // static method call
95  else if (method == "dtFilter") {
96  nargchk(nrhs>=4 && nlhs<=1);
97  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
98  guide(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F)),
99  dst;
100  OptionsParser opts(rhs.begin() + 4, rhs.end());
101  dtFilter(guide, src, dst,
102  opts.sigmaSpatial, opts.sigmaColor, opts.mode, opts.numIters);
103  plhs[0] = MxArray(dst);
104  return;
105  }
106 
107  // Big operation switch
108  Ptr<DTFilter> obj = obj_[id];
109  if (method == "delete") {
110  nargchk(nrhs==2 && nlhs==0);
111  obj_.erase(id);
112  }
113  else if (method == "clear") {
114  nargchk(nrhs==2 && nlhs==0);
115  obj->clear();
116  }
117  else if (method == "load") {
118  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
119  string objname;
120  bool loadFromString = false;
121  for (int i=3; i<nrhs; i+=2) {
122  string key(rhs[i].toString());
123  if (key == "ObjName")
124  objname = rhs[i+1].toString();
125  else if (key == "FromString")
126  loadFromString = rhs[i+1].toBool();
127  else
128  mexErrMsgIdAndTxt("mexopencv:error",
129  "Unrecognized option %s", key.c_str());
130  }
131  /*
132  obj_[id] = (loadFromString ?
133  Algorithm::loadFromString<DTFilter>(rhs[2].toString(), objname) :
134  Algorithm::load<DTFilter>(rhs[2].toString(), objname));
135  */
137  // HACK: workaround for missing DTFilter::create()
138  FileStorage fs(rhs[2].toString(), FileStorage::READ +
139  (loadFromString ? FileStorage::MEMORY : 0));
140  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
141  if (obj.empty())
142  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
143  //*/
144  }
145  else if (method == "save") {
146  nargchk(nrhs==3 && nlhs==0);
147  obj->save(rhs[2].toString());
148  }
149  else if (method == "empty") {
150  nargchk(nrhs==2 && nlhs<=1);
151  plhs[0] = MxArray(obj->empty());
152  }
153  else if (method == "getDefaultName") {
154  nargchk(nrhs==2 && nlhs<=1);
155  plhs[0] = MxArray(obj->getDefaultName());
156  }
157  else if (method == "filter") {
158  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
159  int dDepth = -1;
160  for (int i=3; i<nrhs; i+=2) {
161  string key(rhs[i].toString());
162  if (key == "DDepth")
163  dDepth = (rhs[i+1].isChar()) ?
164  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
165  else
166  mexErrMsgIdAndTxt("mexopencv:error",
167  "Unrecognized option %s", key.c_str());
168  }
169  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
170  dst;
171  obj->filter(src, dst, dDepth);
172  plhs[0] = MxArray(dst);
173  }
174  else
175  mexErrMsgIdAndTxt("mexopencv:error",
176  "Unrecognized operation %s", method.c_str());
177 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: DTFilter_.cpp:74
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.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927