mexopencv  3.4.1
MEX interface for OpenCV library
AdaptiveManifoldFilter_.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  double sigma_s;
25  double sigma_r;
27 
34  : sigma_s(16.0),
35  sigma_r(0.2),
36  adjust_outliers(false)
37  {
38  ptrdiff_t len = std::distance(first, last);
39  nargchk((len%2)==0);
40  for (; first != last; first += 2) {
41  string key(first->toString());
42  const MxArray& val = *(first + 1);
43  if (key == "SigmaS")
44  sigma_s = val.toDouble();
45  else if (key == "SigmaR")
46  sigma_r = val.toDouble();
47  else if (key == "AdjustOutliers")
48  adjust_outliers = val.toBool();
49  else
50  mexErrMsgIdAndTxt("mexopencv:error",
51  "Unrecognized option %s", key.c_str());
52  }
53  }
54 };
55 }
56 
64 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
65 {
66  // Check the number of arguments
67  nargchk(nrhs>=2 && nlhs<=1);
68 
69  // Argument vector
70  vector<MxArray> rhs(prhs, prhs+nrhs);
71  int id = rhs[0].toInt();
72  string method(rhs[1].toString());
73 
74  // Constructor is called. Create a new object from argument
75  if (method == "new") {
76  nargchk(nrhs>=2 && nlhs<=1);
77  OptionsParser opts(rhs.begin() + 2, rhs.end());
79  opts.sigma_s, opts.sigma_r, opts.adjust_outliers);
80  plhs[0] = MxArray(last_id);
81  mexLock();
82  return;
83  }
84  // static method call
85  else if (method == "amFilter") {
86  nargchk(nrhs>=4 && nlhs<=1);
87  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
88  joint(rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
89  (rhs[3].isUint16() ? CV_16U : CV_32F))),
90  dst;
91  OptionsParser opts(rhs.begin() + 4, rhs.end());
92  amFilter(joint, src, dst,
93  opts.sigma_s, opts.sigma_r, opts.adjust_outliers);
94  plhs[0] = MxArray(dst);
95  return;
96  }
97 
98  // Big operation switch
100  if (obj.empty())
101  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
102  if (method == "delete") {
103  nargchk(nrhs==2 && nlhs==0);
104  obj_.erase(id);
105  mexUnlock();
106  }
107  else if (method == "clear") {
108  nargchk(nrhs==2 && nlhs==0);
109  obj->clear();
110  }
111  else if (method == "load") {
112  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
113  string objname;
114  bool loadFromString = false;
115  for (int i=3; i<nrhs; i+=2) {
116  string key(rhs[i].toString());
117  if (key == "ObjName")
118  objname = rhs[i+1].toString();
119  else if (key == "FromString")
120  loadFromString = rhs[i+1].toBool();
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized option %s", key.c_str());
124  }
125  /*
126  obj_[id] = (loadFromString ?
127  Algorithm::loadFromString<AdaptiveManifoldFilter>(rhs[2].toString(), objname) :
128  Algorithm::load<AdaptiveManifoldFilter>(rhs[2].toString(), objname));
129  */
131  // HACK: workaround for missing AdaptiveManifoldFilter::create()
132  FileStorage fs(rhs[2].toString(), FileStorage::READ +
133  (loadFromString ? FileStorage::MEMORY : 0));
134  if (!fs.isOpened())
135  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
136  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
137  if (fn.empty())
138  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
139  obj->read(fn);
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 == "collectGarbage") {
155  nargchk(nrhs==2 && nlhs==0);
156  obj->collectGarbage();
157  }
158  else if (method == "filter") {
159  nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
160  Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
161  joint, dst;
162  if (nrhs == 4)
163  joint = rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
164  (rhs[3].isUint16() ? CV_16U : CV_32F));
165  obj->filter(src, dst, (nrhs==4 ? joint : noArray()));
166  plhs[0] = MxArray(dst);
167  }
168  else if (method == "get") {
169  nargchk(nrhs==3 && nlhs<=1);
170  string prop(rhs[2].toString());
171  if (prop == "SigmaS")
172  plhs[0] = MxArray(obj->getSigmaS());
173  else if (prop == "SigmaR")
174  plhs[0] = MxArray(obj->getSigmaR());
175  else if (prop == "TreeHeight")
176  plhs[0] = MxArray(obj->getTreeHeight());
177  else if (prop == "PCAIterations")
178  plhs[0] = MxArray(obj->getPCAIterations());
179  else if (prop == "AdjustOutliers")
180  plhs[0] = MxArray(obj->getAdjustOutliers());
181  else if (prop == "UseRNG")
182  plhs[0] = MxArray(obj->getUseRNG());
183  else
184  mexErrMsgIdAndTxt("mexopencv:error",
185  "Unrecognized property %s", prop.c_str());
186  }
187  else if (method == "set") {
188  nargchk(nrhs==4 && nlhs==0);
189  string prop(rhs[2].toString());
190  if (prop == "SigmaS")
191  obj->setSigmaS(rhs[3].toDouble());
192  else if (prop == "SigmaR")
193  obj->setSigmaR(rhs[3].toDouble());
194  else if (prop == "TreeHeight")
195  obj->setTreeHeight(rhs[3].toInt());
196  else if (prop == "PCAIterations")
197  obj->setPCAIterations(rhs[3].toInt());
198  else if (prop == "AdjustOutliers")
199  obj->setAdjustOutliers(rhs[3].toBool());
200  else if (prop == "UseRNG")
201  obj->setUseRNG(rhs[3].toBool());
202  else
203  mexErrMsgIdAndTxt("mexopencv:error",
204  "Unrecognized property %s", prop.c_str());
205  }
206  else
207  mexErrMsgIdAndTxt("mexopencv:error",
208  "Unrecognized operation %s", method.c_str());
209 }
virtual void setAdjustOutliers(bool val)=0
T empty(T... args)
T distance(T... args)
virtual double getSigmaS() const=0
virtual void setTreeHeight(int val)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getPCAIterations() const=0
virtual bool getUseRNG() const=0
#define CV_8U
virtual double getSigmaR() const=0
STL namespace.
virtual void setPCAIterations(int val)=0
virtual void setSigmaR(double val)=0
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual int getTreeHeight() const=0
virtual void setSigmaS(double val)=0
virtual void clear()
#define CV_32F
OptionsParser(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Parse input arguments.
virtual void filter(InputArray src, OutputArray dst, InputArray joint=noArray())=0
virtual void read(const FileNode &fn)
virtual bool getAdjustOutliers() const=0
InputOutputArray noArray()
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.
Option arguments parser used by create and filter methods.
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
virtual void setUseRNG(bool val)=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
#define false
Definition: tmwtypes.h:814
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
virtual void save(const String &filename) const
virtual bool empty() const
void amFilter(InputArray joint, InputArray src, OutputArray dst, double sigma_s, double sigma_r, bool adjust_outliers=false)
cv::Mat toMat() const
Ptr< AdaptiveManifoldFilter > createAMFilter(double sigma_s, double sigma_r, bool adjust_outliers=false)
map< int, Ptr< AdaptiveManifoldFilter > > obj_
Object container.