mexopencv  3.4.1
MEX interface for OpenCV library
GrayworldWB_.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 && nlhs<=1);
43  plhs[0] = MxArray(last_id);
44  mexLock();
45  return;
46  }
47 
48  // Big operation switch
49  Ptr<GrayworldWB> obj = obj_[id];
50  if (obj.empty())
51  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
52  if (method == "delete") {
53  nargchk(nrhs==2 && nlhs==0);
54  obj_.erase(id);
55  mexUnlock();
56  }
57  else if (method == "clear") {
58  nargchk(nrhs==2 && nlhs==0);
59  obj->clear();
60  }
61  else if (method == "save") {
62  nargchk(nrhs==3 && nlhs==0);
63  obj->save(rhs[2].toString());
64  }
65  else if (method == "load") {
66  nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
67  string objname;
68  bool loadFromString = false;
69  for (int i=3; i<nrhs; i+=2) {
70  string key(rhs[i].toString());
71  if (key == "ObjName")
72  objname = rhs[i+1].toString();
73  else if (key == "FromString")
74  loadFromString = rhs[i+1].toBool();
75  else
76  mexErrMsgIdAndTxt("mexopencv:error",
77  "Unrecognized option %s", key.c_str());
78  }
79  /*
80  obj_[id] = (loadFromString ?
81  Algorithm::loadFromString<GrayworldWB>(rhs[2].toString(), objname) :
82  Algorithm::load<GrayworldWB>(rhs[2].toString(), objname));
83  */
85  // HACK: workaround for missing GrayworldWB::create()
86  FileStorage fs(rhs[2].toString(), FileStorage::READ +
87  (loadFromString ? FileStorage::MEMORY : 0));
88  if (!fs.isOpened())
89  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
90  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
91  if (fn.empty())
92  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
93  obj->read(fn);
94  //*/
95  }
96  else if (method == "empty") {
97  nargchk(nrhs==2 && nlhs<=1);
98  plhs[0] = MxArray(obj->empty());
99  }
100  else if (method == "getDefaultName") {
101  nargchk(nrhs==2 && nlhs<=1);
102  plhs[0] = MxArray(obj->getDefaultName());
103  }
104  else if (method == "balanceWhite") {
105  nargchk(nrhs==3 && nlhs<=1);
106  Mat src(rhs[2].toMat(rhs[2].isUint16() ? CV_16U : CV_8U)),
107  dst;
108  obj->balanceWhite(src, dst);
109  plhs[0] = MxArray(dst);
110  }
111  else if (method == "get") {
112  nargchk(nrhs==3 && nlhs<=1);
113  string prop(rhs[2].toString());
114  if (prop == "SaturationThreshold")
115  plhs[0] = MxArray(obj->getSaturationThreshold());
116  else
117  mexErrMsgIdAndTxt("mexopencv:error",
118  "Unrecognized property %s", prop.c_str());
119  }
120  else if (method == "set") {
121  nargchk(nrhs==4 && nlhs==0);
122  string prop(rhs[2].toString());
123  if (prop == "SaturationThreshold")
124  obj->setSaturationThreshold(rhs[3].toFloat());
125  else
126  mexErrMsgIdAndTxt("mexopencv:error",
127  "Unrecognized property %s", prop.c_str());
128  }
129  else
130  mexErrMsgIdAndTxt("mexopencv:error",
131  "Unrecognized operation %s", method.c_str());
132 }
T empty(T... args)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
STL namespace.
virtual bool isOpened() const
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual float getSaturationThreshold() const=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...
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
int last_id
Last object id to allocate.
FileNode getFirstTopLevelNode() const
#define CV_16U
map< int, Ptr< GrayworldWB > > obj_
Object container.
STL class.
bool empty() const
virtual String getDefaultName() const
virtual void setSaturationThreshold(float val)=0
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
Ptr< GrayworldWB > createGrayworldWB()
virtual void balanceWhite(InputArray src, OutputArray dst)=0
cv::Mat toMat() const