mexopencv  3.4.1
MEX interface for OpenCV library
SelectiveSearchSegmentation_.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::segmentation;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 
29 {
30  ptrdiff_t len = std::distance(first, last);
31  nargchk((len%2)==0);
32  double sigma = 0.5;
33  float k = 300;
34  int min_size = 100;
35  for (; first != last; first += 2) {
36  string key(first->toString());
37  const MxArray& val = *(first + 1);
38  if (key == "Sigma")
39  sigma = val.toDouble();
40  else if (key == "K")
41  k = val.toFloat();
42  else if (key == "MinSize")
43  min_size = val.toInt();
44  else
45  mexErrMsgIdAndTxt("mexopencv:error",
46  "Unrecognized option %s", key.c_str());
47  }
48  return createGraphSegmentation(sigma, k, min_size);
49 }
50 
59 {
60  ptrdiff_t len = std::distance(first, last);
61  nargchk(len>=0);
64  for (; first != last; ++first) {
65  string type(first->toString());
67  if (type == "Color")
69  else if (type == "Size")
71  else if (type == "Texture")
73  else if (type == "Fill")
75  else
76  mexErrMsgIdAndTxt("mexopencv:error",
77  "Unrecognized segmentation strategy %s", type.c_str());
78  p->addStrategy(s, 1.0f / len); // equal weights
79  }
80  return p;
81 }
82 
95  const string& type,
98 {
100  if (type == "Color")
102  else if (type == "Size")
104  else if (type == "Texture")
106  else if (type == "Fill")
108  else if (type == "Multiple")
110  else
111  mexErrMsgIdAndTxt("mexopencv:error",
112  "Unrecognized segmentation strategy %s", type.c_str());
113  if (p.empty())
114  mexErrMsgIdAndTxt("mexopencv:error",
115  "Failed to create SelectiveSearchSegmentationStrategy");
116  return p;
117 }
118 }
119 
127 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
128 {
129  // Check the number of arguments
130  nargchk(nrhs>=2 && nlhs<=1);
131 
132  // Argument vector
133  vector<MxArray> rhs(prhs, prhs+nrhs);
134  int id = rhs[0].toInt();
135  string method(rhs[1].toString());
136 
137  // Constructor is called. Create a new object from argument
138  if (method == "new") {
139  nargchk(nrhs==2 && nlhs<=1);
141  plhs[0] = MxArray(last_id);
142  mexLock();
143  return;
144  }
145 
146  // Big operation switch
148  if (obj.empty())
149  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
150  if (method == "delete") {
151  nargchk(nrhs==2 && nlhs==0);
152  obj_.erase(id);
153  mexUnlock();
154  }
155  else if (method == "clear") {
156  nargchk(nrhs==2 && nlhs==0);
157  obj->clear();
158  }
159  else if (method == "load") {
160  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
161  string objname;
162  bool loadFromString = false;
163  for (int i=3; i<nrhs; i+=2) {
164  string key(rhs[i].toString());
165  if (key == "ObjName")
166  objname = rhs[i+1].toString();
167  else if (key == "FromString")
168  loadFromString = rhs[i+1].toBool();
169  else
170  mexErrMsgIdAndTxt("mexopencv:error",
171  "Unrecognized option %s", key.c_str());
172  }
173  /*
174  obj_[id] = (loadFromString ?
175  Algorithm::loadFromString<SelectiveSearchSegmentation>(rhs[2].toString(), objname) :
176  Algorithm::load<SelectiveSearchSegmentation>(rhs[2].toString(), objname));
177  */
179  // HACK: workaround for missing SelectiveSearchSegmentation::create()
180  FileStorage fs(rhs[2].toString(), FileStorage::READ +
181  (loadFromString ? FileStorage::MEMORY : 0));
182  if (!fs.isOpened())
183  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
184  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
185  if (fn.empty())
186  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
187  obj->read(fn);
188  //*/
189  }
190  else if (method == "save") {
191  nargchk(nrhs==3 && nlhs==0);
192  obj->save(rhs[2].toString());
193  }
194  else if (method == "empty") {
195  nargchk(nrhs==2 && nlhs<=1);
196  plhs[0] = MxArray(obj->empty());
197  }
198  else if (method == "getDefaultName") {
199  nargchk(nrhs==2 && nlhs<=1);
200  plhs[0] = MxArray(obj->getDefaultName());
201  }
202  else if (method == "setBaseImage") {
203  nargchk(nrhs==3 && nlhs==0);
204  Mat img(rhs[2].toMat());
205  obj->setBaseImage(img);
206  }
207  else if (method == "switchToSingleStrategy") {
208  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
209  int k = 200;
210  float sigma = 0.8f;
211  for (int i=2; i<nrhs; i+=2) {
212  string key(rhs[i].toString());
213  if (key == "K")
214  k = rhs[i+1].toInt();
215  else if (key == "Sigma")
216  sigma = rhs[i+1].toFloat();
217  else
218  mexErrMsgIdAndTxt("mexopencv:error",
219  "Unrecognized option %s", key.c_str());
220  }
221  obj->switchToSingleStrategy(k, sigma);
222  }
223  else if (method == "switchToSelectiveSearchFast") {
224  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
225  int base_k = 150;
226  int inc_k = 150;
227  float sigma = 0.8f;
228  for (int i=2; i<nrhs; i+=2) {
229  string key(rhs[i].toString());
230  if (key == "BaseK")
231  base_k = rhs[i+1].toInt();
232  else if (key == "IncK")
233  inc_k = rhs[i+1].toInt();
234  else if (key == "Sigma")
235  sigma = rhs[i+1].toFloat();
236  else
237  mexErrMsgIdAndTxt("mexopencv:error",
238  "Unrecognized option %s", key.c_str());
239  }
240  obj->switchToSelectiveSearchFast(base_k, inc_k, sigma);
241  }
242  else if (method == "switchToSelectiveSearchQuality") {
243  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
244  int base_k = 150;
245  int inc_k = 150;
246  float sigma = 0.8f;
247  for (int i=2; i<nrhs; i+=2) {
248  string key(rhs[i].toString());
249  if (key == "BaseK")
250  base_k = rhs[i+1].toInt();
251  else if (key == "IncK")
252  inc_k = rhs[i+1].toInt();
253  else if (key == "Sigma")
254  sigma = rhs[i+1].toFloat();
255  else
256  mexErrMsgIdAndTxt("mexopencv:error",
257  "Unrecognized option %s", key.c_str());
258  }
259  obj->switchToSelectiveSearchQuality(base_k, inc_k, sigma);
260  }
261  else if (method == "addImage") {
262  nargchk(nrhs==3 && nlhs==0);
263  Mat img(rhs[2].toMat());
264  obj->addImage(img);
265  }
266  else if (method == "clearImages") {
267  nargchk(nrhs==2 && nlhs==0);
268  obj->clearImages();
269  }
270  else if (method == "addGraphSegmentation") {
271  nargchk(nrhs>=2 && nlhs==0);
273  rhs.begin() + 2, rhs.end());
274  obj->addGraphSegmentation(g);
275  }
276  else if (method == "clearGraphSegmentations") {
277  nargchk(nrhs==2 && nlhs==0);
279  }
280  else if (method == "addStrategy") {
281  nargchk(nrhs>=3 && nlhs==0);
284  rhs[2].toString(), rhs.begin() + 3, rhs.end());
285  obj->addStrategy(s);
286  }
287  else if (method == "clearStrategies") {
288  nargchk(nrhs==2 && nlhs==0);
289  obj->clearStrategies();
290  }
291  else if (method == "process") {
292  nargchk(nrhs==2 && nlhs<=1);
293  vector<Rect> rects;
294  obj->process(rects);
295  plhs[0] = MxArray(Mat(rects, false).reshape(1,0)); // Nx4
296  }
297  else
298  mexErrMsgIdAndTxt("mexopencv:error",
299  "Unrecognized operation %s", method.c_str());
300 }
map< int, Ptr< SelectiveSearchSegmentation > > obj_
Object container.
T empty(T... args)
T distance(T... args)
Ptr< SelectiveSearchSegmentationStrategyMultiple > createSelectiveSearchSegmentationStrategyMultiple()
Ptr< SelectiveSearchSegmentationStrategyColor > createSelectiveSearchSegmentationStrategyColor()
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void switchToSelectiveSearchQuality(int base_k=150, int inc_k=150, float sigma=0.8f)=0
STL namespace.
T end(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void addGraphSegmentation(Ptr< GraphSegmentation > g)=0
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void process(std::vector< Rect > &rects)=0
virtual void addStrategy(Ptr< SelectiveSearchSegmentationStrategy > s)=0
virtual void clear()
virtual void read(const FileNode &fn)
Ptr< SelectiveSearchSegmentation > createSelectiveSearchSegmentation()
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...
Ptr< SelectiveSearchSegmentationStrategy > create_SelectiveSearchSegmentationStrategy(const string &type, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of SelectiveSearchSegmentationStrategy using options in arguments.
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
virtual void addStrategy(Ptr< SelectiveSearchSegmentationStrategy > g, float weight)=0
virtual void switchToSelectiveSearchFast(int base_k=150, int inc_k=150, float sigma=0.8f)=0
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
Ptr< GraphSegmentation > create_GraphSegmentation(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of GraphSegmentation using options in arguments.
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
Ptr< GraphSegmentation > createGraphSegmentation(double sigma=0.5, float k=300, int min_size=100)
Ptr< SelectiveSearchSegmentationStrategySize > createSelectiveSearchSegmentationStrategySize()
double toDouble() const
Convert MxArray to double.
Definition: MxArray.cpp:496
virtual void save(const String &filename) const
virtual bool empty() const
Ptr< SelectiveSearchSegmentationStrategyMultiple > create_SelectiveSearchSegmentationStrategyMultiple(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of SelectiveSearchSegmentationStrategyMultiple using options in arguments...
int type() const
Ptr< SelectiveSearchSegmentationStrategyTexture > createSelectiveSearchSegmentationStrategyTexture()
cv::Mat toMat() const
Ptr< SelectiveSearchSegmentationStrategyFill > createSelectiveSearchSegmentationStrategyFill()
virtual void switchToSingleStrategy(int k=200, float sigma=0.8f)=0