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