mexopencv  0.1
mex interface for opencv library
Tonemap_.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<Tonemap> > obj_;
19 
25 Ptr<Tonemap> create_Tonemap(
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  float gamma = 1.0f;
32  for (; first != last; first += 2) {
33  string key(first->toString());
34  const MxArray& val = *(first + 1);
35  if (key == "Gamma")
36  gamma = val.toFloat();
37  else
38  mexErrMsgIdAndTxt("mexopencv:error",
39  "Unrecognized option %s", key.c_str());
40  }
41  return createTonemap(gamma);
42 }
43 }
44 
52 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
53 {
54  // Check the number of arguments
55  nargchk(nrhs>=2 && nlhs<=1);
56 
57  // Argument vector
58  vector<MxArray> rhs(prhs, prhs+nrhs);
59  int id = rhs[0].toInt();
60  string method(rhs[1].toString());
61 
62  // Constructor is called. Create a new object from argument
63  if (method == "new") {
64  nargchk(nrhs>=2 && nlhs<=1);
65  obj_[++last_id] = create_Tonemap(
66  rhs.begin() + 2, rhs.end());
67  plhs[0] = MxArray(last_id);
68  return;
69  }
70 
71  // Big operation switch
72  Ptr<Tonemap> obj = obj_[id];
73  if (method == "delete") {
74  nargchk(nrhs==2 && nlhs==0);
75  obj_.erase(id);
76  }
77  else if (method == "clear") {
78  nargchk(nrhs==2 && nlhs==0);
79  obj->clear();
80  }
81  else if (method == "load") {
82  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
83  string objname;
84  bool loadFromString = false;
85  for (int i=3; i<nrhs; i+=2) {
86  string key(rhs[i].toString());
87  if (key == "ObjName")
88  objname = rhs[i+1].toString();
89  else if (key == "FromString")
90  loadFromString = rhs[i+1].toBool();
91  else
92  mexErrMsgIdAndTxt("mexopencv:error",
93  "Unrecognized option %s", key.c_str());
94  }
95  /*
96  obj_[id] = (loadFromString ?
97  Algorithm::loadFromString<Tonemap>(rhs[2].toString(), objname) :
98  Algorithm::load<Tonemap>(rhs[2].toString(), objname));
99  */
101  // HACK: workaround for missing Tonemap::create()
102  FileStorage fs(rhs[2].toString(), FileStorage::READ +
103  (loadFromString ? FileStorage::MEMORY : 0));
104  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
105  if (obj.empty())
106  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
107  //*/
108  }
109  else if (method == "save") {
110  nargchk(nrhs==3 && nlhs==0);
111  obj->save(rhs[2].toString());
112  }
113  else if (method == "empty") {
114  nargchk(nrhs==2 && nlhs<=1);
115  plhs[0] = MxArray(obj->empty());
116  }
117  else if (method == "getDefaultName") {
118  nargchk(nrhs==2 && nlhs<=1);
119  plhs[0] = MxArray(obj->getDefaultName());
120  }
121  else if (method == "process") {
122  nargchk(nrhs==3 && nlhs<=1);
123  Mat src(rhs[2].toMat(CV_32F)), dst;
124  obj->process(src, dst);
125  plhs[0] = MxArray(dst);
126  }
127  else if (method == "get") {
128  nargchk(nrhs==3 && nlhs<=1);
129  string prop(rhs[2].toString());
130  if (prop == "Gamma")
131  plhs[0] = MxArray(obj->getGamma());
132  else
133  mexErrMsgIdAndTxt("mexopencv:error",
134  "Unrecognized property %s", prop.c_str());
135  }
136  else if (method == "set") {
137  nargchk(nrhs==4 && nlhs==0);
138  string prop(rhs[2].toString());
139  if (prop == "Gamma")
140  obj->setGamma(rhs[3].toFloat());
141  else
142  mexErrMsgIdAndTxt("mexopencv:error",
143  "Unrecognized property %s", prop.c_str());
144  }
145  else
146  mexErrMsgIdAndTxt("mexopencv:error",
147  "Unrecognized operation %s", method.c_str());
148 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Tonemap_.cpp:52
float toFloat() const
Convert MxArray to float.
Definition: MxArray.cpp:503
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.