mexopencv  3.4.1
MEX interface for OpenCV library
LearningBasedWB_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/xphoto.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::xphoto;
13 
14 namespace {
15 // Persistent objects
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Argument vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // constructor call
40  if (method == "new") {
41  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42  string path_to_model;
43  for (int i=2; i<nrhs; i+=2) {
44  string key(rhs[i].toString());
45  if (key == "PathToModel")
46  path_to_model = rhs[i+1].toString();
47  else
48  mexErrMsgIdAndTxt("mexopencv:error",
49  "Unrecognized option %s", key.c_str());
50  }
51  obj_[++last_id] = createLearningBasedWB(path_to_model);
52  plhs[0] = MxArray(last_id);
53  mexLock();
54  return;
55  }
56 
57  // Big operation switch
58  Ptr<LearningBasedWB> obj = obj_[id];
59  if (obj.empty())
60  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
61  if (method == "delete") {
62  nargchk(nrhs==2 && nlhs==0);
63  obj_.erase(id);
64  mexUnlock();
65  }
66  else if (method == "clear") {
67  nargchk(nrhs==2 && nlhs==0);
68  obj->clear();
69  }
70  else if (method == "save") {
71  nargchk(nrhs==3 && nlhs==0);
72  obj->save(rhs[2].toString());
73  }
74  else if (method == "load") {
75  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
76  string objname;
77  bool loadFromString = false;
78  for (int i=3; i<nrhs; i+=2) {
79  string key(rhs[i].toString());
80  if (key == "ObjName")
81  objname = rhs[i+1].toString();
82  else if (key == "FromString")
83  loadFromString = rhs[i+1].toBool();
84  else
85  mexErrMsgIdAndTxt("mexopencv:error",
86  "Unrecognized option %s", key.c_str());
87  }
88  /*
89  obj_[id] = (loadFromString ?
90  Algorithm::loadFromString<LearningBasedWB>(rhs[2].toString(), objname) :
91  Algorithm::load<LearningBasedWB>(rhs[2].toString(), objname));
92  */
94  // HACK: workaround for missing LearningBasedWB::create()
95  FileStorage fs(rhs[2].toString(), FileStorage::READ +
96  (loadFromString ? FileStorage::MEMORY : 0));
97  if (!fs.isOpened())
98  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
99  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
100  if (fn.empty())
101  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
102  obj->read(fn);
103  //*/
104  }
105  else if (method == "empty") {
106  nargchk(nrhs==2 && nlhs<=1);
107  plhs[0] = MxArray(obj->empty());
108  }
109  else if (method == "getDefaultName") {
110  nargchk(nrhs==2 && nlhs<=1);
111  plhs[0] = MxArray(obj->getDefaultName());
112  }
113  else if (method == "balanceWhite") {
114  nargchk(nrhs==3 && nlhs<=1);
115  Mat src(rhs[2].toMat(rhs[2].isUint16() ? CV_16U : CV_8U)),
116  dst;
117  obj->balanceWhite(src, dst);
118  plhs[0] = MxArray(dst);
119  }
120  else if (method == "extractSimpleFeatures") {
121  nargchk(nrhs==3 && nlhs<=1);
122  Mat src(rhs[2].toMat(rhs[2].isUint16() ? CV_16U : CV_8U)),
123  dst;
124  obj->extractSimpleFeatures(src, dst);
125  plhs[0] = MxArray(dst);
126  }
127  else if (method == "get") {
128  nargchk(nrhs==3 && nlhs<=1);
129  string prop(rhs[2].toString());
130  if (prop == "RangeMaxVal")
131  plhs[0] = MxArray(obj->getRangeMaxVal());
132  else if (prop == "SaturationThreshold")
133  plhs[0] = MxArray(obj->getSaturationThreshold());
134  else if (prop == "HistBinNum")
135  plhs[0] = MxArray(obj->getHistBinNum());
136  else
137  mexErrMsgIdAndTxt("mexopencv:error",
138  "Unrecognized property %s", prop.c_str());
139  }
140  else if (method == "set") {
141  nargchk(nrhs==4 && nlhs==0);
142  string prop(rhs[2].toString());
143  if (prop == "RangeMaxVal")
144  obj->setRangeMaxVal(rhs[3].toInt());
145  else if (prop == "SaturationThreshold")
146  obj->setSaturationThreshold(rhs[3].toFloat());
147  else if (prop == "HistBinNum")
148  obj->setHistBinNum(rhs[3].toInt());
149  else
150  mexErrMsgIdAndTxt("mexopencv:error",
151  "Unrecognized property %s", prop.c_str());
152  }
153  else
154  mexErrMsgIdAndTxt("mexopencv:error",
155  "Unrecognized operation %s", method.c_str());
156 }
virtual void setSaturationThreshold(float val)=0
T empty(T... args)
virtual float getSaturationThreshold() const=0
virtual void setRangeMaxVal(int val)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual int getHistBinNum() const=0
#define CV_8U
map< int, Ptr< LearningBasedWB > > obj_
Object container.
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setHistBinNum(int val)=0
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...
virtual int getRangeMaxVal() const=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
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
#define CV_16U
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void extractSimpleFeatures(InputArray src, OutputArray dst)=0
virtual void save(const String &filename) const
virtual bool empty() const
Ptr< LearningBasedWB > createLearningBasedWB(const String &path_to_model=String())
int last_id
Last object id to allocate.
virtual void balanceWhite(InputArray src, OutputArray dst)=0
cv::Mat toMat() const