mexopencv  3.4.1
MEX interface for OpenCV library
Tonemap_.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  for (; first != last; first += 2) {
33  string key(first->toString());
34  const MxArray& val = *(first + 1);
35  if (key == "Gamma")
36  gamma = val.toFloat();
37  else
38  mexErrMsgIdAndTxt("mexopencv:error",
39  "Unrecognized option %s", key.c_str());
40  }
41  return createTonemap(gamma);
42 }
43 }
44 
52 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
53 {
54  // Check the number of arguments
55  nargchk(nrhs>=2 && nlhs<=1);
56 
57  // Argument vector
58  vector<MxArray> rhs(prhs, prhs+nrhs);
59  int id = rhs[0].toInt();
60  string method(rhs[1].toString());
61 
62  // Constructor is called. Create a new object from argument
63  if (method == "new") {
64  nargchk(nrhs>=2 && nlhs<=1);
66  rhs.begin() + 2, rhs.end());
67  plhs[0] = MxArray(last_id);
68  mexLock();
69  return;
70  }
71 
72  // Big operation switch
73  Ptr<Tonemap> obj = obj_[id];
74  if (obj.empty())
75  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
76  if (method == "delete") {
77  nargchk(nrhs==2 && nlhs==0);
78  obj_.erase(id);
79  mexUnlock();
80  }
81  else if (method == "clear") {
82  nargchk(nrhs==2 && nlhs==0);
83  obj->clear();
84  }
85  else if (method == "load") {
86  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
87  string objname;
88  bool loadFromString = false;
89  for (int i=3; i<nrhs; i+=2) {
90  string key(rhs[i].toString());
91  if (key == "ObjName")
92  objname = rhs[i+1].toString();
93  else if (key == "FromString")
94  loadFromString = rhs[i+1].toBool();
95  else
96  mexErrMsgIdAndTxt("mexopencv:error",
97  "Unrecognized option %s", key.c_str());
98  }
99  /*
100  obj_[id] = (loadFromString ?
101  Algorithm::loadFromString<Tonemap>(rhs[2].toString(), objname) :
102  Algorithm::load<Tonemap>(rhs[2].toString(), objname));
103  */
105  // HACK: workaround for missing Tonemap::create()
106  FileStorage fs(rhs[2].toString(), FileStorage::READ +
107  (loadFromString ? FileStorage::MEMORY : 0));
108  if (!fs.isOpened())
109  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
110  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
111  if (fn.empty())
112  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
113  obj->read(fn);
114  //*/
115  }
116  else if (method == "save") {
117  nargchk(nrhs==3 && nlhs==0);
118  obj->save(rhs[2].toString());
119  }
120  else if (method == "empty") {
121  nargchk(nrhs==2 && nlhs<=1);
122  plhs[0] = MxArray(obj->empty());
123  }
124  else if (method == "getDefaultName") {
125  nargchk(nrhs==2 && nlhs<=1);
126  plhs[0] = MxArray(obj->getDefaultName());
127  }
128  else if (method == "process") {
129  nargchk(nrhs==3 && nlhs<=1);
130  Mat src(rhs[2].toMat(CV_32F)), dst;
131  obj->process(src, dst);
132  plhs[0] = MxArray(dst);
133  }
134  else if (method == "get") {
135  nargchk(nrhs==3 && nlhs<=1);
136  string prop(rhs[2].toString());
137  if (prop == "Gamma")
138  plhs[0] = MxArray(obj->getGamma());
139  else
140  mexErrMsgIdAndTxt("mexopencv:error",
141  "Unrecognized property %s", prop.c_str());
142  }
143  else if (method == "set") {
144  nargchk(nrhs==4 && nlhs==0);
145  string prop(rhs[2].toString());
146  if (prop == "Gamma")
147  obj->setGamma(rhs[3].toFloat());
148  else
149  mexErrMsgIdAndTxt("mexopencv:error",
150  "Unrecognized property %s", prop.c_str());
151  }
152  else
153  mexErrMsgIdAndTxt("mexopencv:error",
154  "Unrecognized operation %s", method.c_str());
155 }
virtual float getGamma() const=0
T empty(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Tonemap_.cpp:52
Ptr< Tonemap > create_Tonemap(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of Tonemap using options in arguments.
Definition: Tonemap_.cpp:25
T distance(T... args)
map< int, Ptr< Tonemap > > obj_
Object container.
Definition: Tonemap_.cpp:18
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
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()
int last_id
Last object id to allocate.
Definition: Tonemap_.cpp:16
#define CV_32F
virtual void read(const FileNode &fn)
Ptr< Tonemap > createTonemap(float gamma=1.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
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
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 process(InputArray src, OutputArray dst)=0
cv::Mat toMat() const