mexopencv  3.4.1
MEX interface for OpenCV library
SuperpixelLSC_.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>=3 && (nrhs%2)==1 && nlhs<=1);
42  int region_size = 10;
43  float ratio = 0.075f;
44  for (int i=3; i<nrhs; i+=2) {
45  string key(rhs[i].toString());
46  if (key == "RegionSize")
47  region_size = rhs[i+1].toInt();
48  else if (key == "Ratio")
49  ratio = rhs[i+1].toFloat();
50  else
51  mexErrMsgIdAndTxt("mexopencv:error",
52  "Unrecognized option %s", key.c_str());
53  }
54  Mat image(rhs[2].toMat());
55  obj_[++last_id] = createSuperpixelLSC(image, region_size, ratio);
56  plhs[0] = MxArray(last_id);
57  mexLock();
58  return;
59  }
60 
61  // Big operation switch
62  Ptr<SuperpixelLSC> obj = obj_[id];
63  if (obj.empty())
64  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
65  if (method == "delete") {
66  nargchk(nrhs==2 && nlhs==0);
67  obj_.erase(id);
68  mexUnlock();
69  }
70  else if (method == "clear") {
71  nargchk(nrhs==2 && nlhs==0);
72  obj->clear();
73  }
74  else if (method == "load") {
75  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
76  string objname;
77  bool loadFromString = false;
78  for (int i=3; i<nrhs; i+=2) {
79  string key(rhs[i].toString());
80  if (key == "ObjName")
81  objname = rhs[i+1].toString();
82  else if (key == "FromString")
83  loadFromString = rhs[i+1].toBool();
84  else
85  mexErrMsgIdAndTxt("mexopencv:error",
86  "Unrecognized option %s", key.c_str());
87  }
88  /*
89  obj_[id] = (loadFromString ?
90  Algorithm::loadFromString<SuperpixelLSC>(rhs[2].toString(), objname) :
91  Algorithm::load<SuperpixelLSC>(rhs[2].toString(), objname));
92  */
94  // HACK: workaround for missing SuperpixelLSC::create()
95  FileStorage fs(rhs[2].toString(), FileStorage::READ +
96  (loadFromString ? FileStorage::MEMORY : 0));
97  if (!fs.isOpened())
98  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
99  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
100  if (fn.empty())
101  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
102  obj->read(fn);
103  //*/
104  }
105  else if (method == "save") {
106  nargchk(nrhs==3 && nlhs==0);
107  obj->save(rhs[2].toString());
108  }
109  else if (method == "empty") {
110  nargchk(nrhs==2 && nlhs<=1);
111  plhs[0] = MxArray(obj->empty());
112  }
113  else if (method == "getDefaultName") {
114  nargchk(nrhs==2 && nlhs<=1);
115  plhs[0] = MxArray(obj->getDefaultName());
116  }
117  else if (method == "iterate") {
118  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
119  int num_iterations = 10;
120  for (int i=2; i<nrhs; i+=2) {
121  string key(rhs[i].toString());
122  if (key == "NumIterations")
123  num_iterations = rhs[i+1].toInt();
124  else
125  mexErrMsgIdAndTxt("mexopencv:error",
126  "Unrecognized option %s", key.c_str());
127  }
128  obj->iterate(num_iterations);
129  }
130  else if (method == "getNumberOfSuperpixels") {
131  nargchk(nrhs==2 && nlhs<=1);
132  plhs[0] = MxArray(obj->getNumberOfSuperpixels());
133  }
134  else if (method == "getLabels") {
135  nargchk(nrhs==2 && nlhs<=1);
136  Mat labels_out;
137  obj->getLabels(labels_out);
138  plhs[0] = MxArray(labels_out);
139  }
140  else if (method == "getLabelContourMask") {
141  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
142  bool thick_line = true;
143  for (int i=2; i<nrhs; i+=2) {
144  string key(rhs[i].toString());
145  if (key == "ThickLine")
146  thick_line = rhs[i+1].toBool();
147  else
148  mexErrMsgIdAndTxt("mexopencv:error",
149  "Unrecognized option %s", key.c_str());
150  }
151  Mat image;
152  obj->getLabelContourMask(image, thick_line);
153  plhs[0] = MxArray(image);
154  }
155  else if (method == "enforceLabelConnectivity") {
156  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
157  int min_element_size = 20;
158  for (int i=2; i<nrhs; i+=2) {
159  string key(rhs[i].toString());
160  if (key == "MinElementSize")
161  min_element_size = rhs[i+1].toInt();
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized option %s", key.c_str());
165  }
166  obj->enforceLabelConnectivity(min_element_size);
167  }
168  else
169  mexErrMsgIdAndTxt("mexopencv:error",
170  "Unrecognized operation %s", method.c_str());
171 }
T empty(T... args)
Ptr< SuperpixelLSC > createSuperpixelLSC(InputArray image, int region_size=10, float ratio=0.075f)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
int last_id
Last object id to allocate.
STL namespace.
virtual int getNumberOfSuperpixels() const=0
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void enforceLabelConnectivity(int min_element_size=20)=0
virtual void clear()
virtual void read(const FileNode &fn)
virtual void getLabelContourMask(OutputArray image, bool thick_line=true) const=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...
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
map< int, Ptr< SuperpixelLSC > > obj_
Object container.
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
virtual void iterate(int num_iterations=10)=0
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
cv::Mat toMat() const
virtual void getLabels(OutputArray labels_out) const=0