mexopencv  3.4.1
MEX interface for OpenCV library
RidgeDetectionFilter_.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 && (nrhs%2)==0 && nlhs<=1);
42  int ddepth = CV_32F;
43  int dx = 1;
44  int dy = 1;
45  int ksize = 3;
46  int out_dtype = CV_8U;
47  double scale = 1;
48  double delta = 0;
49  int borderType = cv::BORDER_DEFAULT;
50  for (int i=2; i<nrhs; i+=2) {
51  string key(rhs[i].toString());
52  if (key == "DDepth")
53  ddepth = (rhs[i+1].isChar()) ?
54  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
55  else if (key == "Dx")
56  dx = rhs[i+1].toInt();
57  else if (key == "Dy")
58  dy = rhs[i+1].toInt();
59  else if (key == "KSize")
60  ksize = rhs[i+1].toInt();
61  else if (key == "OutDType")
62  out_dtype = (rhs[i+1].isChar()) ?
63  ClassNameMap[rhs[i+1].toString()] : rhs[i+1].toInt();
64  else if (key == "Scale")
65  scale = rhs[i+1].toDouble();
66  else if (key == "Delta")
67  delta = rhs[i+1].toDouble();
68  else if (key == "BorderType")
69  borderType = BorderType[rhs[i+1].toString()];
70  else
71  mexErrMsgIdAndTxt("mexopencv:error",
72  "Unrecognized option %s", key.c_str());
73  }
74  obj_[++last_id] = RidgeDetectionFilter::create(ddepth, dx, dy, ksize,
75  out_dtype, scale, delta, borderType);
76  plhs[0] = MxArray(last_id);
77  mexLock();
78  return;
79  }
80 
81  // Big operation switch
83  if (obj.empty())
84  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
85  if (method == "delete") {
86  nargchk(nrhs==2 && nlhs==0);
87  obj_.erase(id);
88  mexUnlock();
89  }
90  else if (method == "clear") {
91  nargchk(nrhs==2 && nlhs==0);
92  obj->clear();
93  }
94  else if (method == "load") {
95  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
96  string objname;
97  bool loadFromString = false;
98  for (int i=3; i<nrhs; i+=2) {
99  string key(rhs[i].toString());
100  if (key == "ObjName")
101  objname = rhs[i+1].toString();
102  else if (key == "FromString")
103  loadFromString = rhs[i+1].toBool();
104  else
105  mexErrMsgIdAndTxt("mexopencv:error",
106  "Unrecognized option %s", key.c_str());
107  }
108  obj_[id] = (loadFromString ?
109  Algorithm::loadFromString<RidgeDetectionFilter>(rhs[2].toString(), objname) :
110  Algorithm::load<RidgeDetectionFilter>(rhs[2].toString(), objname));
111  }
112  else if (method == "save") {
113  nargchk(nrhs==3 && nlhs==0);
114  obj->save(rhs[2].toString());
115  }
116  else if (method == "empty") {
117  nargchk(nrhs==2 && nlhs<=1);
118  plhs[0] = MxArray(obj->empty());
119  }
120  else if (method == "getDefaultName") {
121  nargchk(nrhs==2 && nlhs<=1);
122  plhs[0] = MxArray(obj->getDefaultName());
123  }
124  else if (method == "getRidgeFilteredImage") {
125  nargchk(nrhs==3 && nlhs<=1);
126  Mat img(rhs[2].toMat()), out;
127  obj->getRidgeFilteredImage(img, out);
128  plhs[0] = MxArray(out);
129  }
130  else
131  mexErrMsgIdAndTxt("mexopencv:error",
132  "Unrecognized operation %s", method.c_str());
133 }
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
virtual void getRidgeFilteredImage(InputArray _img, OutputArray out)=0
#define CV_8U
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void clear()
#define CV_32F
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...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
const ConstMap< std::string, int > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:66
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
STL class.
bool empty() const
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
BORDER_DEFAULT
map< int, Ptr< RidgeDetectionFilter > > obj_
Object container.
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const