mexopencv  0.1
mex interface for opencv library
TonemapReinhard_.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<TonemapReinhard> > obj_;
19 
25 Ptr<TonemapReinhard> create_TonemapReinhard(
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  float intensity = 0.0f;
33  float light_adapt = 1.0f;
34  float color_adapt = 0.0f;
35  for (; first != last; first += 2) {
36  string key(first->toString());
37  const MxArray& val = *(first + 1);
38  if (key == "Gamma")
39  gamma = val.toFloat();
40  else if (key == "Intensity")
41  intensity = val.toFloat();
42  else if (key == "LightAdaptation")
43  light_adapt = val.toFloat();
44  else if (key == "ColorAdaptation")
45  color_adapt = val.toFloat();
46  else
47  mexErrMsgIdAndTxt("mexopencv:error",
48  "Unrecognized option %s", key.c_str());
49  }
50  return createTonemapReinhard(gamma, intensity, light_adapt, color_adapt);
51 }
52 }
53 
61 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
62 {
63  // Check the number of arguments
64  nargchk(nrhs>=2 && nlhs<=1);
65 
66  // Argument vector
67  vector<MxArray> rhs(prhs, prhs+nrhs);
68  int id = rhs[0].toInt();
69  string method(rhs[1].toString());
70 
71  // Constructor is called. Create a new object from argument
72  if (method == "new") {
73  nargchk(nrhs>=2 && nlhs<=1);
74  obj_[++last_id] = create_TonemapReinhard(
75  rhs.begin() + 2, rhs.end());
76  plhs[0] = MxArray(last_id);
77  return;
78  }
79 
80  // Big operation switch
81  Ptr<TonemapReinhard> obj = obj_[id];
82  if (method == "delete") {
83  nargchk(nrhs==2 && nlhs==0);
84  obj_.erase(id);
85  }
86  else if (method == "clear") {
87  nargchk(nrhs==2 && nlhs==0);
88  obj->clear();
89  }
90  else if (method == "load") {
91  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
92  string objname;
93  bool loadFromString = false;
94  for (int i=3; i<nrhs; i+=2) {
95  string key(rhs[i].toString());
96  if (key == "ObjName")
97  objname = rhs[i+1].toString();
98  else if (key == "FromString")
99  loadFromString = rhs[i+1].toBool();
100  else
101  mexErrMsgIdAndTxt("mexopencv:error",
102  "Unrecognized option %s", key.c_str());
103  }
104  /*
105  obj_[id] = (loadFromString ?
106  Algorithm::loadFromString<TonemapReinhard>(rhs[2].toString(), objname) :
107  Algorithm::load<TonemapReinhard>(rhs[2].toString(), objname));
108  */
110  // HACK: workaround for missing TonemapReinhard::create()
111  FileStorage fs(rhs[2].toString(), FileStorage::READ +
112  (loadFromString ? FileStorage::MEMORY : 0));
113  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
114  if (obj.empty())
115  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
116  //*/
117  }
118  else if (method == "save") {
119  nargchk(nrhs==3 && nlhs==0);
120  obj->save(rhs[2].toString());
121  }
122  else if (method == "empty") {
123  nargchk(nrhs==2 && nlhs<=1);
124  plhs[0] = MxArray(obj->empty());
125  }
126  else if (method == "getDefaultName") {
127  nargchk(nrhs==2 && nlhs<=1);
128  plhs[0] = MxArray(obj->getDefaultName());
129  }
130  else if (method == "process") {
131  nargchk(nrhs==3 && nlhs<=1);
132  Mat src(rhs[2].toMat(CV_32F)), dst;
133  obj->process(src, dst);
134  plhs[0] = MxArray(dst);
135  }
136  else if (method == "get") {
137  nargchk(nrhs==3 && nlhs<=1);
138  string prop(rhs[2].toString());
139  if (prop == "Gamma")
140  plhs[0] = MxArray(obj->getGamma());
141  else if (prop == "Intensity")
142  plhs[0] = MxArray(obj->getIntensity());
143  else if (prop == "LightAdaptation")
144  plhs[0] = MxArray(obj->getLightAdaptation());
145  else if (prop == "ColorAdaptation")
146  plhs[0] = MxArray(obj->getColorAdaptation());
147  else
148  mexErrMsgIdAndTxt("mexopencv:error",
149  "Unrecognized property %s", prop.c_str());
150  }
151  else if (method == "set") {
152  nargchk(nrhs==4 && nlhs==0);
153  string prop(rhs[2].toString());
154  if (prop == "Gamma")
155  obj->setGamma(rhs[3].toFloat());
156  else if (prop == "Intensity")
157  obj->setIntensity(rhs[3].toFloat());
158  else if (prop == "LightAdaptation")
159  obj->setLightAdaptation(rhs[3].toFloat());
160  else if (prop == "ColorAdaptation")
161  obj->setColorAdaptation(rhs[3].toFloat());
162  else
163  mexErrMsgIdAndTxt("mexopencv:error",
164  "Unrecognized property %s", prop.c_str());
165  }
166  else
167  mexErrMsgIdAndTxt("mexopencv:error",
168  "Unrecognized operation %s", method.c_str());
169 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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.