mexopencv  3.4.1
MEX interface for OpenCV library
MergeMertens_.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 contrast_weight = 1.0f;
32  float saturation_weight = 1.0f;
33  float exposure_weight = 0.0f;
34  for (; first != last; first += 2) {
35  string key(first->toString());
36  const MxArray& val = *(first + 1);
37  if (key == "ContrastWeight")
38  contrast_weight = val.toFloat();
39  else if (key == "SaturationWeight")
40  saturation_weight = val.toFloat();
41  else if (key == "ExposureWeight")
42  exposure_weight = val.toFloat();
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47  return createMergeMertens(contrast_weight, saturation_weight, exposure_weight);
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<MergeMertens> 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<MergeMertens>(rhs[2].toString(), objname) :
108  Algorithm::load<MergeMertens>(rhs[2].toString(), objname));
109  */
111  // HACK: workaround for missing MergeMertens::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  vector<Mat> src;
137  {
138  vector<MxArray> arr(rhs[2].toVector<MxArray>());
139  src.reserve(arr.size());
140  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
141  src.push_back(it->toMat(CV_8U));
142  }
143  Mat dst;
144  obj->process(src, dst);
145  plhs[0] = MxArray(dst);
146  }
147  else if (method == "get") {
148  nargchk(nrhs==3 && nlhs<=1);
149  string prop(rhs[2].toString());
150  if (prop == "ContrastWeight")
151  plhs[0] = MxArray(obj->getContrastWeight());
152  else if (prop == "SaturationWeight")
153  plhs[0] = MxArray(obj->getSaturationWeight());
154  else if (prop == "ExposureWeight ")
155  plhs[0] = MxArray(obj->getExposureWeight());
156  else
157  mexErrMsgIdAndTxt("mexopencv:error",
158  "Unrecognized property %s", prop.c_str());
159  }
160  else if (method == "set") {
161  nargchk(nrhs==4 && nlhs==0);
162  string prop(rhs[2].toString());
163  if (prop == "ContrastWeight")
164  obj->setContrastWeight(rhs[3].toFloat());
165  else if (prop == "SaturationWeight")
166  obj->setSaturationWeight(rhs[3].toFloat());
167  else if (prop == "ExposureWeight")
168  obj->setExposureWeight(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 }
Ptr< MergeMertens > createMergeMertens(float contrast_weight=1.0f, float saturation_weight=1.0f, float exposure_weight=0.0f)
virtual void setExposureWeight(float exposure_weight)=0
T empty(T... args)
T distance(T... args)
virtual float getSaturationWeight() const=0
virtual void process(InputArrayOfArrays src, OutputArray dst, InputArray times, InputArray response)=0
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.
#define CV_8U
int last_id
Last object id to allocate.
STL namespace.
virtual void setSaturationWeight(float saturation_weight)=0
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
map< int, Ptr< MergeMertens > > obj_
Object container.
T push_back(T... args)
virtual void clear()
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...
float toFloat() const
Convert MxArray to float.
Definition: MxArray.cpp:503
Ptr< MergeMertens > create_MergeMertens(vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of MergeMertens using options in arguments.
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 getContrastWeight() const=0
FileNode getFirstTopLevelNode() const
virtual void setContrastWeight(float contrast_weiht)=0
T size(T... args)
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 float getExposureWeight() const=0
T reserve(T... args)