mexopencv  3.4.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;
20 
23  ("NC", cv::ximgproc::DTF_NC)
24  ("IC", cv::ximgproc::DTF_IC)
25  ("RF", cv::ximgproc::DTF_RF);
26 
29 {
30  double sigmaSpatial;
31  double sigmaColor;
32  int mode;
33  int numIters;
34 
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  mexLock();
93  return;
94  }
95  // static method call
96  else if (method == "dtFilter") {
97  nargchk(nrhs>=4 && nlhs<=1);
98  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
99  guide(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F)),
100  dst;
101  OptionsParser opts(rhs.begin() + 4, rhs.end());
102  dtFilter(guide, src, dst,
103  opts.sigmaSpatial, opts.sigmaColor, opts.mode, opts.numIters);
104  plhs[0] = MxArray(dst);
105  return;
106  }
107 
108  // Big operation switch
109  Ptr<DTFilter> obj = obj_[id];
110  if (obj.empty())
111  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
112  if (method == "delete") {
113  nargchk(nrhs==2 && nlhs==0);
114  obj_.erase(id);
115  mexUnlock();
116  }
117  else if (method == "clear") {
118  nargchk(nrhs==2 && nlhs==0);
119  obj->clear();
120  }
121  else if (method == "load") {
122  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
123  string objname;
124  bool loadFromString = false;
125  for (int i=3; i<nrhs; i+=2) {
126  string key(rhs[i].toString());
127  if (key == "ObjName")
128  objname = rhs[i+1].toString();
129  else if (key == "FromString")
130  loadFromString = rhs[i+1].toBool();
131  else
132  mexErrMsgIdAndTxt("mexopencv:error",
133  "Unrecognized option %s", key.c_str());
134  }
135  /*
136  obj_[id] = (loadFromString ?
137  Algorithm::loadFromString<DTFilter>(rhs[2].toString(), objname) :
138  Algorithm::load<DTFilter>(rhs[2].toString(), objname));
139  */
141  // HACK: workaround for missing DTFilter::create()
142  FileStorage fs(rhs[2].toString(), FileStorage::READ +
143  (loadFromString ? FileStorage::MEMORY : 0));
144  if (!fs.isOpened())
145  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
146  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
147  if (fn.empty())
148  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
149  obj->read(fn);
150  //*/
151  }
152  else if (method == "save") {
153  nargchk(nrhs==3 && nlhs==0);
154  obj->save(rhs[2].toString());
155  }
156  else if (method == "empty") {
157  nargchk(nrhs==2 && nlhs<=1);
158  plhs[0] = MxArray(obj->empty());
159  }
160  else if (method == "getDefaultName") {
161  nargchk(nrhs==2 && nlhs<=1);
162  plhs[0] = MxArray(obj->getDefaultName());
163  }
164  else if (method == "filter") {
165  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
166  int dDepth = -1;
167  for (int i=3; i<nrhs; i+=2) {
168  string key(rhs[i].toString());
169  if (key == "DDepth")
170  dDepth = (rhs[i+1].isChar()) ?
171  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
172  else
173  mexErrMsgIdAndTxt("mexopencv:error",
174  "Unrecognized option %s", key.c_str());
175  }
176  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
177  dst;
178  obj->filter(src, dst, dDepth);
179  plhs[0] = MxArray(dst);
180  }
181  else
182  mexErrMsgIdAndTxt("mexopencv:error",
183  "Unrecognized operation %s", method.c_str());
184 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
int last_id
Last object id to allocate.
Definition: DTFilter_.cpp:17
T empty(T... args)
T distance(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: DTFilter_.cpp:74
#define CV_8U
Ptr< DTFilter > createDTFilter(InputArray guide, double sigmaSpatial, double sigmaColor, int mode=DTF_NC, int numIters=3)
STL namespace.
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void filter(InputArray src, OutputArray dst, int dDepth=-1)=0
map< int, Ptr< DTFilter > > obj_
Object container.
Definition: DTFilter_.cpp:19
virtual void clear()
const ConstMap< string, int > EdgeAwareFiltersListMap
Domain Transform filter modes.
Definition: DTFilter_.cpp:22
#define CV_32F
virtual void read(const FileNode &fn)
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
OptionsParser(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Parse input arguments.
Definition: DTFilter_.cpp:39
virtual void save(const String &filename) const
virtual bool empty() const
void dtFilter(InputArray guide, InputArray src, OutputArray dst, double sigmaSpatial, double sigmaColor, int mode=DTF_NC, int numIters=3)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
Option arguments parser used by create and filter methods.
Definition: DTFilter_.cpp:28
cv::Mat toMat() const