mexopencv  3.4.1
MEX interface for OpenCV library
MergeRobertson_.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 }
20 
28 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
29 {
30  // Check the number of arguments
31  nargchk(nrhs>=2 && nlhs<=1);
32 
33  // Argument vector
34  vector<MxArray> rhs(prhs, prhs+nrhs);
35  int id = rhs[0].toInt();
36  string method(rhs[1].toString());
37 
38  // Constructor is called. Create a new object from argument
39  if (method == "new") {
40  nargchk(nrhs==2 && nlhs<=1);
42  plhs[0] = MxArray(last_id);
43  mexLock();
44  return;
45  }
46 
47  // Big operation switch
48  Ptr<MergeRobertson> obj = obj_[id];
49  if (obj.empty())
50  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
51  if (method == "delete") {
52  nargchk(nrhs==2 && nlhs==0);
53  obj_.erase(id);
54  mexUnlock();
55  }
56  else if (method == "clear") {
57  nargchk(nrhs==2 && nlhs==0);
58  obj->clear();
59  }
60  else if (method == "load") {
61  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
62  string objname;
63  bool loadFromString = false;
64  for (int i=3; i<nrhs; i+=2) {
65  string key(rhs[i].toString());
66  if (key == "ObjName")
67  objname = rhs[i+1].toString();
68  else if (key == "FromString")
69  loadFromString = rhs[i+1].toBool();
70  else
71  mexErrMsgIdAndTxt("mexopencv:error",
72  "Unrecognized option %s", key.c_str());
73  }
74  /*
75  obj_[id] = (loadFromString ?
76  Algorithm::loadFromString<MergeRobertson>(rhs[2].toString(), objname) :
77  Algorithm::load<MergeRobertson>(rhs[2].toString(), objname));
78  */
80  // HACK: workaround for missing MergeRobertson::create()
81  FileStorage fs(rhs[2].toString(), FileStorage::READ +
82  (loadFromString ? FileStorage::MEMORY : 0));
83  if (!fs.isOpened())
84  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
85  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
86  if (fn.empty())
87  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
88  obj->read(fn);
89  //*/
90  }
91  else if (method == "save") {
92  nargchk(nrhs==3 && nlhs==0);
93  obj->save(rhs[2].toString());
94  }
95  else if (method == "empty") {
96  nargchk(nrhs==2 && nlhs<=1);
97  plhs[0] = MxArray(obj->empty());
98  }
99  else if (method == "getDefaultName") {
100  nargchk(nrhs==2 && nlhs<=1);
101  plhs[0] = MxArray(obj->getDefaultName());
102  }
103  else if (method == "process") {
104  nargchk((nrhs==4 || nrhs==5) && nlhs<=1);
105  vector<Mat> src;
106  {
107  vector<MxArray> arr(rhs[2].toVector<MxArray>());
108  src.reserve(arr.size());
109  for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it)
110  src.push_back(it->toMat(CV_8U));
111  }
112  Mat times(rhs[3].toMat(CV_32F)), dst;
113  if (nrhs == 5) {
114  Mat response(rhs[4].toMat(CV_32F));
115  obj->process(src, dst, times, response);
116  }
117  else
118  obj->process(src, dst, times);
119  plhs[0] = MxArray(dst);
120  }
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized operation %s", method.c_str());
124 }
T empty(T... args)
virtual void process(InputArrayOfArrays src, OutputArray dst, InputArray times, InputArray response)=0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
Ptr< MergeRobertson > createMergeRobertson()
#define CV_8U
map< int, Ptr< MergeRobertson > > obj_
Object container.
STL namespace.
T end(T... args)
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
T push_back(T... args)
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
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
int last_id
Last object id to allocate.
cv::Mat toMat() const
T reserve(T... args)