mexopencv  0.1
mex interface for opencv library
ObjectnessBING_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/saliency.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::saliency;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<ObjectnessBING> > obj_;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=2);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs==2 && nlhs<=1);
42  obj_[++last_id] = makePtr<ObjectnessBING>();
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<ObjectnessBING> obj = obj_[id];
49  if (method == "delete") {
50  nargchk(nrhs==2 && nlhs==0);
51  obj_.erase(id);
52  }
53  else if (method == "clear") {
54  nargchk(nrhs==2 && nlhs==0);
55  obj->clear();
56  }
57  else if (method == "load") {
58  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
59  string objname;
60  bool loadFromString = false;
61  for (int i=3; i<nrhs; i+=2) {
62  string key(rhs[i].toString());
63  if (key == "ObjName")
64  objname = rhs[i+1].toString();
65  else if (key == "FromString")
66  loadFromString = rhs[i+1].toBool();
67  else
68  mexErrMsgIdAndTxt("mexopencv:error",
69  "Unrecognized option %s", key.c_str());
70  }
71  /*
72  obj_[id] = (loadFromString ?
73  Algorithm::loadFromString<ObjectnessBING>(rhs[2].toString(), objname) :
74  Algorithm::load<ObjectnessBING>(rhs[2].toString(), objname));
75  */
77  // HACK: workaround for missing ObjectnessBING::create()
78  FileStorage fs(rhs[2].toString(), FileStorage::READ +
79  (loadFromString ? FileStorage::MEMORY : 0));
80  // HACK: cast as base class since ObjectnessBING overrides read method
81  (obj.dynamicCast<Saliency>())->read(
82  objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
83  if (obj.empty())
84  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
85  //*/
86  }
87  else if (method == "save") {
88  nargchk(nrhs==3 && nlhs==0);
89  obj->save(rhs[2].toString());
90  }
91  else if (method == "empty") {
92  nargchk(nrhs==2 && nlhs<=1);
93  plhs[0] = MxArray(obj->empty());
94  }
95  else if (method == "getDefaultName") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->getDefaultName());
98  }
99  else if (method == "getClassName") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getClassName());
102  }
103  else if (method == "computeSaliency") {
104  nargchk(nrhs==3 && nlhs<=1);
105  Mat image(rhs[2].toMat(CV_8U));
106  vector<Vec4i> objectnessBoundingBox;
107  bool b = obj->computeSaliency(image, objectnessBoundingBox);
108  if (!b)
109  mexErrMsgIdAndTxt("mexopencv:error", "computeSaliency failed");
110  plhs[0] = MxArray(objectnessBoundingBox);
111  }
112  else if (method == "getobjectnessValues") {
113  nargchk(nrhs==2 && nlhs<=1);
114  vector<float> objectnessValues = obj->getobjectnessValues();
115  plhs[0] = MxArray(objectnessValues);
116  }
117  else if (method == "setTrainingPath") {
118  nargchk(nrhs==3 && nlhs==0);
119  string trainingPath(rhs[2].toString());
120  obj->setTrainingPath(trainingPath);
121  }
122  else if (method == "setBBResDir") {
123  nargchk(nrhs==3 && nlhs==0);
124  string resultsDir(rhs[2].toString());
125  obj->setBBResDir(resultsDir);
126  }
127  else if (method == "get") {
128  nargchk(nrhs==3 && nlhs<=1);
129  string prop(rhs[2].toString());
130  if (prop == "Base")
131  plhs[0] = MxArray(obj->getBase());
132  else if (prop == "NSS")
133  plhs[0] = MxArray(obj->getNSS());
134  else if (prop == "W")
135  plhs[0] = MxArray(obj->getW());
136  else
137  mexErrMsgIdAndTxt("mexopencv:error",
138  "Unrecognized property %s", prop.c_str());
139  }
140  else if (method == "set") {
141  nargchk(nrhs==4 && nlhs==0);
142  string prop(rhs[2].toString());
143  if (prop == "Base")
144  obj->setBase(rhs[3].toDouble());
145  else if (prop == "NSS")
146  obj->setNSS(rhs[3].toInt());
147  else if (prop == "W")
148  obj->setW(rhs[3].toInt());
149  else
150  mexErrMsgIdAndTxt("mexopencv:error",
151  "Unrecognized property %s", prop.c_str());
152  }
153  else
154  mexErrMsgIdAndTxt("mexopencv:error",
155  "Unrecognized operation %s", method.c_str());
156 }
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.