9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<FastGlobalSmootherFilter> > obj_;
26 double lambda_attenuation;
33 OptionsParser(vector<MxArray>::const_iterator first,
34 vector<MxArray>::const_iterator last)
37 lambda_attenuation(0.25),
40 ptrdiff_t len = std::distance(first, last);
42 for (; first != last; first += 2) {
43 string key(first->toString());
44 const MxArray& val = *(first + 1);
47 else if (key ==
"SigmaColor")
48 sigma_color = val.toDouble();
49 else if (key ==
"LambdaAttenuation")
50 lambda_attenuation = val.toDouble();
51 else if (key ==
"NumIter")
52 num_iter = val.toInt();
54 mexErrMsgIdAndTxt(
"mexopencv:error",
55 "Unrecognized option %s", key.c_str());
68 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
74 vector<MxArray> rhs(prhs, prhs+nrhs);
75 int id = rhs[0].toInt();
76 string method(rhs[1].toString());
79 if (method ==
"new") {
81 Mat guide(rhs[2].toMat(CV_8U));
82 OptionsParser opts(rhs.begin() + 3, rhs.end());
83 obj_[++last_id] = createFastGlobalSmootherFilter(guide,
84 opts.lambda, opts.sigma_color,
85 opts.lambda_attenuation, opts.num_iter);
90 else if (method ==
"fastGlobalSmootherFilter") {
92 Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
93 (rhs[2].isInt16() ? CV_16S : CV_32F))),
94 guide(rhs[3].toMat(CV_8U)),
96 OptionsParser opts(rhs.begin() + 4, rhs.end());
97 fastGlobalSmootherFilter(guide, src, dst,
98 opts.lambda, opts.sigma_color,
99 opts.lambda_attenuation, opts.num_iter);
105 Ptr<FastGlobalSmootherFilter> obj = obj_[id];
106 if (method ==
"delete") {
110 else if (method ==
"clear") {
114 else if (method ==
"load") {
115 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
117 bool loadFromString =
false;
118 for (
int i=3; i<nrhs; i+=2) {
119 string key(rhs[i].toString());
120 if (key ==
"ObjName")
121 objname = rhs[i+1].toString();
122 else if (key ==
"FromString")
123 loadFromString = rhs[i+1].toBool();
125 mexErrMsgIdAndTxt(
"mexopencv:error",
126 "Unrecognized option %s", key.c_str());
135 FileStorage fs(rhs[2].toString(), FileStorage::READ +
136 (loadFromString ? FileStorage::MEMORY : 0));
137 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
139 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
142 else if (method ==
"save") {
144 obj->save(rhs[2].toString());
146 else if (method ==
"empty") {
148 plhs[0] =
MxArray(obj->empty());
150 else if (method ==
"getDefaultName") {
152 plhs[0] =
MxArray(obj->getDefaultName());
154 else if (method ==
"filter") {
156 Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
157 (rhs[2].isInt16() ? CV_16S : CV_32F))),
159 obj->filter(src, dst);
163 mexErrMsgIdAndTxt(
"mexopencv:error",
164 "Unrecognized operation %s", method.c_str());
double toDouble() const
Convert MxArray to double.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.