mexopencv  3.4.1
MEX interface for OpenCV library
RetinaFastToneMapping_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::bioinspired;
13 
14 // Persistent objects
15 namespace {
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 is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs==3 && nlhs<=1);
42  obj_[++last_id] = RetinaFastToneMapping::create(rhs[2].toSize());
43  plhs[0] = MxArray(last_id);
44  mexLock();
45  return;
46  }
47 
48  // Big operation switch
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 == "load") {
62  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
63  string objname;
64  bool loadFromString = false;
65  for (int i=3; i<nrhs; i+=2) {
66  string key(rhs[i].toString());
67  if (key == "ObjName")
68  objname = rhs[i+1].toString();
69  else if (key == "FromString")
70  loadFromString = rhs[i+1].toBool();
71  else
72  mexErrMsgIdAndTxt("mexopencv:error",
73  "Unrecognized option %s", key.c_str());
74  }
75  /*
76  obj_[id] = (loadFromString ?
77  Algorithm::loadFromString<RetinaFastToneMapping>(rhs[2].toString(), objname) :
78  Algorithm::load<RetinaFastToneMapping>(rhs[2].toString(), objname));
79  */
81  // HACK: workaround for missing RetinaFastToneMapping::create()
82  FileStorage fs(rhs[2].toString(), FileStorage::READ +
83  (loadFromString ? FileStorage::MEMORY : 0));
84  if (!fs.isOpened())
85  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
86  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
87  if (fn.empty())
88  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
89  obj->read(fn);
90  //*/
91  }
92  else if (method == "save") {
93  nargchk(nrhs==3 && nlhs==0);
94  obj->save(rhs[2].toString());
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 == "setup") {
105  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs==0);
106  float photoreceptorsNeighborhoodRadius = 3.f;
107  float ganglioncellsNeighborhoodRadius = 1.f;
108  float meanLuminanceModulatorK = 1.f;
109  for (int i=2; i<nrhs; i+=2) {
110  string key(rhs[i].toString());
111  if (key == "PhotoreceptorsNeighborhoodRadius")
112  photoreceptorsNeighborhoodRadius = rhs[i+1].toFloat();
113  else if (key == "GanglioncellsNeighborhoodRadius")
114  ganglioncellsNeighborhoodRadius = rhs[i+1].toFloat();
115  else if (key == "MeanLuminanceModulatorK")
116  meanLuminanceModulatorK = rhs[i+1].toFloat();
117  else
118  mexErrMsgIdAndTxt("mexopencv:error",
119  "Unrecognized option %s", key.c_str());
120  }
121  obj->setup(photoreceptorsNeighborhoodRadius,
122  ganglioncellsNeighborhoodRadius, meanLuminanceModulatorK);
123  }
124  else if (method == "applyFastToneMapping") {
125  nargchk(nrhs==3 && nlhs<=1);
126  Mat inputImage(rhs[2].toMat(CV_32F)),
127  outputToneMappedImage;
128  obj->applyFastToneMapping(inputImage, outputToneMappedImage);
129  plhs[0] = MxArray(outputToneMappedImage);
130  }
131  else
132  mexErrMsgIdAndTxt("mexopencv:error",
133  "Unrecognized operation %s", method.c_str());
134 }
T empty(T... args)
virtual void setup(const float photoreceptorsNeighborhoodRadius=3.f, const float ganglioncellsNeighborhoodRadius=1.f, const float meanLuminanceModulatorK=1.f)=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.
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
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...
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
map< int, Ptr< RetinaFastToneMapping > > obj_
Object container.
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
virtual void applyFastToneMapping(InputArray inputImage, OutputArray outputToneMappedImage)=0
cv::Mat toMat() const