mexopencv  0.1
mex interface for opencv library
TonemapMantiuk_.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<TonemapMantiuk> > obj_;
19 
25 Ptr<TonemapMantiuk> create_TonemapMantiuk(
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 scale = 0.7f;
33  float saturation = 1.0f;
34  for (; first != last; first += 2) {
35  string key(first->toString());
36  const MxArray& val = *(first + 1);
37  if (key == "Gamma")
38  gamma = val.toFloat();
39  else if (key == "Scale")
40  scale = val.toFloat();
41  else if (key == "Saturation")
42  saturation = val.toFloat();
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47  return createTonemapMantiuk(gamma, scale, saturation);
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);
71  obj_[++last_id] = create_TonemapMantiuk(
72  rhs.begin() + 2, rhs.end());
73  plhs[0] = MxArray(last_id);
74  return;
75  }
76 
77  // Big operation switch
78  Ptr<TonemapMantiuk> obj = obj_[id];
79  if (method == "delete") {
80  nargchk(nrhs==2 && nlhs==0);
81  obj_.erase(id);
82  }
83  else if (method == "clear") {
84  nargchk(nrhs==2 && nlhs==0);
85  obj->clear();
86  }
87  else if (method == "load") {
88  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
89  string objname;
90  bool loadFromString = false;
91  for (int i=3; i<nrhs; i+=2) {
92  string key(rhs[i].toString());
93  if (key == "ObjName")
94  objname = rhs[i+1].toString();
95  else if (key == "FromString")
96  loadFromString = rhs[i+1].toBool();
97  else
98  mexErrMsgIdAndTxt("mexopencv:error",
99  "Unrecognized option %s", key.c_str());
100  }
101  /*
102  obj_[id] = (loadFromString ?
103  Algorithm::loadFromString<TonemapMantiuk>(rhs[2].toString(), objname) :
104  Algorithm::load<TonemapMantiuk>(rhs[2].toString(), objname));
105  */
107  // HACK: workaround for missing TonemapMantiuk::create()
108  FileStorage fs(rhs[2].toString(), FileStorage::READ +
109  (loadFromString ? FileStorage::MEMORY : 0));
110  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
111  if (obj.empty())
112  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
113  //*/
114  }
115  else if (method == "save") {
116  nargchk(nrhs==3 && nlhs==0);
117  obj->save(rhs[2].toString());
118  }
119  else if (method == "empty") {
120  nargchk(nrhs==2 && nlhs<=1);
121  plhs[0] = MxArray(obj->empty());
122  }
123  else if (method == "getDefaultName") {
124  nargchk(nrhs==2 && nlhs<=1);
125  plhs[0] = MxArray(obj->getDefaultName());
126  }
127  else if (method == "process") {
128  nargchk(nrhs==3 && nlhs<=1);
129  Mat src(rhs[2].toMat(CV_32F)), dst;
130  obj->process(src, dst);
131  plhs[0] = MxArray(dst);
132  }
133  else if (method == "get") {
134  nargchk(nrhs==3 && nlhs<=1);
135  string prop(rhs[2].toString());
136  if (prop == "Gamma")
137  plhs[0] = MxArray(obj->getGamma());
138  else if (prop == "Scale")
139  plhs[0] = MxArray(obj->getScale());
140  else if (prop == "Saturation")
141  plhs[0] = MxArray(obj->getSaturation());
142  else
143  mexErrMsgIdAndTxt("mexopencv:error",
144  "Unrecognized property %s", prop.c_str());
145  }
146  else if (method == "set") {
147  nargchk(nrhs==4 && nlhs==0);
148  string prop(rhs[2].toString());
149  if (prop == "Gamma")
150  obj->setGamma(rhs[3].toFloat());
151  else if (prop == "Scale")
152  obj->setScale(rhs[3].toFloat());
153  else if (prop == "Saturation")
154  obj->setSaturation(rhs[3].toFloat());
155  else
156  mexErrMsgIdAndTxt("mexopencv:error",
157  "Unrecognized property %s", prop.c_str());
158  }
159  else
160  mexErrMsgIdAndTxt("mexopencv:error",
161  "Unrecognized operation %s", method.c_str());
162 }
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.