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