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