mexopencv  3.4.1
MEX interface for OpenCV library
EdgeAwareInterpolator_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/ximgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::ximgproc;
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==2 && nlhs<=1);
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<EdgeAwareInterpolator>(rhs[2].toString(), objname) :
78  Algorithm::load<EdgeAwareInterpolator>(rhs[2].toString(), objname));
79  */
81  // HACK: workaround for missing EdgeAwareInterpolator::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 == "interpolate") {
105  nargchk(nrhs==6 && nlhs<=1);
106  Mat from_image(rhs[2].toMat(CV_8U)),
107  to_image(rhs[4].toMat(CV_8U)),
108  dense_flow;
109  vector<Point2f> from_points(rhs[3].toVector<Point2f>()),
110  to_points(rhs[5].toVector<Point2f>());
111  obj->interpolate(from_image, from_points,
112  to_image, to_points, dense_flow);
113  plhs[0] = MxArray(dense_flow);
114  }
115  else if (method == "get") {
116  nargchk(nrhs==3 && nlhs<=1);
117  string prop(rhs[2].toString());
118  if (prop == "K")
119  plhs[0] = MxArray(obj->getK());
120  else if (prop == "Sigma")
121  plhs[0] = MxArray(obj->getSigma());
122  else if (prop == "Lambda")
123  plhs[0] = MxArray(obj->getLambda());
124  else if (prop == "UsePostProcessing")
125  plhs[0] = MxArray(obj->getUsePostProcessing());
126  else if (prop == "FGSLambda")
127  plhs[0] = MxArray(obj->getFGSLambda());
128  else if (prop == "FGSSigma")
129  plhs[0] = MxArray(obj->getFGSSigma());
130  else
131  mexErrMsgIdAndTxt("mexopencv:error",
132  "Unrecognized property %s", prop.c_str());
133  }
134  else if (method == "set") {
135  nargchk(nrhs==4 && nlhs==0);
136  string prop(rhs[2].toString());
137  if (prop == "K")
138  obj->setK(rhs[3].toInt());
139  else if (prop == "Sigma")
140  obj->setSigma(rhs[3].toFloat());
141  else if (prop == "Lambda")
142  obj->setLambda(rhs[3].toFloat());
143  else if (prop == "UsePostProcessing")
144  obj->setUsePostProcessing(rhs[3].toBool());
145  else if (prop == "FGSLambda")
146  obj->setFGSLambda(rhs[3].toFloat());
147  else if (prop == "FGSSigma")
148  obj->setFGSSigma(rhs[3].toFloat());
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 }
T empty(T... args)
virtual void interpolate(InputArray from_image, InputArray from_points, InputArray to_image, InputArray to_points, OutputArray dense_flow)=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void setFGSSigma(float _sigma)=0
#define CV_8U
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setLambda(float _lambda)=0
virtual void setFGSLambda(float _lambda)=0
map< int, Ptr< EdgeAwareInterpolator > > obj_
Object container.
virtual void clear()
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual void read(const FileNode &fn)
Ptr< EdgeAwareInterpolator > createEdgeAwareInterpolator()
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
virtual void setUsePostProcessing(bool _use_post_proc)=0
virtual String getDefaultName() const
virtual void setSigma(float _sigma)=0
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
cv::Mat toMat() const