mexopencv  0.1
mex interface for opencv library
SuperpixelSLIC_.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<SuperpixelSLIC> > obj_;
20 
22 const ConstMap<string, int> SLICAlgorithmMap = ConstMap<string, int>
23  ("SLIC", cv::ximgproc::SLIC)
24  ("SLICO", cv::ximgproc::SLICO);
25 }
26 
34 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
35 {
36  // Check the number of arguments
37  nargchk(nrhs>=2 && nlhs<=1);
38 
39  // Argument vector
40  vector<MxArray> rhs(prhs, prhs+nrhs);
41  int id = rhs[0].toInt();
42  string method(rhs[1].toString());
43 
44  // Constructor is called. Create a new object from argument
45  if (method == "new") {
46  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
47  int algorithm = cv::ximgproc::SLICO;
48  int region_size = 10;
49  float ruler = 10.0f;
50  for (int i=3; i<nrhs; i+=2) {
51  string key(rhs[i].toString());
52  if (key == "Algorithm")
53  algorithm = SLICAlgorithmMap[rhs[i+1].toString()];
54  else if (key == "RegionSize")
55  region_size = rhs[i+1].toInt();
56  else if (key == "Ruler")
57  ruler = rhs[i+1].toFloat();
58  else
59  mexErrMsgIdAndTxt("mexopencv:error",
60  "Unrecognized option %s", key.c_str());
61  }
62  Mat image(rhs[2].toMat());
63  obj_[++last_id] = createSuperpixelSLIC(
64  image, algorithm, region_size, ruler);
65  plhs[0] = MxArray(last_id);
66  return;
67  }
68 
69  // Big operation switch
70  Ptr<SuperpixelSLIC> obj = obj_[id];
71  if (method == "delete") {
72  nargchk(nrhs==2 && nlhs==0);
73  obj_.erase(id);
74  }
75  else if (method == "clear") {
76  nargchk(nrhs==2 && nlhs==0);
77  obj->clear();
78  }
79  else if (method == "load") {
80  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
81  string objname;
82  bool loadFromString = false;
83  for (int i=3; i<nrhs; i+=2) {
84  string key(rhs[i].toString());
85  if (key == "ObjName")
86  objname = rhs[i+1].toString();
87  else if (key == "FromString")
88  loadFromString = rhs[i+1].toBool();
89  else
90  mexErrMsgIdAndTxt("mexopencv:error",
91  "Unrecognized option %s", key.c_str());
92  }
93  /*
94  obj_[id] = (loadFromString ?
95  Algorithm::loadFromString<SuperpixelSLIC>(rhs[2].toString(), objname) :
96  Algorithm::load<SuperpixelSLIC>(rhs[2].toString(), objname));
97  */
99  // HACK: workaround for missing SuperpixelSLIC::create()
100  FileStorage fs(rhs[2].toString(), FileStorage::READ +
101  (loadFromString ? FileStorage::MEMORY : 0));
102  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
103  if (obj.empty())
104  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
105  //*/
106  }
107  else if (method == "save") {
108  nargchk(nrhs==3 && nlhs==0);
109  obj->save(rhs[2].toString());
110  }
111  else if (method == "empty") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(obj->empty());
114  }
115  else if (method == "getDefaultName") {
116  nargchk(nrhs==2 && nlhs<=1);
117  plhs[0] = MxArray(obj->getDefaultName());
118  }
119  else if (method == "iterate") {
120  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
121  int num_iterations = 10;
122  for (int i=2; i<nrhs; i+=2) {
123  string key(rhs[i].toString());
124  if (key == "NumIterations")
125  num_iterations = rhs[i+1].toInt();
126  else
127  mexErrMsgIdAndTxt("mexopencv:error",
128  "Unrecognized option %s", key.c_str());
129  }
130  obj->iterate(num_iterations);
131  }
132  else if (method == "getNumberOfSuperpixels") {
133  nargchk(nrhs==2 && nlhs<=1);
134  plhs[0] = MxArray(obj->getNumberOfSuperpixels());
135  }
136  else if (method == "getLabels") {
137  nargchk(nrhs==2 && nlhs<=1);
138  Mat labels_out;
139  obj->getLabels(labels_out);
140  plhs[0] = MxArray(labels_out);
141  }
142  else if (method == "getLabelContourMask") {
143  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
144  bool thick_line = true;
145  for (int i=2; i<nrhs; i+=2) {
146  string key(rhs[i].toString());
147  if (key == "ThickLine")
148  thick_line = rhs[i+1].toBool();
149  else
150  mexErrMsgIdAndTxt("mexopencv:error",
151  "Unrecognized option %s", key.c_str());
152  }
153  Mat image;
154  obj->getLabelContourMask(image, thick_line);
155  plhs[0] = MxArray(image);
156  }
157  else if (method == "enforceLabelConnectivity") {
158  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
159  int min_element_size = 25;
160  for (int i=2; i<nrhs; i+=2) {
161  string key(rhs[i].toString());
162  if (key == "MinElementSize")
163  min_element_size = rhs[i+1].toInt();
164  else
165  mexErrMsgIdAndTxt("mexopencv:error",
166  "Unrecognized option %s", key.c_str());
167  }
168  obj->enforceLabelConnectivity(min_element_size);
169  }
170  else
171  mexErrMsgIdAndTxt("mexopencv:error",
172  "Unrecognized operation %s", method.c_str());
173 }
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.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927