mexopencv  3.4.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;
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  mexLock();
60  return;
61  }
62 
63  // Big operation switch
64  Ptr<GraphSegmentation> obj = obj_[id];
65  if (obj.empty())
66  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
67  if (method == "delete") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj_.erase(id);
70  mexUnlock();
71  }
72  else if (method == "clear") {
73  nargchk(nrhs==2 && nlhs==0);
74  obj->clear();
75  }
76  else if (method == "load") {
77  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
78  string objname;
79  bool loadFromString = false;
80  for (int i=3; i<nrhs; i+=2) {
81  string key(rhs[i].toString());
82  if (key == "ObjName")
83  objname = rhs[i+1].toString();
84  else if (key == "FromString")
85  loadFromString = rhs[i+1].toBool();
86  else
87  mexErrMsgIdAndTxt("mexopencv:error",
88  "Unrecognized option %s", key.c_str());
89  }
90  /*
91  obj_[id] = (loadFromString ?
92  Algorithm::loadFromString<GraphSegmentation>(rhs[2].toString(), objname) :
93  Algorithm::load<GraphSegmentation>(rhs[2].toString(), objname));
94  */
96  // HACK: workaround for missing GraphSegmentation::create()
97  FileStorage fs(rhs[2].toString(), FileStorage::READ +
98  (loadFromString ? FileStorage::MEMORY : 0));
99  if (!fs.isOpened())
100  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
101  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
102  if (fn.empty())
103  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
104  obj->read(fn);
105  //*/
106  }
107  else if (method == "save") {
108  nargchk(nrhs==3 && nlhs==0);
109  obj->save(rhs[2].toString());
110  }
111  else if (method == "empty") {
112  nargchk(nrhs==2 && nlhs<=1);
113  plhs[0] = MxArray(obj->empty());
114  }
115  else if (method == "getDefaultName") {
116  nargchk(nrhs==2 && nlhs<=1);
117  plhs[0] = MxArray(obj->getDefaultName());
118  }
119  else if (method == "processImage") {
120  nargchk(nrhs==3 && nlhs<=1);
121  Mat src(rhs[2].toMat()), dst;
122  obj->processImage(src, dst);
123  plhs[0] = MxArray(dst);
124  }
125  else if (method == "get") {
126  nargchk(nrhs==3 && nlhs<=1);
127  string prop(rhs[2].toString());
128  if (prop == "Sigma")
129  plhs[0] = MxArray(obj->getSigma());
130  else if (prop == "K")
131  plhs[0] = MxArray(obj->getK());
132  else if (prop == "MinSize")
133  plhs[0] = MxArray(obj->getMinSize());
134  else
135  mexErrMsgIdAndTxt("mexopencv:error",
136  "Unrecognized property %s", prop.c_str());
137  }
138  else if (method == "set") {
139  nargchk(nrhs==4 && nlhs==0);
140  string prop(rhs[2].toString());
141  if (prop == "Sigma")
142  obj->setSigma(rhs[3].toDouble());
143  else if (prop == "K")
144  obj->setK(rhs[3].toFloat());
145  else if (prop == "MinSize")
146  obj->setMinSize(rhs[3].toInt());
147  else
148  mexErrMsgIdAndTxt("mexopencv:error",
149  "Unrecognized property %s", prop.c_str());
150  }
151  else
152  mexErrMsgIdAndTxt("mexopencv:error",
153  "Unrecognized operation %s", method.c_str());
154 }
T empty(T... args)
virtual void setSigma(double sigma)=0
virtual void processImage(InputArray src, OutputArray dst)=0
map< int, Ptr< GraphSegmentation > > obj_
Object container.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void clear()
virtual void read(const FileNode &fn)
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
virtual void setMinSize(int min_size)=0
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
Ptr< GraphSegmentation > createGraphSegmentation(double sigma=0.5, float k=300, int min_size=100)
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const