mexopencv  3.4.1
MEX interface for OpenCV library
FastLineDetector_.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 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 is called. Create a new object from argument
40  if (method == "new") {
41  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
42  int length_threshold = 10;
43  float distance_threshold = 1.414213562f;
44  double canny_th1 = 50.0;
45  double canny_th2 = 50.0;
46  int canny_aperture_size = 3;
47  bool do_merge = false;
48  for (int i=2; i<nrhs; i+=2) {
49  string key(rhs[i].toString());
50  if (key == "LengthThreshold")
51  length_threshold = rhs[i+1].toInt();
52  else if (key == "DistanceThreshold")
53  distance_threshold = rhs[i+1].toFloat();
54  else if (key == "CannyThreshold1")
55  canny_th1 = rhs[i+1].toDouble();
56  else if (key == "CannyThreshold2")
57  canny_th2 = rhs[i+1].toDouble();
58  else if (key == "CannyApertureSize")
59  canny_aperture_size = rhs[i+1].toInt();
60  else if (key == "DoMerge")
61  do_merge = rhs[i+1].toBool();
62  else
63  mexErrMsgIdAndTxt("mexopencv:error",
64  "Unrecognized option %s", key.c_str());
65  }
67  length_threshold, distance_threshold, canny_th1, canny_th2,
68  canny_aperture_size, do_merge);
69  plhs[0] = MxArray(last_id);
70  mexLock();
71  return;
72  }
73 
74  // Big operation switch
75  Ptr<FastLineDetector> obj = obj_[id];
76  if (obj.empty())
77  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
78  if (method == "delete") {
79  nargchk(nrhs==2 && nlhs==0);
80  obj_.erase(id);
81  mexUnlock();
82  }
83  else if (method == "clear") {
84  nargchk(nrhs==2 && nlhs==0);
85  obj->clear();
86  }
87  else if (method == "load") {
88  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
89  string objname;
90  bool loadFromString = false;
91  for (int i=3; i<nrhs; i+=2) {
92  string key(rhs[i].toString());
93  if (key == "ObjName")
94  objname = rhs[i+1].toString();
95  else if (key == "FromString")
96  loadFromString = rhs[i+1].toBool();
97  else
98  mexErrMsgIdAndTxt("mexopencv:error",
99  "Unrecognized option %s", key.c_str());
100  }
101  /*
102  obj_[id] = (loadFromString ?
103  Algorithm::loadFromString<FastLineDetector>(rhs[2].toString(), objname) :
104  Algorithm::load<FastLineDetector>(rhs[2].toString(), objname));
105  */
107  // HACK: workaround for missing FastLineDetector::create()
108  FileStorage fs(rhs[2].toString(), FileStorage::READ +
109  (loadFromString ? FileStorage::MEMORY : 0));
110  if (!fs.isOpened())
111  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
112  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
113  if (fn.empty())
114  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
115  obj->read(fn);
116  //*/
117  }
118  else if (method == "save") {
119  nargchk(nrhs==3 && nlhs==0);
120  obj->save(rhs[2].toString());
121  }
122  else if (method == "empty") {
123  nargchk(nrhs==2 && nlhs<=1);
124  plhs[0] = MxArray(obj->empty());
125  }
126  else if (method == "getDefaultName") {
127  nargchk(nrhs==2 && nlhs<=1);
128  plhs[0] = MxArray(obj->getDefaultName());
129  }
130  else if (method == "detect") {
131  nargchk(nrhs==3 && nlhs<=1);
132  Mat image(rhs[2].toMat(CV_8U));
133  vector<Vec4f> lines;
134  obj->detect(image, lines);
135  plhs[0] = MxArray(lines);
136  }
137  else if (method == "drawSegments") {
138  nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs<=1);
139  bool draw_arrow = false;
140  for (int i=4; i<nrhs; i+=2) {
141  string key(rhs[i].toString());
142  if (key == "DrawArrow")
143  draw_arrow = rhs[i+1].toBool();
144  else
145  mexErrMsgIdAndTxt("mexopencv:error",
146  "Unrecognized option %s", key.c_str());
147  }
148  Mat image(rhs[2].toMat());
149  //vector<Vec4f> lines(MxArrayToVectorVec<float,4>(rhs[3]));
150  vector<Vec4f> lines(rhs[3].toVector<Vec4f>());
151  obj->drawSegments(image, lines, draw_arrow);
152  plhs[0] = MxArray(image);
153  }
154  else
155  mexErrMsgIdAndTxt("mexopencv:error",
156  "Unrecognized operation %s", method.c_str());
157 }
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.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void detect(InputArray _image, OutputArray _lines)=0
Ptr< FastLineDetector > createFastLineDetector(int _length_threshold=10, float _distance_threshold=1.414213562f, double _canny_th1=50.0, double _canny_th2=50.0, int _canny_aperture_size=3, bool _do_merge=false)
virtual void clear()
virtual void drawSegments(InputOutputArray _image, InputArray lines, bool draw_arrow=false)=0
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.
int last_id
Last object id to allocate.
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 String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void save(const String &filename) const
virtual bool empty() const
map< int, Ptr< FastLineDetector > > obj_
Object container.
cv::Mat toMat() const