9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<DTFilter> > obj_;
23 (
"NC", cv::ximgproc::DTF_NC)
24 (
"IC", cv::ximgproc::DTF_IC)
25 (
"RF", cv::ximgproc::DTF_RF);
39 OptionsParser(vector<MxArray>::const_iterator first,
40 vector<MxArray>::const_iterator last)
43 mode(
cv::ximgproc::DTF_NC),
46 ptrdiff_t len = std::distance(first, last);
48 for (; first != last; first += 2) {
49 string key(first->toString());
50 const MxArray& val = *(first + 1);
51 if (key ==
"SigmaSpatial")
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();
60 mexErrMsgIdAndTxt(
"mexopencv:error",
61 "Unrecognized option %s", key.c_str());
74 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
80 vector<MxArray> rhs(prhs, prhs+nrhs);
81 int id = rhs[0].toInt();
82 string method(rhs[1].toString());
85 if (method ==
"new") {
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);
95 else if (method ==
"dtFilter") {
97 Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
98 guide(rhs[3].toMat(rhs[3].isUint8() ? CV_8U : CV_32F)),
100 OptionsParser opts(rhs.begin() + 4, rhs.end());
101 dtFilter(guide, src, dst,
102 opts.sigmaSpatial, opts.sigmaColor, opts.mode, opts.numIters);
108 Ptr<DTFilter> obj = obj_[id];
109 if (method ==
"delete") {
113 else if (method ==
"clear") {
117 else if (method ==
"load") {
118 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
120 bool loadFromString =
false;
121 for (
int i=3; i<nrhs; i+=2) {
122 string key(rhs[i].toString());
123 if (key ==
"ObjName")
124 objname = rhs[i+1].toString();
125 else if (key ==
"FromString")
126 loadFromString = rhs[i+1].toBool();
128 mexErrMsgIdAndTxt(
"mexopencv:error",
129 "Unrecognized option %s", key.c_str());
138 FileStorage fs(rhs[2].toString(), FileStorage::READ +
139 (loadFromString ? FileStorage::MEMORY : 0));
140 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
142 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
145 else if (method ==
"save") {
147 obj->save(rhs[2].toString());
149 else if (method ==
"empty") {
151 plhs[0] =
MxArray(obj->empty());
153 else if (method ==
"getDefaultName") {
155 plhs[0] =
MxArray(obj->getDefaultName());
157 else if (method ==
"filter") {
158 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
160 for (
int i=3; i<nrhs; i+=2) {
161 string key(rhs[i].toString());
163 dDepth = (rhs[i+1].isChar()) ?
166 mexErrMsgIdAndTxt(
"mexopencv:error",
167 "Unrecognized option %s", key.c_str());
169 Mat src(rhs[2].toMat(rhs[2].isUint8() ? CV_8U : CV_32F)),
171 obj->filter(src, dst, dDepth);
175 mexErrMsgIdAndTxt(
"mexopencv:error",
176 "Unrecognized operation %s", method.c_str());
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
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.
std::map wrapper with one-line initialization and lookup method.