mexopencv  3.4.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;
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);
43  plhs[0] = MxArray(last_id);
44  mexLock();
45  return;
46  }
47 
48  // Big operation switch
50  if (obj.empty())
51  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  mexUnlock();
56  }
57  else if (method == "clear") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj->clear();
60  }
61  else if (method == "load") {
62  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
63  string objname;
64  bool loadFromString = false;
65  for (int i=3; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key == "ObjName")
68  objname = rhs[i+1].toString();
69  else if (key == "FromString")
70  loadFromString = rhs[i+1].toBool();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
75  obj_[id] = (loadFromString ?
76  Algorithm::loadFromString<MotionSaliencyBinWangApr2014>(rhs[2].toString(), objname) :
77  Algorithm::load<MotionSaliencyBinWangApr2014>(rhs[2].toString(), objname));
78  }
79  else if (method == "save") {
80  nargchk(nrhs==3 && nlhs==0);
81  obj->save(rhs[2].toString());
82  }
83  else if (method == "empty") {
84  nargchk(nrhs==2 && nlhs<=1);
85  plhs[0] = MxArray(obj->empty());
86  }
87  else if (method == "getDefaultName") {
88  nargchk(nrhs==2 && nlhs<=1);
89  plhs[0] = MxArray(obj->getDefaultName());
90  }
91  else if (method == "computeSaliency") {
92  nargchk(nrhs==3 && nlhs<=1);
93  Mat image(rhs[2].toMat(CV_8U)),
94  saliencyMap;
95  bool b = obj->computeSaliency(image, saliencyMap);
96  if (!b)
97  mexErrMsgIdAndTxt("mexopencv:error", "computeSaliency failed");
98  plhs[0] = MxArray(saliencyMap);
99  }
100  else if (method == "setImagesize") {
101  nargchk(nrhs==4 && nlhs==0);
102  int W = rhs[2].toInt(),
103  H = rhs[3].toInt();
104  obj->setImagesize(W, H);
105  }
106  else if (method == "init") {
107  nargchk(nrhs==2 && nlhs==0);
108  bool b = obj->init();
109  if (!b)
110  mexErrMsgIdAndTxt("mexopencv:error", "init failed");
111  }
112  else if (method == "get") {
113  nargchk(nrhs==3 && nlhs<=1);
114  string prop(rhs[2].toString());
115  if (prop == "ImageWidth")
116  plhs[0] = MxArray(obj->getImageWidth());
117  else if (prop == "ImageHeight")
118  plhs[0] = MxArray(obj->getImageHeight());
119  else
120  mexErrMsgIdAndTxt("mexopencv:error",
121  "Unrecognized property %s", prop.c_str());
122  }
123  else if (method == "set") {
124  nargchk(nrhs==4 && nlhs==0);
125  string prop(rhs[2].toString());
126  if (prop == "ImageWidth")
127  obj->setImageWidth(rhs[3].toInt());
128  else if (prop == "ImageHeight")
129  obj->setImageHeight(rhs[3].toInt());
130  else
131  mexErrMsgIdAndTxt("mexopencv:error",
132  "Unrecognized property %s", prop.c_str());
133  }
134  else
135  mexErrMsgIdAndTxt("mexopencv:error",
136  "Unrecognized operation %s", method.c_str());
137 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
map< int, Ptr< MotionSaliencyBinWangApr2014 > > obj_
Object container.
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void clear()
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
bool computeSaliency(InputArray image, OutputArray saliencyMap)
virtual void save(const String &filename) const
virtual bool empty() const
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const