9 #include "opencv2/ximgproc.hpp" 19 map<int,Ptr<StructuredEdgeDetection> > obj_;
22 class MatlabRFFeatureGetter :
public cv::ximgproc::RFFeatureGetter
28 explicit MatlabRFFeatureGetter(
const string &func)
43 virtual void getFeatures(
const Mat &src, Mat &features,
44 const int gnrmRad,
const int gsmthRad,
const int shrink,
45 const int outNum,
const int gradNum)
const 49 opts.set(
"gradientNormalizationRadius", gnrmRad);
50 opts.set(
"gradientSmoothingRadius", gsmthRad);
51 opts.set(
"shrinkNumber", shrink);
52 opts.set(
"numberOfOutputChannels", outNum);
53 opts.set(
"numberOfGradientOrientations", gradNum);
54 mxArray *lhs, *rhs[3];
62 if (mexCallMATLAB(1, &lhs, 3, rhs,
"feval") == 0) {
64 CV_Assert(res.isNumeric() && !res.isComplex() && res.ndims() <= 3);
65 features = res.toMat(CV_32F);
76 createRFFeatureGetter()->getFeatures(src, features,
77 gnrmRad, gsmthRad, shrink, outNum, gradNum);
82 mxDestroyArray(rhs[0]);
83 mxDestroyArray(rhs[1]);
84 mxDestroyArray(rhs[2]);
91 static Ptr<MatlabRFFeatureGetter> create(
const string &func)
93 return makePtr<MatlabRFFeatureGetter>(func);
108 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
114 vector<MxArray> rhs(prhs, prhs+nrhs);
115 int id = rhs[0].toInt();
116 string method(rhs[1].toString());
119 if (method ==
"new") {
120 nargchk((nrhs==3||nrhs==4) && nlhs<=1);
121 string model(rhs[2].toString());
122 Ptr<RFFeatureGetter> howToGetFeatures;
124 howToGetFeatures = MatlabRFFeatureGetter::create(
127 howToGetFeatures = createRFFeatureGetter();
128 obj_[++last_id] = createStructuredEdgeDetection(
129 model, howToGetFeatures);
135 Ptr<StructuredEdgeDetection> obj = obj_[id];
136 if (method ==
"delete") {
140 else if (method ==
"clear") {
144 else if (method ==
"load") {
145 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
147 bool loadFromString =
false;
148 for (
int i=3; i<nrhs; i+=2) {
149 string key(rhs[i].toString());
150 if (key ==
"ObjName")
151 objname = rhs[i+1].toString();
152 else if (key ==
"FromString")
153 loadFromString = rhs[i+1].toBool();
155 mexErrMsgIdAndTxt(
"mexopencv:error",
156 "Unrecognized option %s", key.c_str());
165 FileStorage fs(rhs[2].toString(), FileStorage::READ +
166 (loadFromString ? FileStorage::MEMORY : 0));
167 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
169 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
172 else if (method ==
"save") {
174 obj->save(rhs[2].toString());
176 else if (method ==
"empty") {
178 plhs[0] =
MxArray(obj->empty());
180 else if (method ==
"getDefaultName") {
182 plhs[0] =
MxArray(obj->getDefaultName());
184 else if (method ==
"detectEdges") {
186 Mat src(rhs[2].toMat(CV_32F)), dst;
187 obj->detectEdges(src, dst);
191 mexErrMsgIdAndTxt(
"mexopencv:error",
192 "Unrecognized operation %s", method.c_str());
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.