mexopencv  0.1
mex interface for opencv library
StaticSaliencySpectralResidual_.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<StaticSaliencySpectralResidual> > 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<StaticSaliencySpectralResidual>();
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<StaticSaliencySpectralResidual> 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<StaticSaliencySpectralResidual>(rhs[2].toString(), objname) :
74  Algorithm::load<StaticSaliencySpectralResidual>(rhs[2].toString(), objname));
75  */
77  // HACK: workaround for missing StaticSaliencySpectralResidual::create()
78  FileStorage fs(rhs[2].toString(), FileStorage::READ +
79  (loadFromString ? FileStorage::MEMORY : 0));
80  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
81  if (obj.empty())
82  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
83  //*/
84  }
85  else if (method == "save") {
86  nargchk(nrhs==3 && nlhs==0);
87  obj->save(rhs[2].toString());
88  }
89  else if (method == "empty") {
90  nargchk(nrhs==2 && nlhs<=1);
91  plhs[0] = MxArray(obj->empty());
92  }
93  else if (method == "getDefaultName") {
94  nargchk(nrhs==2 && nlhs<=1);
95  plhs[0] = MxArray(obj->getDefaultName());
96  }
97  else if (method == "getClassName") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->getClassName());
100  }
101  else if (method == "computeSaliency") {
102  nargchk(nrhs==3 && nlhs<=1);
103  Mat image(rhs[2].toMat()),
104  saliencyMap;
105  bool b = obj->computeSaliency(image, saliencyMap);
106  if (!b)
107  mexErrMsgIdAndTxt("mexopencv:error", "computeSaliency failed");
108  plhs[0] = MxArray(saliencyMap);
109  }
110  else if (method == "computeBinaryMap") {
111  nargchk(nrhs==3 && nlhs<=1);
112  Mat saliencyMap(rhs[2].toMat(CV_32F)),
113  binaryMap;
114  bool b = obj->computeBinaryMap(saliencyMap, binaryMap);
115  if (!b)
116  mexErrMsgIdAndTxt("mexopencv:error", "computeBinaryMap failed");
117  plhs[0] = MxArray(binaryMap);
118  }
119  else if (method == "get") {
120  nargchk(nrhs==3 && nlhs<=1);
121  string prop(rhs[2].toString());
122  if (prop == "ImageWidth")
123  plhs[0] = MxArray(obj->getImageWidth());
124  else if (prop == "ImageHeight")
125  plhs[0] = MxArray(obj->getImageHeight());
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized property %s", prop.c_str());
129  }
130  else if (method == "set") {
131  nargchk(nrhs==4 && nlhs==0);
132  string prop(rhs[2].toString());
133  if (prop == "ImageWidth")
134  obj->setImageWidth(rhs[3].toInt());
135  else if (prop == "ImageHeight")
136  obj->setImageHeight(rhs[3].toInt());
137  else
138  mexErrMsgIdAndTxt("mexopencv:error",
139  "Unrecognized property %s", prop.c_str());
140  }
141  else
142  mexErrMsgIdAndTxt("mexopencv:error",
143  "Unrecognized operation %s", method.c_str());
144 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.