mexopencv  3.4.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;
19 
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);
69  rhs.begin() + 2, rhs.end());
70  plhs[0] = MxArray(last_id);
71  mexLock();
72  return;
73  }
74 
75  // Big operation switch
76  Ptr<CalibrateRobertson> obj = obj_[id];
77  if (obj.empty())
78  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
79  if (method == "delete") {
80  nargchk(nrhs==2 && nlhs==0);
81  obj_.erase(id);
82  mexUnlock();
83  }
84  else if (method == "clear") {
85  nargchk(nrhs==2 && nlhs==0);
86  obj->clear();
87  }
88  else if (method == "load") {
89  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
90  string objname;
91  bool loadFromString = false;
92  for (int i=3; i<nrhs; i+=2) {
93  string key(rhs[i].toString());
94  if (key == "ObjName")
95  objname = rhs[i+1].toString();
96  else if (key == "FromString")
97  loadFromString = rhs[i+1].toBool();
98  else
99  mexErrMsgIdAndTxt("mexopencv:error",
100  "Unrecognized option %s", key.c_str());
101  }
102  /*
103  obj_[id] = (loadFromString ?
104  Algorithm::loadFromString<CalibrateRobertson>(rhs[2].toString(), objname) :
105  Algorithm::load<CalibrateRobertson>(rhs[2].toString(), objname));
106  */
108  // HACK: workaround for missing CalibrateRobertson::create()
109  FileStorage fs(rhs[2].toString(), FileStorage::READ +
110  (loadFromString ? FileStorage::MEMORY : 0));
111  if (!fs.isOpened())
112  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
113  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
114  if (fn.empty())
115  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
116  obj->read(fn);
117  //*/
118  }
119  else if (method == "save") {
120  nargchk(nrhs==3 && nlhs==0);
121  obj->save(rhs[2].toString());
122  }
123  else if (method == "empty") {
124  nargchk(nrhs==2 && nlhs<=1);
125  plhs[0] = MxArray(obj->empty());
126  }
127  else if (method == "getDefaultName") {
128  nargchk(nrhs==2 && nlhs<=1);
129  plhs[0] = MxArray(obj->getDefaultName());
130  }
131  else if (method == "process") {
132  nargchk(nrhs==4 && nlhs<=1);
133  vector<Mat> src;
134  {
135  vector<MxArray> arr(rhs[2].toVector<MxArray>());
136  src.reserve(arr.size());
137  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
138  src.push_back(it->toMat(CV_8U));
139  }
140  Mat times(rhs[3].toMat(CV_32F)), dst;
141  obj->process(src, dst, times);
142  plhs[0] = MxArray(dst);
143  }
144  else if (method == "getRadiance") {
145  nargchk(nrhs==2 && nlhs<=1);
146  plhs[0] = MxArray(obj->getRadiance());
147  }
148  else if (method == "get") {
149  nargchk(nrhs==3 && nlhs<=1);
150  string prop(rhs[2].toString());
151  if (prop == "MaxIter")
152  plhs[0] = MxArray(obj->getMaxIter());
153  else if (prop == "Threshold")
154  plhs[0] = MxArray(obj->getThreshold());
155  else
156  mexErrMsgIdAndTxt("mexopencv:error",
157  "Unrecognized property %s", prop.c_str());
158  }
159  else if (method == "set") {
160  nargchk(nrhs==4 && nlhs==0);
161  string prop(rhs[2].toString());
162  if (prop == "MaxIter")
163  obj->setMaxIter(rhs[3].toInt());
164  else if (prop == "Threshold")
165  obj->setThreshold(rhs[3].toFloat());
166  else
167  mexErrMsgIdAndTxt("mexopencv:error",
168  "Unrecognized property %s", prop.c_str());
169  }
170  else
171  mexErrMsgIdAndTxt("mexopencv:error",
172  "Unrecognized operation %s", method.c_str());
173 }
virtual void setThreshold(float threshold)=0
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
T empty(T... args)
T distance(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual Mat getRadiance() const=0
#define CV_8U
STL namespace.
T end(T... args)
virtual bool isOpened() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
map< int, Ptr< CalibrateRobertson > > obj_
Object container.
T push_back(T... args)
virtual void clear()
virtual void process(InputArrayOfArrays src, OutputArray dst, InputArray times)=0
#define CV_32F
virtual float getThreshold() const=0
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
T size(T... args)
STL class.
bool empty() const
Ptr< CalibrateRobertson > createCalibrateRobertson(int max_iter=30, float threshold=0.01f)
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
Ptr< CalibrateRobertson > create_CalibrateRobertson(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of CalibrateRobertson using options in arguments.
double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
virtual int getMaxIter() const=0
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setMaxIter(int max_iter)=0
cv::Mat toMat() const
T reserve(T... args)