mexopencv  0.1
mex interface for opencv library
GraphSegmentation_.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 // Persistent objects
15 namespace {
17 int last_id = 0;
19 map<int,Ptr<GraphSegmentation> > 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>=2 && (nrhs%2)==0 && nlhs<=1);
42  double sigma = 0.5;
43  float k = 300;
44  int min_size = 100;
45  for (int i=2; i<nrhs; i+=2) {
46  string key(rhs[i].toString());
47  if (key == "Sigma")
48  sigma = rhs[i+1].toDouble();
49  else if (key == "K")
50  k = rhs[i+1].toFloat();
51  else if (key == "MinSize")
52  min_size = rhs[i+1].toInt();
53  else
54  mexErrMsgIdAndTxt("mexopencv:error",
55  "Unrecognized option %s", key.c_str());
56  }
57  obj_[++last_id] = createGraphSegmentation(sigma, k, min_size);
58  plhs[0] = MxArray(last_id);
59  return;
60  }
61 
62  // Big operation switch
63  Ptr<GraphSegmentation> obj = obj_[id];
64  if (method == "delete") {
65  nargchk(nrhs==2 && nlhs==0);
66  obj_.erase(id);
67  }
68  else if (method == "clear") {
69  nargchk(nrhs==2 && nlhs==0);
70  obj->clear();
71  }
72  else if (method == "load") {
73  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
74  string objname;
75  bool loadFromString = false;
76  for (int i=3; i<nrhs; i+=2) {
77  string key(rhs[i].toString());
78  if (key == "ObjName")
79  objname = rhs[i+1].toString();
80  else if (key == "FromString")
81  loadFromString = rhs[i+1].toBool();
82  else
83  mexErrMsgIdAndTxt("mexopencv:error",
84  "Unrecognized option %s", key.c_str());
85  }
86  /*
87  obj_[id] = (loadFromString ?
88  Algorithm::loadFromString<GraphSegmentation>(rhs[2].toString(), objname) :
89  Algorithm::load<GraphSegmentation>(rhs[2].toString(), objname));
90  */
92  // HACK: workaround for missing GraphSegmentation::create()
93  FileStorage fs(rhs[2].toString(), FileStorage::READ +
94  (loadFromString ? FileStorage::MEMORY : 0));
95  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
96  if (obj.empty())
97  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
98  //*/
99  }
100  else if (method == "save") {
101  nargchk(nrhs==3 && nlhs==0);
102  obj->save(rhs[2].toString());
103  }
104  else if (method == "empty") {
105  nargchk(nrhs==2 && nlhs<=1);
106  plhs[0] = MxArray(obj->empty());
107  }
108  else if (method == "getDefaultName") {
109  nargchk(nrhs==2 && nlhs<=1);
110  plhs[0] = MxArray(obj->getDefaultName());
111  }
112  else if (method == "processImage") {
113  nargchk(nrhs==3 && nlhs<=1);
114  Mat src(rhs[2].toMat()), dst;
115  obj->processImage(src, dst);
116  plhs[0] = MxArray(dst);
117  }
118  else if (method == "get") {
119  nargchk(nrhs==3 && nlhs<=1);
120  string prop(rhs[2].toString());
121  if (prop == "Sigma")
122  plhs[0] = MxArray(obj->getSigma());
123  else if (prop == "K")
124  plhs[0] = MxArray(obj->getK());
125  else if (prop == "MinSize")
126  plhs[0] = MxArray(obj->getMinSize());
127  else
128  mexErrMsgIdAndTxt("mexopencv:error",
129  "Unrecognized property %s", prop.c_str());
130  }
131  else if (method == "set") {
132  nargchk(nrhs==4 && nlhs==0);
133  string prop(rhs[2].toString());
134  if (prop == "Sigma")
135  obj->setSigma(rhs[3].toDouble());
136  else if (prop == "K")
137  obj->setK(rhs[3].toFloat());
138  else if (prop == "MinSize")
139  obj->setMinSize(rhs[3].toInt());
140  else
141  mexErrMsgIdAndTxt("mexopencv:error",
142  "Unrecognized property %s", prop.c_str());
143  }
144  else
145  mexErrMsgIdAndTxt("mexopencv:error",
146  "Unrecognized operation %s", method.c_str());
147 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.