mexopencv  0.1
mex interface for opencv library
SuperpixelSEEDS_.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<SuperpixelSEEDS> > 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>=5 && (nrhs%2)==1 && nlhs<=1);
42  int prior = 2;
43  int histogram_bins = 5;
44  bool double_step = false;
45  for (int i=5; i<nrhs; i+=2) {
46  string key(rhs[i].toString());
47  if (key == "Prior")
48  prior = rhs[i+1].toInt();
49  else if (key == "HistogramBins")
50  histogram_bins = rhs[i+1].toInt();
51  else if (key == "DoubleStep")
52  double_step = rhs[i+1].toBool();
53  else
54  mexErrMsgIdAndTxt("mexopencv:error",
55  "Unrecognized option %s", key.c_str());
56  }
57  vector<int> sz(rhs[2].toVector<int>());
58  if (sz.size() != 2 && sz.size() != 3)
59  mexErrMsgIdAndTxt("mexopencv:error", "Incorrect size");
60  int image_width = sz[1],
61  image_height = sz[0],
62  image_channels = (sz.size() == 3 ? sz[2] : 1),
63  num_superpixels = rhs[3].toInt(),
64  num_levels = rhs[4].toInt();
65  obj_[++last_id] = createSuperpixelSEEDS(
66  image_width, image_height, image_channels,
67  num_superpixels, num_levels, prior, histogram_bins, double_step);
68  plhs[0] = MxArray(last_id);
69  return;
70  }
71 
72  // Big operation switch
73  Ptr<SuperpixelSEEDS> obj = obj_[id];
74  if (method == "delete") {
75  nargchk(nrhs==2 && nlhs==0);
76  obj_.erase(id);
77  }
78  else if (method == "clear") {
79  nargchk(nrhs==2 && nlhs==0);
80  obj->clear();
81  }
82  else if (method == "load") {
83  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
84  string objname;
85  bool loadFromString = false;
86  for (int i=3; i<nrhs; i+=2) {
87  string key(rhs[i].toString());
88  if (key == "ObjName")
89  objname = rhs[i+1].toString();
90  else if (key == "FromString")
91  loadFromString = rhs[i+1].toBool();
92  else
93  mexErrMsgIdAndTxt("mexopencv:error",
94  "Unrecognized option %s", key.c_str());
95  }
96  /*
97  obj_[id] = (loadFromString ?
98  Algorithm::loadFromString<SuperpixelSEEDS>(rhs[2].toString(), objname) :
99  Algorithm::load<SuperpixelSEEDS>(rhs[2].toString(), objname));
100  */
102  // HACK: workaround for missing SuperpixelSEEDS::create()
103  FileStorage fs(rhs[2].toString(), FileStorage::READ +
104  (loadFromString ? FileStorage::MEMORY : 0));
105  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
106  if (obj.empty())
107  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
108  //*/
109  }
110  else if (method == "save") {
111  nargchk(nrhs==3 && nlhs==0);
112  obj->save(rhs[2].toString());
113  }
114  else if (method == "empty") {
115  nargchk(nrhs==2 && nlhs<=1);
116  plhs[0] = MxArray(obj->empty());
117  }
118  else if (method == "getDefaultName") {
119  nargchk(nrhs==2 && nlhs<=1);
120  plhs[0] = MxArray(obj->getDefaultName());
121  }
122  else if (method == "iterate") {
123  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
124  int num_iterations = 4;
125  for (int i=3; i<nrhs; i+=2) {
126  string key(rhs[i].toString());
127  if (key == "NumIterations")
128  num_iterations = rhs[i+1].toInt();
129  else
130  mexErrMsgIdAndTxt("mexopencv:error",
131  "Unrecognized option %s", key.c_str());
132  }
133  Mat img(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
134  (rhs[2].isUint16() ? CV_16U : CV_32F)));
135  obj->iterate(img, num_iterations);
136  }
137  else if (method == "getNumberOfSuperpixels") {
138  nargchk(nrhs==2 && nlhs<=1);
139  plhs[0] = MxArray(obj->getNumberOfSuperpixels());
140  }
141  else if (method == "getLabels") {
142  nargchk(nrhs==2 && nlhs<=1);
143  Mat labels_out;
144  obj->getLabels(labels_out);
145  plhs[0] = MxArray(labels_out);
146  }
147  else if (method == "getLabelContourMask") {
148  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
149  bool thick_line = false;
150  for (int i=2; i<nrhs; i+=2) {
151  string key(rhs[i].toString());
152  if (key == "ThickLine")
153  thick_line = rhs[i+1].toBool();
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized option %s", key.c_str());
157  }
158  Mat image;
159  obj->getLabelContourMask(image, thick_line);
160  plhs[0] = MxArray(image);
161  }
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized operation %s", method.c_str());
165 }
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.