mexopencv  3.4.1
MEX interface for OpenCV library
EdgeBoxes_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/ximgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::ximgproc;
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<=1);
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 && (nrhs%2)==0 && nlhs<=1);
42  float alpha = 0.65f;
43  float beta = 0.75f;
44  float eta = 1;
45  float minScore = 0.01f;
46  int maxBoxes = 10000;
47  float edgeMinMag = 0.1f;
48  float edgeMergeThr = 0.5f;
49  float clusterMinMag = 0.5f;
50  float maxAspectRatio = 3;
51  float minBoxArea = 1000;
52  float gamma = 2;
53  float kappa = 1.5f;
54  for (int i=2; i<nrhs; i+=2) {
55  string key(rhs[i].toString());
56  if (key == "Alpha")
57  alpha = rhs[i+1].toFloat();
58  else if (key == "Beta")
59  beta = rhs[i+1].toFloat();
60  else if (key == "Eta")
61  eta = rhs[i+1].toFloat();
62  else if (key == "MinScore")
63  minScore = rhs[i+1].toFloat();
64  else if (key == "MaxBoxes")
65  maxBoxes = rhs[i+1].toInt();
66  else if (key == "EdgeMinMag")
67  edgeMinMag = rhs[i+1].toFloat();
68  else if (key == "EdgeMergeThr")
69  edgeMergeThr = rhs[i+1].toFloat();
70  else if (key == "ClusterMinMag")
71  clusterMinMag = rhs[i+1].toFloat();
72  else if (key == "MaxAspectRatio")
73  maxAspectRatio = rhs[i+1].toFloat();
74  else if (key == "MinBoxArea")
75  minBoxArea = rhs[i+1].toFloat();
76  else if (key == "Gamma")
77  gamma = rhs[i+1].toFloat();
78  else if (key == "Kappa")
79  kappa = rhs[i+1].toFloat();
80  else
81  mexErrMsgIdAndTxt("mexopencv:error",
82  "Unrecognized option %s", key.c_str());
83  }
84  obj_[++last_id] = createEdgeBoxes(alpha, beta, eta, minScore,
85  maxBoxes, edgeMinMag, edgeMergeThr, clusterMinMag, maxAspectRatio,
86  minBoxArea, gamma, kappa);
87  plhs[0] = MxArray(last_id);
88  mexLock();
89  return;
90  }
91 
92  // Big operation switch
93  Ptr<EdgeBoxes> obj = obj_[id];
94  if (obj.empty())
95  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
96  if (method == "delete") {
97  nargchk(nrhs==2 && nlhs==0);
98  obj_.erase(id);
99  mexUnlock();
100  }
101  else if (method == "clear") {
102  nargchk(nrhs==2 && nlhs==0);
103  obj->clear();
104  }
105  else if (method == "load") {
106  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
107  string objname;
108  bool loadFromString = false;
109  for (int i=3; i<nrhs; i+=2) {
110  string key(rhs[i].toString());
111  if (key == "ObjName")
112  objname = rhs[i+1].toString();
113  else if (key == "FromString")
114  loadFromString = rhs[i+1].toBool();
115  else
116  mexErrMsgIdAndTxt("mexopencv:error",
117  "Unrecognized option %s", key.c_str());
118  }
119  /*
120  obj_[id] = (loadFromString ?
121  Algorithm::loadFromString<EdgeBoxes>(rhs[2].toString(), objname) :
122  Algorithm::load<EdgeBoxes>(rhs[2].toString(), objname));
123  */
125  // HACK: workaround for missing EdgeBoxes::create()
126  FileStorage fs(rhs[2].toString(), FileStorage::READ +
127  (loadFromString ? FileStorage::MEMORY : 0));
128  if (!fs.isOpened())
129  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
130  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
131  if (fn.empty())
132  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
133  obj->read(fn);
134  //*/
135  }
136  else if (method == "save") {
137  nargchk(nrhs==3 && nlhs==0);
138  obj->save(rhs[2].toString());
139  }
140  else if (method == "empty") {
141  nargchk(nrhs==2 && nlhs<=1);
142  plhs[0] = MxArray(obj->empty());
143  }
144  else if (method == "getDefaultName") {
145  nargchk(nrhs==2 && nlhs<=1);
146  plhs[0] = MxArray(obj->getDefaultName());
147  }
148  else if (method == "getBoundingBoxes") {
149  nargchk(nrhs==4 && nlhs<=1);
150  Mat edge_map(rhs[2].toMat(CV_32F)),
151  orientation_map(rhs[3].toMat(CV_32F));
152  vector<Rect> boxes;
153  obj->getBoundingBoxes(edge_map, orientation_map, boxes);
154  plhs[0] = MxArray(boxes);
155  }
156  else if (method == "get") {
157  nargchk(nrhs==3 && nlhs<=1);
158  string prop(rhs[2].toString());
159  if (prop == "Alpha")
160  plhs[0] = MxArray(obj->getAlpha());
161  else if (prop == "Beta")
162  plhs[0] = MxArray(obj->getBeta());
163  else if (prop == "Eta")
164  plhs[0] = MxArray(obj->getEta());
165  else if (prop == "MinScore")
166  plhs[0] = MxArray(obj->getMinScore());
167  else if (prop == "MaxBoxes")
168  plhs[0] = MxArray(obj->getMaxBoxes());
169  else if (prop == "EdgeMinMag")
170  plhs[0] = MxArray(obj->getEdgeMinMag());
171  else if (prop == "EdgeMergeThr")
172  plhs[0] = MxArray(obj->getEdgeMergeThr());
173  else if (prop == "ClusterMinMag")
174  plhs[0] = MxArray(obj->getClusterMinMag());
175  else if (prop == "MaxAspectRatio")
176  plhs[0] = MxArray(obj->getMaxAspectRatio());
177  else if (prop == "MinBoxArea")
178  plhs[0] = MxArray(obj->getMinBoxArea());
179  else if (prop == "Gamma")
180  plhs[0] = MxArray(obj->getGamma());
181  else if (prop == "Kappa")
182  plhs[0] = MxArray(obj->getKappa());
183  else
184  mexErrMsgIdAndTxt("mexopencv:error",
185  "Unrecognized property %s", prop.c_str());
186  }
187  else if (method == "set") {
188  nargchk(nrhs==4 && nlhs==0);
189  string prop(rhs[2].toString());
190  if (prop == "Alpha")
191  obj->setAlpha(rhs[3].toFloat());
192  else if (prop == "Beta")
193  obj->setBeta(rhs[3].toFloat());
194  else if (prop == "Eta")
195  obj->setEta(rhs[3].toFloat());
196  else if (prop == "MinScore")
197  obj->setMinScore(rhs[3].toFloat());
198  else if (prop == "MaxBoxes")
199  obj->setMaxBoxes(rhs[3].toInt());
200  else if (prop == "EdgeMinMag")
201  obj->setEdgeMinMag(rhs[3].toFloat());
202  else if (prop == "EdgeMergeThr")
203  obj->setEdgeMergeThr(rhs[3].toFloat());
204  else if (prop == "ClusterMinMag")
205  obj->setClusterMinMag(rhs[3].toFloat());
206  else if (prop == "MaxAspectRatio")
207  obj->setMaxAspectRatio(rhs[3].toFloat());
208  else if (prop == "MinBoxArea")
209  obj->setMinBoxArea(rhs[3].toFloat());
210  else if (prop == "Gamma")
211  obj->setGamma(rhs[3].toFloat());
212  else if (prop == "Kappa")
213  obj->setKappa(rhs[3].toFloat());
214  else
215  mexErrMsgIdAndTxt("mexopencv:error",
216  "Unrecognized property %s", prop.c_str());
217  }
218  else
219  mexErrMsgIdAndTxt("mexopencv:error",
220  "Unrecognized operation %s", method.c_str());
221 }
virtual float getClusterMinMag() const=0
Ptr< EdgeBoxes > createEdgeBoxes(float alpha=0.65f, float beta=0.75f, float eta=1, float minScore=0.01f, int maxBoxes=10000, float edgeMinMag=0.1f, float edgeMergeThr=0.5f, float clusterMinMag=0.5f, float maxAspectRatio=3, float minBoxArea=1000, float gamma=2, float kappa=1.5f)
T empty(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setMaxAspectRatio(float value)=0
int last_id
Last object id to allocate.
Definition: EdgeBoxes_.cpp:17
virtual float getMaxAspectRatio() const=0
virtual int getMaxBoxes() const=0
STL namespace.
virtual void setMaxBoxes(int value)=0
virtual void getBoundingBoxes(InputArray edge_map, InputArray orientation_map, std::vector< Rect > &boxes)=0
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setClusterMinMag(float value)=0
virtual float getAlpha() const=0
virtual float getKappa() const=0
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual float getMinScore() const=0
virtual void setEdgeMinMag(float value)=0
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...
virtual float getMinBoxArea() const=0
virtual float getEdgeMinMag() const=0
virtual void setEdgeMergeThr(float value)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual float getEta() const=0
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
virtual void setKappa(float value)=0
virtual float getEdgeMergeThr() const=0
FileNode getFirstTopLevelNode() const
virtual void setMinBoxArea(float value)=0
STL class.
bool empty() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: EdgeBoxes_.cpp:29
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void setAlpha(float value)=0
map< int, Ptr< EdgeBoxes > > obj_
Object container.
Definition: EdgeBoxes_.cpp:19
virtual float getGamma() const=0
virtual void setEta(float value)=0
virtual void setBeta(float value)=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual float getBeta() const=0
virtual void setGamma(float value)=0
cv::Mat toMat() const
virtual void setMinScore(float value)=0