mexopencv  3.4.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;
19 
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);
75  rhs.begin() + 2, rhs.end());
76  plhs[0] = MxArray(last_id);
77  mexLock();
78  return;
79  }
80 
81  // Big operation switch
82  Ptr<TonemapReinhard> obj = obj_[id];
83  if (obj.empty())
84  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
85  if (method == "delete") {
86  nargchk(nrhs==2 && nlhs==0);
87  obj_.erase(id);
88  mexUnlock();
89  }
90  else if (method == "clear") {
91  nargchk(nrhs==2 && nlhs==0);
92  obj->clear();
93  }
94  else if (method == "load") {
95  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
96  string objname;
97  bool loadFromString = false;
98  for (int i=3; i<nrhs; i+=2) {
99  string key(rhs[i].toString());
100  if (key == "ObjName")
101  objname = rhs[i+1].toString();
102  else if (key == "FromString")
103  loadFromString = rhs[i+1].toBool();
104  else
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "Unrecognized option %s", key.c_str());
107  }
108  /*
109  obj_[id] = (loadFromString ?
110  Algorithm::loadFromString<TonemapReinhard>(rhs[2].toString(), objname) :
111  Algorithm::load<TonemapReinhard>(rhs[2].toString(), objname));
112  */
114  // HACK: workaround for missing TonemapReinhard::create()
115  FileStorage fs(rhs[2].toString(), FileStorage::READ +
116  (loadFromString ? FileStorage::MEMORY : 0));
117  if (!fs.isOpened())
118  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
119  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
120  if (fn.empty())
121  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
122  obj->read(fn);
123  //*/
124  }
125  else if (method == "save") {
126  nargchk(nrhs==3 && nlhs==0);
127  obj->save(rhs[2].toString());
128  }
129  else if (method == "empty") {
130  nargchk(nrhs==2 && nlhs<=1);
131  plhs[0] = MxArray(obj->empty());
132  }
133  else if (method == "getDefaultName") {
134  nargchk(nrhs==2 && nlhs<=1);
135  plhs[0] = MxArray(obj->getDefaultName());
136  }
137  else if (method == "process") {
138  nargchk(nrhs==3 && nlhs<=1);
139  Mat src(rhs[2].toMat(CV_32F)), dst;
140  obj->process(src, dst);
141  plhs[0] = MxArray(dst);
142  }
143  else if (method == "get") {
144  nargchk(nrhs==3 && nlhs<=1);
145  string prop(rhs[2].toString());
146  if (prop == "Gamma")
147  plhs[0] = MxArray(obj->getGamma());
148  else if (prop == "Intensity")
149  plhs[0] = MxArray(obj->getIntensity());
150  else if (prop == "LightAdaptation")
151  plhs[0] = MxArray(obj->getLightAdaptation());
152  else if (prop == "ColorAdaptation")
153  plhs[0] = MxArray(obj->getColorAdaptation());
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized property %s", prop.c_str());
157  }
158  else if (method == "set") {
159  nargchk(nrhs==4 && nlhs==0);
160  string prop(rhs[2].toString());
161  if (prop == "Gamma")
162  obj->setGamma(rhs[3].toFloat());
163  else if (prop == "Intensity")
164  obj->setIntensity(rhs[3].toFloat());
165  else if (prop == "LightAdaptation")
166  obj->setLightAdaptation(rhs[3].toFloat());
167  else if (prop == "ColorAdaptation")
168  obj->setColorAdaptation(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 }
virtual float getGamma() const=0
virtual float getColorAdaptation() const=0
T empty(T... args)
T distance(T... args)
int last_id
Last object id to allocate.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual float getIntensity() const=0
map< int, Ptr< TonemapReinhard > > obj_
Object container.
STL namespace.
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void clear()
#define CV_32F
virtual void read(const FileNode &fn)
Ptr< TonemapReinhard > createTonemapReinhard(float gamma=1.0f, float intensity=0.0f, float light_adapt=1.0f, float color_adapt=0.0f)
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...
virtual void setGamma(float gamma)=0
float toFloat() const
Convert MxArray to float.
Definition: MxArray.cpp:503
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
virtual float getLightAdaptation() const=0
FileNode getFirstTopLevelNode() const
virtual void setColorAdaptation(float color_adapt)=0
STL class.
bool empty() const
virtual void setIntensity(float intensity)=0
virtual String getDefaultName() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
virtual void setLightAdaptation(float light_adapt)=0
Ptr< TonemapReinhard > create_TonemapReinhard(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of TonemapReinhard using options in arguments.
virtual void process(InputArray src, OutputArray dst)=0
cv::Mat toMat() const