mexopencv  0.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;
19 map<int,Ptr<SuperpixelLSC> > 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<=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  return;
58  }
59 
60  // Big operation switch
61  Ptr<SuperpixelLSC> obj = obj_[id];
62  if (method == "delete") {
63  nargchk(nrhs==2 && nlhs==0);
64  obj_.erase(id);
65  }
66  else if (method == "clear") {
67  nargchk(nrhs==2 && nlhs==0);
68  obj->clear();
69  }
70  else if (method == "load") {
71  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
72  string objname;
73  bool loadFromString = false;
74  for (int i=3; i<nrhs; i+=2) {
75  string key(rhs[i].toString());
76  if (key == "ObjName")
77  objname = rhs[i+1].toString();
78  else if (key == "FromString")
79  loadFromString = rhs[i+1].toBool();
80  else
81  mexErrMsgIdAndTxt("mexopencv:error",
82  "Unrecognized option %s", key.c_str());
83  }
84  /*
85  obj_[id] = (loadFromString ?
86  Algorithm::loadFromString<SuperpixelLSC>(rhs[2].toString(), objname) :
87  Algorithm::load<SuperpixelLSC>(rhs[2].toString(), objname));
88  */
90  // HACK: workaround for missing SuperpixelLSC::create()
91  FileStorage fs(rhs[2].toString(), FileStorage::READ +
92  (loadFromString ? FileStorage::MEMORY : 0));
93  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
94  if (obj.empty())
95  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
96  //*/
97  }
98  else if (method == "save") {
99  nargchk(nrhs==3 && nlhs==0);
100  obj->save(rhs[2].toString());
101  }
102  else if (method == "empty") {
103  nargchk(nrhs==2 && nlhs<=1);
104  plhs[0] = MxArray(obj->empty());
105  }
106  else if (method == "getDefaultName") {
107  nargchk(nrhs==2 && nlhs<=1);
108  plhs[0] = MxArray(obj->getDefaultName());
109  }
110  else if (method == "iterate") {
111  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
112  int num_iterations = 10;
113  for (int i=2; i<nrhs; i+=2) {
114  string key(rhs[i].toString());
115  if (key == "NumIterations")
116  num_iterations = rhs[i+1].toInt();
117  else
118  mexErrMsgIdAndTxt("mexopencv:error",
119  "Unrecognized option %s", key.c_str());
120  }
121  obj->iterate(num_iterations);
122  }
123  else if (method == "getNumberOfSuperpixels") {
124  nargchk(nrhs==2 && nlhs<=1);
125  plhs[0] = MxArray(obj->getNumberOfSuperpixels());
126  }
127  else if (method == "getLabels") {
128  nargchk(nrhs==2 && nlhs<=1);
129  Mat labels_out;
130  obj->getLabels(labels_out);
131  plhs[0] = MxArray(labels_out);
132  }
133  else if (method == "getLabelContourMask") {
134  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
135  bool thick_line = true;
136  for (int i=2; i<nrhs; i+=2) {
137  string key(rhs[i].toString());
138  if (key == "ThickLine")
139  thick_line = rhs[i+1].toBool();
140  else
141  mexErrMsgIdAndTxt("mexopencv:error",
142  "Unrecognized option %s", key.c_str());
143  }
144  Mat image;
145  obj->getLabelContourMask(image, thick_line);
146  plhs[0] = MxArray(image);
147  }
148  else if (method == "enforceLabelConnectivity") {
149  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
150  int min_element_size = 20;
151  for (int i=2; i<nrhs; i+=2) {
152  string key(rhs[i].toString());
153  if (key == "MinElementSize")
154  min_element_size = rhs[i+1].toInt();
155  else
156  mexErrMsgIdAndTxt("mexopencv:error",
157  "Unrecognized option %s", key.c_str());
158  }
159  obj->enforceLabelConnectivity(min_element_size);
160  }
161  else
162  mexErrMsgIdAndTxt("mexopencv:error",
163  "Unrecognized operation %s", method.c_str());
164 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.