mexopencv  3.4.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;
20 
23 {
24  int radius;
25  double eps;
26 
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  mexLock();
79  return;
80  }
81  // static method call
82  else if (method == "guidedFilter") {
83  nargchk(nrhs>=4 && nlhs<=1);
84  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
85  guide(rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
86  (rhs[3].isUint16() ? CV_16U : CV_32F))),
87  dst;
88  OptionsParser opts(rhs.begin() + 4, rhs.end());
89  guidedFilter(guide, src, dst, opts.radius, opts.eps);
90  plhs[0] = MxArray(dst);
91  return;
92  }
93 
94  // Big operation switch
95  Ptr<GuidedFilter> obj = obj_[id];
96  if (obj.empty())
97  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
98  if (method == "delete") {
99  nargchk(nrhs==2 && nlhs==0);
100  obj_.erase(id);
101  mexUnlock();
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<GuidedFilter>(rhs[2].toString(), objname) :
124  Algorithm::load<GuidedFilter>(rhs[2].toString(), objname));
125  */
127  // HACK: workaround for missing GuidedFilter::create()
128  FileStorage fs(rhs[2].toString(), FileStorage::READ +
129  (loadFromString ? FileStorage::MEMORY : 0));
130  if (!fs.isOpened())
131  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
132  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
133  if (fn.empty())
134  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
135  obj->read(fn);
136  //*/
137  }
138  else if (method == "save") {
139  nargchk(nrhs==3 && nlhs==0);
140  obj->save(rhs[2].toString());
141  }
142  else if (method == "empty") {
143  nargchk(nrhs==2 && nlhs<=1);
144  plhs[0] = MxArray(obj->empty());
145  }
146  else if (method == "getDefaultName") {
147  nargchk(nrhs==2 && nlhs<=1);
148  plhs[0] = MxArray(obj->getDefaultName());
149  }
150  else if (method == "filter") {
151  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
152  int dDepth = -1;
153  for (int i=3; i<nrhs; i+=2) {
154  string key(rhs[i].toString());
155  if (key == "DDepth")
156  dDepth = (rhs[i+1].isChar()) ?
157  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
158  else
159  mexErrMsgIdAndTxt("mexopencv:error",
160  "Unrecognized option %s", key.c_str());
161  }
162  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
163  dst;
164  obj->filter(src, dst, dDepth);
165  plhs[0] = MxArray(dst);
166  }
167  else
168  mexErrMsgIdAndTxt("mexopencv:error",
169  "Unrecognized operation %s", method.c_str());
170 }
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
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.
virtual void filter(InputArray src, OutputArray dst, int dDepth=-1)=0
#define CV_8U
int last_id
Last object id to allocate.
STL namespace.
T end(T... args)
virtual bool isOpened() const
void guidedFilter(InputArray guide, InputArray src, OutputArray dst, int radius, double eps, int dDepth=-1)
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
OptionsParser(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Parse input arguments.
Option arguments parser used by create and filter methods.
virtual void clear()
#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
#define CV_16U
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
Ptr< GuidedFilter > createGuidedFilter(InputArray guide, int radius, double eps)
static softfloat eps()
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const
map< int, Ptr< GuidedFilter > > obj_
Object container.