mexopencv  0.1
mex interface for opencv library
MergeRobertson_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/photo.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
18 map<int,Ptr<MergeRobertson> > obj_;
19 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // Constructor is called. Create a new object from argument
39  if (method == "new") {
40  nargchk(nrhs==2 && nlhs<=1);
41  obj_[++last_id] = createMergeRobertson();
42  plhs[0] = MxArray(last_id);
43  return;
44  }
45 
46  // Big operation switch
47  Ptr<MergeRobertson> obj = obj_[id];
48  if (method == "delete") {
49  nargchk(nrhs==2 && nlhs==0);
50  obj_.erase(id);
51  }
52  else if (method == "clear") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj->clear();
55  }
56  else if (method == "load") {
57  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
58  string objname;
59  bool loadFromString = false;
60  for (int i=3; i<nrhs; i+=2) {
61  string key(rhs[i].toString());
62  if (key == "ObjName")
63  objname = rhs[i+1].toString();
64  else if (key == "FromString")
65  loadFromString = rhs[i+1].toBool();
66  else
67  mexErrMsgIdAndTxt("mexopencv:error",
68  "Unrecognized option %s", key.c_str());
69  }
70  /*
71  obj_[id] = (loadFromString ?
72  Algorithm::loadFromString<MergeRobertson>(rhs[2].toString(), objname) :
73  Algorithm::load<MergeRobertson>(rhs[2].toString(), objname));
74  */
76  // HACK: workaround for missing MergeRobertson::create()
77  FileStorage fs(rhs[2].toString(), FileStorage::READ +
78  (loadFromString ? FileStorage::MEMORY : 0));
79  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
80  if (obj.empty())
81  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
82  //*/
83  }
84  else if (method == "save") {
85  nargchk(nrhs==3 && nlhs==0);
86  obj->save(rhs[2].toString());
87  }
88  else if (method == "empty") {
89  nargchk(nrhs==2 && nlhs<=1);
90  plhs[0] = MxArray(obj->empty());
91  }
92  else if (method == "getDefaultName") {
93  nargchk(nrhs==2 && nlhs<=1);
94  plhs[0] = MxArray(obj->getDefaultName());
95  }
96  else if (method == "process") {
97  nargchk((nrhs==4 || nrhs==5) && nlhs<=1);
98  vector<Mat> src;
99  {
100  vector<MxArray> arr(rhs[2].toVector<MxArray>());
101  src.reserve(arr.size());
102  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
103  src.push_back(it->toMat(CV_8U));
104  }
105  Mat times(rhs[3].toMat(CV_32F)), dst;
106  if (nrhs == 5) {
107  Mat response(rhs[4].toMat(CV_32F));
108  obj->process(src, dst, times, response);
109  }
110  else
111  obj->process(src, dst, times);
112  plhs[0] = MxArray(dst);
113  }
114  else
115  mexErrMsgIdAndTxt("mexopencv:error",
116  "Unrecognized operation %s", method.c_str());
117 }
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.