mexopencv  3.4.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;
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
49  Ptr<ObjectnessBING> obj = obj_[id];
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  /*
76  obj_[id] = (loadFromString ?
77  Algorithm::loadFromString<ObjectnessBING>(rhs[2].toString(), objname) :
78  Algorithm::load<ObjectnessBING>(rhs[2].toString(), objname));
79  */
81  // HACK: ObjectnessBING read/write interface is non-conformant
82  FileStorage fs(rhs[2].toString(), FileStorage::READ +
83  (loadFromString ? FileStorage::MEMORY : 0));
84  if (!fs.isOpened())
85  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
86  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
87  if (fn.empty())
88  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
89  // HACK: cast as base class since ObjectnessBING overrides read method
90  (obj.staticCast<Saliency>())->read(fn);
91  //*/
92  }
93  else if (method == "save") {
94  nargchk(nrhs==3 && nlhs==0);
95  obj->save(rhs[2].toString());
96  }
97  else if (method == "empty") {
98  nargchk(nrhs==2 && nlhs<=1);
99  plhs[0] = MxArray(obj->empty());
100  }
101  else if (method == "getDefaultName") {
102  nargchk(nrhs==2 && nlhs<=1);
103  plhs[0] = MxArray(obj->getDefaultName());
104  }
105  else if (method == "computeSaliency") {
106  nargchk(nrhs==3 && nlhs<=1);
107  Mat image(rhs[2].toMat(CV_8U));
108  vector<Vec4i> objectnessBoundingBox;
109  bool b = obj->computeSaliency(image, objectnessBoundingBox);
110  if (!b)
111  mexErrMsgIdAndTxt("mexopencv:error", "computeSaliency failed");
112  plhs[0] = MxArray(objectnessBoundingBox);
113  }
114  else if (method == "getobjectnessValues") {
115  nargchk(nrhs==2 && nlhs<=1);
116  vector<float> objectnessValues(obj->getobjectnessValues());
117  plhs[0] = MxArray(objectnessValues);
118  }
119  else if (method == "setTrainingPath") {
120  nargchk(nrhs==3 && nlhs==0);
121  string trainingPath(rhs[2].toString());
122  obj->setTrainingPath(trainingPath);
123  }
124  else if (method == "setBBResDir") {
125  nargchk(nrhs==3 && nlhs==0);
126  string resultsDir(rhs[2].toString());
127  obj->setBBResDir(resultsDir);
128  }
129  else if (method == "get") {
130  nargchk(nrhs==3 && nlhs<=1);
131  string prop(rhs[2].toString());
132  if (prop == "Base")
133  plhs[0] = MxArray(obj->getBase());
134  else if (prop == "NSS")
135  plhs[0] = MxArray(obj->getNSS());
136  else if (prop == "W")
137  plhs[0] = MxArray(obj->getW());
138  else
139  mexErrMsgIdAndTxt("mexopencv:error",
140  "Unrecognized property %s", prop.c_str());
141  }
142  else if (method == "set") {
143  nargchk(nrhs==4 && nlhs==0);
144  string prop(rhs[2].toString());
145  if (prop == "Base")
146  obj->setBase(rhs[3].toDouble());
147  else if (prop == "NSS")
148  obj->setNSS(rhs[3].toInt());
149  else if (prop == "W")
150  obj->setW(rhs[3].toInt());
151  else
152  mexErrMsgIdAndTxt("mexopencv:error",
153  "Unrecognized property %s", prop.c_str());
154  }
155  else
156  mexErrMsgIdAndTxt("mexopencv:error",
157  "Unrecognized operation %s", method.c_str());
158 }
T empty(T... args)
bool computeSaliency(InputArray image, OutputArray saliencyMap)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
Ptr< Y > staticCast() const
map< int, Ptr< ObjectnessBING > > obj_
Object container.
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
void setTrainingPath(const String &trainingPath)
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...
void setBBResDir(const String &resultsDir)
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
std::vector< float > getobjectnessValues()
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
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void save(const String &filename) const
virtual bool empty() const
int last_id
Last object id to allocate.
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const