mexopencv  3.4.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;
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();
66  image_width, image_height, image_channels,
67  num_superpixels, num_levels, prior, histogram_bins, double_step);
68  plhs[0] = MxArray(last_id);
69  mexLock();
70  return;
71  }
72 
73  // Big operation switch
74  Ptr<SuperpixelSEEDS> obj = obj_[id];
75  if (obj.empty())
76  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
77  if (method == "delete") {
78  nargchk(nrhs==2 && nlhs==0);
79  obj_.erase(id);
80  mexUnlock();
81  }
82  else if (method == "clear") {
83  nargchk(nrhs==2 && nlhs==0);
84  obj->clear();
85  }
86  else if (method == "load") {
87  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
88  string objname;
89  bool loadFromString = false;
90  for (int i=3; i<nrhs; i+=2) {
91  string key(rhs[i].toString());
92  if (key == "ObjName")
93  objname = rhs[i+1].toString();
94  else if (key == "FromString")
95  loadFromString = rhs[i+1].toBool();
96  else
97  mexErrMsgIdAndTxt("mexopencv:error",
98  "Unrecognized option %s", key.c_str());
99  }
100  /*
101  obj_[id] = (loadFromString ?
102  Algorithm::loadFromString<SuperpixelSEEDS>(rhs[2].toString(), objname) :
103  Algorithm::load<SuperpixelSEEDS>(rhs[2].toString(), objname));
104  */
106  // HACK: workaround for missing SuperpixelSEEDS::create()
107  FileStorage fs(rhs[2].toString(), FileStorage::READ +
108  (loadFromString ? FileStorage::MEMORY : 0));
109  if (!fs.isOpened())
110  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
111  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
112  if (fn.empty())
113  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
114  obj->read(fn);
115  //*/
116  }
117  else if (method == "save") {
118  nargchk(nrhs==3 && nlhs==0);
119  obj->save(rhs[2].toString());
120  }
121  else if (method == "empty") {
122  nargchk(nrhs==2 && nlhs<=1);
123  plhs[0] = MxArray(obj->empty());
124  }
125  else if (method == "getDefaultName") {
126  nargchk(nrhs==2 && nlhs<=1);
127  plhs[0] = MxArray(obj->getDefaultName());
128  }
129  else if (method == "iterate") {
130  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
131  int num_iterations = 4;
132  for (int i=3; i<nrhs; i+=2) {
133  string key(rhs[i].toString());
134  if (key == "NumIterations")
135  num_iterations = rhs[i+1].toInt();
136  else
137  mexErrMsgIdAndTxt("mexopencv:error",
138  "Unrecognized option %s", key.c_str());
139  }
140  Mat img(rhs[2].toMat(rhs[2].isUint8() ? CV_8U :
141  (rhs[2].isUint16() ? CV_16U : CV_32F)));
142  obj->iterate(img, num_iterations);
143  }
144  else if (method == "getNumberOfSuperpixels") {
145  nargchk(nrhs==2 && nlhs<=1);
146  plhs[0] = MxArray(obj->getNumberOfSuperpixels());
147  }
148  else if (method == "getLabels") {
149  nargchk(nrhs==2 && nlhs<=1);
150  Mat labels_out;
151  obj->getLabels(labels_out);
152  plhs[0] = MxArray(labels_out);
153  }
154  else if (method == "getLabelContourMask") {
155  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
156  bool thick_line = false;
157  for (int i=2; i<nrhs; i+=2) {
158  string key(rhs[i].toString());
159  if (key == "ThickLine")
160  thick_line = rhs[i+1].toBool();
161  else
162  mexErrMsgIdAndTxt("mexopencv:error",
163  "Unrecognized option %s", key.c_str());
164  }
165  Mat image;
166  obj->getLabelContourMask(image, thick_line);
167  plhs[0] = MxArray(image);
168  }
169  else
170  mexErrMsgIdAndTxt("mexopencv:error",
171  "Unrecognized operation %s", method.c_str());
172 }
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 getLabelContourMask(OutputArray image, bool thick_line=false)=0
#define CV_8U
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
virtual int getNumberOfSuperpixels()=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
FileNode getFirstTopLevelNode() const
#define CV_16U
T size(T... args)
STL class.
virtual void getLabels(OutputArray labels_out)=0
bool empty() const
virtual void iterate(InputArray img, int num_iterations=4)=0
virtual String getDefaultName() const
Global constant definitions.
Ptr< SuperpixelSEEDS > createSuperpixelSEEDS(int image_width, int image_height, int image_channels, int num_superpixels, int num_levels, int prior=2, int histogram_bins=5, bool double_step=false)
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.
int last_id
Last object id to allocate.
cv::Mat toMat() const
map< int, Ptr< SuperpixelSEEDS > > obj_
Object container.