9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<AdaptiveManifoldFilter> > obj_;
32 OptionsParser(vector<MxArray>::const_iterator first,
33 vector<MxArray>::const_iterator last)
36 adjust_outliers(false)
38 ptrdiff_t len = std::distance(first, last);
40 for (; first != last; first += 2) {
41 string key(first->toString());
42 const MxArray& val = *(first + 1);
45 else if (key ==
"SigmaR")
46 sigma_r = val.toDouble();
47 else if (key ==
"AdjustOutliers")
48 adjust_outliers = val.toBool();
50 mexErrMsgIdAndTxt(
"mexopencv:error",
51 "Unrecognized option %s", key.c_str());
64 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
70 vector<MxArray> rhs(prhs, prhs+nrhs);
71 int id = rhs[0].toInt();
72 string method(rhs[1].toString());
75 if (method ==
"new") {
77 OptionsParser opts(rhs.begin() + 2, rhs.end());
78 obj_[++last_id] = createAMFilter(
79 opts.sigma_s, opts.sigma_r, opts.adjust_outliers);
84 else if (method ==
"amFilter") {
86 Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
87 joint(rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
88 (rhs[3].isUint16() ? CV_16U : CV_32F))),
90 OptionsParser opts(rhs.begin() + 4, rhs.end());
91 amFilter(joint, src, dst,
92 opts.sigma_s, opts.sigma_r, opts.adjust_outliers);
98 Ptr<AdaptiveManifoldFilter> obj = obj_[id];
99 if (method ==
"delete") {
103 else if (method ==
"clear") {
107 else if (method ==
"load") {
108 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
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();
118 mexErrMsgIdAndTxt(
"mexopencv:error",
119 "Unrecognized option %s", key.c_str());
128 FileStorage fs(rhs[2].toString(), FileStorage::READ +
129 (loadFromString ? FileStorage::MEMORY : 0));
130 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
132 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
135 else if (method ==
"save") {
137 obj->save(rhs[2].toString());
139 else if (method ==
"empty") {
141 plhs[0] =
MxArray(obj->empty());
143 else if (method ==
"getDefaultName") {
145 plhs[0] =
MxArray(obj->getDefaultName());
147 else if (method ==
"collectGarbage") {
149 obj->collectGarbage();
151 else if (method ==
"filter") {
152 nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
153 Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
156 joint = rhs[3].toMat(rhs[3].isUint8() ? CV_8U :
157 (rhs[3].isUint16() ? CV_16U : CV_32F));
158 obj->filter(src, dst, (nrhs==4 ? joint : noArray()));
161 else if (method ==
"get") {
163 string prop(rhs[2].toString());
164 if (prop ==
"SigmaS")
165 plhs[0] =
MxArray(obj->getSigmaS());
166 else if (prop ==
"SigmaR")
167 plhs[0] =
MxArray(obj->getSigmaR());
168 else if (prop ==
"TreeHeight")
169 plhs[0] =
MxArray(obj->getTreeHeight());
170 else if (prop ==
"PCAIterations")
171 plhs[0] =
MxArray(obj->getPCAIterations());
172 else if (prop ==
"AdjustOutliers")
173 plhs[0] =
MxArray(obj->getAdjustOutliers());
174 else if (prop ==
"UseRNG")
175 plhs[0] =
MxArray(obj->getUseRNG());
177 mexErrMsgIdAndTxt(
"mexopencv:error",
178 "Unrecognized property %s", prop.c_str());
180 else if (method ==
"set") {
182 string prop(rhs[2].toString());
183 if (prop ==
"SigmaS")
184 obj->setSigmaS(rhs[3].toDouble());
185 else if (prop ==
"SigmaR")
186 obj->setSigmaR(rhs[3].toDouble());
187 else if (prop ==
"TreeHeight")
188 obj->setTreeHeight(rhs[3].toInt());
189 else if (prop ==
"PCAIterations")
190 obj->setPCAIterations(rhs[3].toInt());
191 else if (prop ==
"AdjustOutliers")
192 obj->setAdjustOutliers(rhs[3].toBool());
193 else if (prop ==
"UseRNG")
194 obj->setUseRNG(rhs[3].toBool());
196 mexErrMsgIdAndTxt(
"mexopencv:error",
197 "Unrecognized property %s", prop.c_str());
200 mexErrMsgIdAndTxt(
"mexopencv:error",
201 "Unrecognized operation %s", method.c_str());
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
double toDouble() const
Convert MxArray to double.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.