mexopencv  0.1
mex interface for opencv library
CalibrateRobertson_.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<CalibrateRobertson> > obj_;
19 
25 Ptr<CalibrateRobertson> create_CalibrateRobertson(
26  vector<MxArray>::const_iterator first,
27  vector<MxArray>::const_iterator last)
28 {
29  ptrdiff_t len = std::distance(first, last);
30  nargchk((len%2)==0);
31  int max_iter = 30;
32  float threshold = 0.01f;
33  for (; first != last; first += 2) {
34  string key(first->toString());
35  const MxArray& val = *(first + 1);
36  if (key == "MaxIter")
37  max_iter = val.toInt();
38  else if (key == "Threshold")
39  threshold = val.toFloat();
40  else
41  mexErrMsgIdAndTxt("mexopencv:error",
42  "Unrecognized option %s", key.c_str());
43  }
44  return createCalibrateRobertson(max_iter, threshold);
45 }
46 }
47 
55 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
56 {
57  // Check the number of arguments
58  nargchk(nrhs>=2 && nlhs<=1);
59 
60  // Argument vector
61  vector<MxArray> rhs(prhs, prhs+nrhs);
62  int id = rhs[0].toInt();
63  string method(rhs[1].toString());
64 
65  // Constructor is called. Create a new object from argument
66  if (method == "new") {
67  nargchk(nrhs>=2 && nlhs<=1);
68  obj_[++last_id] = create_CalibrateRobertson(
69  rhs.begin() + 2, rhs.end());
70  plhs[0] = MxArray(last_id);
71  return;
72  }
73 
74  // Big operation switch
75  Ptr<CalibrateRobertson> obj = obj_[id];
76  if (method == "delete") {
77  nargchk(nrhs==2 && nlhs==0);
78  obj_.erase(id);
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<CalibrateRobertson>(rhs[2].toString(), objname) :
101  Algorithm::load<CalibrateRobertson>(rhs[2].toString(), objname));
102  */
104  // HACK: workaround for missing CalibrateRobertson::create()
105  FileStorage fs(rhs[2].toString(), FileStorage::READ +
106  (loadFromString ? FileStorage::MEMORY : 0));
107  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
108  if (obj.empty())
109  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
110  //*/
111  }
112  else if (method == "save") {
113  nargchk(nrhs==3 && nlhs==0);
114  obj->save(rhs[2].toString());
115  }
116  else if (method == "empty") {
117  nargchk(nrhs==2 && nlhs<=1);
118  plhs[0] = MxArray(obj->empty());
119  }
120  else if (method == "getDefaultName") {
121  nargchk(nrhs==2 && nlhs<=1);
122  plhs[0] = MxArray(obj->getDefaultName());
123  }
124  else if (method == "process") {
125  nargchk(nrhs==4 && nlhs<=1);
126  vector<Mat> src;
127  {
128  vector<MxArray> arr(rhs[2].toVector<MxArray>());
129  src.reserve(arr.size());
130  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
131  src.push_back(it->toMat(CV_8U));
132  }
133  Mat times(rhs[3].toMat(CV_32F)), dst;
134  obj->process(src, dst, times);
135  plhs[0] = MxArray(dst);
136  }
137  else if (method == "getRadiance") {
138  nargchk(nrhs==2 && nlhs<=1);
139  plhs[0] = MxArray(obj->getRadiance());
140  }
141  else if (method == "get") {
142  nargchk(nrhs==3 && nlhs<=1);
143  string prop(rhs[2].toString());
144  if (prop == "MaxIter")
145  plhs[0] = MxArray(obj->getMaxIter());
146  else if (prop == "Threshold")
147  plhs[0] = MxArray(obj->getThreshold());
148  else
149  mexErrMsgIdAndTxt("mexopencv:error",
150  "Unrecognized property %s", prop.c_str());
151  }
152  else if (method == "set") {
153  nargchk(nrhs==4 && nlhs==0);
154  string prop(rhs[2].toString());
155  if (prop == "MaxIter")
156  obj->setMaxIter(rhs[3].toInt());
157  else if (prop == "Threshold")
158  obj->setThreshold(rhs[3].toFloat());
159  else
160  mexErrMsgIdAndTxt("mexopencv:error",
161  "Unrecognized property %s", prop.c_str());
162  }
163  else
164  mexErrMsgIdAndTxt("mexopencv:error",
165  "Unrecognized operation %s", method.c_str());
166 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
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.