mexopencv  0.1
mex interface for opencv library
MotionSaliencyBinWangApr2014_.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<MotionSaliencyBinWangApr2014> > 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<MotionSaliencyBinWangApr2014>();
43  plhs[0] = MxArray(last_id);
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<MotionSaliencyBinWangApr2014> 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<MotionSaliencyBinWangApr2014>(rhs[2].toString(), objname) :
74  Algorithm::load<MotionSaliencyBinWangApr2014>(rhs[2].toString(), objname));
75  */
77  // HACK: workaround for missing MotionSaliencyBinWangApr2014::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(CV_8U)),
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 == "setImagesize") {
111  nargchk(nrhs==4 && nlhs==0);
112  int W = rhs[2].toInt(),
113  H = rhs[3].toInt();
114  obj->setImagesize(W, H);
115  }
116  else if (method == "init") {
117  nargchk(nrhs==2 && nlhs==0);
118  bool b = obj->init();
119  if (!b)
120  mexErrMsgIdAndTxt("mexopencv:error", "init failed");
121  }
122  else if (method == "get") {
123  nargchk(nrhs==3 && nlhs<=1);
124  string prop(rhs[2].toString());
125  if (prop == "ImageWidth")
126  plhs[0] = MxArray(obj->getImageWidth());
127  else if (prop == "ImageHeight")
128  plhs[0] = MxArray(obj->getImageHeight());
129  else
130  mexErrMsgIdAndTxt("mexopencv:error",
131  "Unrecognized property %s", prop.c_str());
132  }
133  else if (method == "set") {
134  nargchk(nrhs==4 && nlhs==0);
135  string prop(rhs[2].toString());
136  if (prop == "ImageWidth")
137  obj->setImageWidth(rhs[3].toInt());
138  else if (prop == "ImageHeight")
139  obj->setImageHeight(rhs[3].toInt());
140  else
141  mexErrMsgIdAndTxt("mexopencv:error",
142  "Unrecognized property %s", prop.c_str());
143  }
144  else
145  mexErrMsgIdAndTxt("mexopencv:error",
146  "Unrecognized operation %s", method.c_str());
147 }
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.