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