mexopencv  3.4.1
MEX interface for OpenCV library
CalibrateDebevec_.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 samples = 70;
32  float lambda = 10.0f;
33  bool random = false;
34  for (; first != last; first += 2) {
35  string key(first->toString());
36  const MxArray& val = *(first + 1);
37  if (key == "Samples")
38  samples = val.toInt();
39  else if (key == "Lambda")
40  lambda = val.toFloat();
41  else if (key == "Random")
42  random = val.toBool();
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47  return createCalibrateDebevec(samples, lambda, random);
48 }
49 }
50 
58 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
59 {
60  // Check the number of arguments
61  nargchk(nrhs>=2 && nlhs<=1);
62 
63  // Argument vector
64  vector<MxArray> rhs(prhs, prhs+nrhs);
65  int id = rhs[0].toInt();
66  string method(rhs[1].toString());
67 
68  // Constructor is called. Create a new object from argument
69  if (method == "new") {
70  nargchk(nrhs>=2 && nlhs<=1);
72  rhs.begin() + 2, rhs.end());
73  plhs[0] = MxArray(last_id);
74  mexLock();
75  return;
76  }
77 
78  // Big operation switch
79  Ptr<CalibrateDebevec> obj = obj_[id];
80  if (obj.empty())
81  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
82  if (method == "delete") {
83  nargchk(nrhs==2 && nlhs==0);
84  obj_.erase(id);
85  mexUnlock();
86  }
87  else if (method == "clear") {
88  nargchk(nrhs==2 && nlhs==0);
89  obj->clear();
90  }
91  else if (method == "load") {
92  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
93  string objname;
94  bool loadFromString = false;
95  for (int i=3; i<nrhs; i+=2) {
96  string key(rhs[i].toString());
97  if (key == "ObjName")
98  objname = rhs[i+1].toString();
99  else if (key == "FromString")
100  loadFromString = rhs[i+1].toBool();
101  else
102  mexErrMsgIdAndTxt("mexopencv:error",
103  "Unrecognized option %s", key.c_str());
104  }
105  /*
106  obj_[id] = (loadFromString ?
107  Algorithm::loadFromString<CalibrateDebevec>(rhs[2].toString(), objname) :
108  Algorithm::load<CalibrateDebevec>(rhs[2].toString(), objname));
109  */
111  // HACK: workaround for missing CalibrateDebevec::create()
112  FileStorage fs(rhs[2].toString(), FileStorage::READ +
113  (loadFromString ? FileStorage::MEMORY : 0));
114  if (!fs.isOpened())
115  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
116  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
117  if (fn.empty())
118  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
119  obj->read(fn);
120  //*/
121  }
122  else if (method == "save") {
123  nargchk(nrhs==3 && nlhs==0);
124  obj->save(rhs[2].toString());
125  }
126  else if (method == "empty") {
127  nargchk(nrhs==2 && nlhs<=1);
128  plhs[0] = MxArray(obj->empty());
129  }
130  else if (method == "getDefaultName") {
131  nargchk(nrhs==2 && nlhs<=1);
132  plhs[0] = MxArray(obj->getDefaultName());
133  }
134  else if (method == "process") {
135  nargchk(nrhs==4 && nlhs<=1);
136  vector<Mat> src;
137  {
138  vector<MxArray> arr(rhs[2].toVector<MxArray>());
139  src.reserve(arr.size());
140  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
141  src.push_back(it->toMat(CV_8U));
142  }
143  Mat times(rhs[3].toMat(CV_32F)), dst;
144  obj->process(src, dst, times);
145  plhs[0] = MxArray(dst);
146  }
147  else if (method == "get") {
148  nargchk(nrhs==3 && nlhs<=1);
149  string prop(rhs[2].toString());
150  if (prop == "Samples")
151  plhs[0] = MxArray(obj->getSamples());
152  else if (prop == "Lambda")
153  plhs[0] = MxArray(obj->getLambda());
154  else if (prop == "Random")
155  plhs[0] = MxArray(obj->getRandom());
156  else
157  mexErrMsgIdAndTxt("mexopencv:error",
158  "Unrecognized property %s", prop.c_str());
159  }
160  else if (method == "set") {
161  nargchk(nrhs==4 && nlhs==0);
162  string prop(rhs[2].toString());
163  if (prop == "Samples")
164  obj->setSamples(rhs[3].toInt());
165  else if (prop == "Lambda")
166  obj->setLambda(rhs[3].toFloat());
167  else if (prop == "Random")
168  obj->setRandom(rhs[3].toBool());
169  else
170  mexErrMsgIdAndTxt("mexopencv:error",
171  "Unrecognized property %s", prop.c_str());
172  }
173  else
174  mexErrMsgIdAndTxt("mexopencv:error",
175  "Unrecognized operation %s", method.c_str());
176 }
int toInt() const
Convert MxArray to int.
Definition: MxArray.cpp:489
virtual float getLambda() const=0
int last_id
Last object id to allocate.
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.
Ptr< CalibrateDebevec > create_CalibrateDebevec(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of CalibrateDebevec using options in arguments.
virtual void setSamples(int samples)=0
#define CV_8U
STL namespace.
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
T push_back(T... args)
virtual void clear()
virtual void process(InputArrayOfArrays src, OutputArray dst, InputArray times)=0
#define CV_32F
virtual void read(const FileNode &fn)
Ptr< CalibrateDebevec > createCalibrateDebevec(int samples=70, float lambda=10.0f, bool random=false)
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...
map< int, Ptr< CalibrateDebevec > > obj_
Object container.
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual bool getRandom() const=0
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
virtual String getDefaultName() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Global constant definitions.
T begin(T... args)
virtual int getSamples() const=0
T c_str(T... args)
virtual void setRandom(bool random)=0
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const
T reserve(T... args)
virtual void setLambda(float lambda)=0