mexopencv  3.4.1
MEX interface for OpenCV library
LineSegmentDetector_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/imgproc.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 // Persistent objects
14 namespace {
16 int last_id = 0;
19 
22  ("None", cv::LSD_REFINE_NONE) // No refinement applied.
23  ("Standard", cv::LSD_REFINE_STD) // Standard refinement is applied.
24  ("Advanced", cv::LSD_REFINE_ADV); // Advanced refinement.
25 }
26 
34 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
35 {
36  // Check the number of arguments
37  nargchk(nrhs>=2 && nlhs<=4);
38 
39  // Arguments vector
40  vector<MxArray> rhs(prhs, prhs+nrhs);
41  int id = rhs[0].toInt();
42  string method(rhs[1].toString());
43 
44  // Constructor is called. Create a new object from argument
45  if (method == "new") {
46  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
47  int refine = cv::LSD_REFINE_STD;
48  double scale = 0.8;
49  double sigma_scale = 0.6;
50  double quant = 2.0;
51  double ang_th = 22.5;
52  double log_eps = 0;
53  double density_th = 0.7;
54  int n_bins = 1024;
55  for (int i=2; i<nrhs; i+=2) {
56  string key(rhs[i].toString());
57  if (key == "Refine")
58  refine = LineSegmentDetectorModesMap[rhs[i+1].toString()];
59  else if (key == "Scale")
60  scale = rhs[i+1].toDouble();
61  else if (key == "SigmaScale")
62  sigma_scale = rhs[i+1].toDouble();
63  else if (key == "QuantError")
64  quant = rhs[i+1].toDouble();
65  else if (key == "AngleTol")
66  ang_th = rhs[i+1].toDouble();
67  else if (key == "DetectionThreshold")
68  log_eps = rhs[i+1].toDouble();
69  else if (key == "MinDensity")
70  density_th = rhs[i+1].toDouble();
71  else if (key == "NBins")
72  n_bins = rhs[i+1].toInt();
73  else
74  mexErrMsgIdAndTxt("mexopencv:error",
75  "Unrecognized option %s", key.c_str());
76  }
77  obj_[++last_id] = createLineSegmentDetector(refine, scale,
78  sigma_scale, quant, ang_th, log_eps, density_th, n_bins);
79  plhs[0] = MxArray(last_id);
80  mexLock();
81  return;
82  }
83 
84  // Big operation switch
86  if (obj.empty())
87  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
88  if (method == "delete") {
89  nargchk(nrhs==2 && nlhs==0);
90  obj_.erase(id);
91  mexUnlock();
92  }
93  else if (method == "clear") {
94  nargchk(nrhs==2 && nlhs==0);
95  obj->clear();
96  }
97  else if (method == "load") {
98  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
99  string objname;
100  bool loadFromString = false;
101  for (int i=3; i<nrhs; i+=2) {
102  string key(rhs[i].toString());
103  if (key == "ObjName")
104  objname = rhs[i+1].toString();
105  else if (key == "FromString")
106  loadFromString = rhs[i+1].toBool();
107  else
108  mexErrMsgIdAndTxt("mexopencv:error",
109  "Unrecognized option %s", key.c_str());
110  }
111  /*
112  obj_[id] = (loadFromString ?
113  Algorithm::loadFromString<LineSegmentDetector>(rhs[2].toString(), objname) :
114  Algorithm::load<LineSegmentDetector>(rhs[2].toString(), objname));
115  */
117  // HACK: workaround for missing LineSegmentDetector::create()
118  FileStorage fs(rhs[2].toString(), FileStorage::READ +
119  (loadFromString ? FileStorage::MEMORY : 0));
120  if (!fs.isOpened())
121  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
122  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
123  if (fn.empty())
124  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
125  obj->read(fn);
126  //*/
127  }
128  else if (method == "save") {
129  nargchk(nrhs==3 && nlhs==0);
130  obj->save(rhs[2].toString());
131  }
132  else if (method == "empty") {
133  nargchk(nrhs==2 && nlhs<=1);
134  plhs[0] = MxArray(obj->empty());
135  }
136  else if (method == "getDefaultName") {
137  nargchk(nrhs==2 && nlhs<=1);
138  plhs[0] = MxArray(obj->getDefaultName());
139  }
140  else if (method == "detect") {
141  nargchk(nrhs==3 && nlhs<=4);
142  Mat image(rhs[2].toMat(CV_8U)), width, prec, nfa;
143  vector<Vec4f> lines;
144  obj->detect(image, lines,
145  (nlhs>1 ? width : noArray()),
146  (nlhs>2 ? prec : noArray()),
147  (nlhs>3 ? nfa : noArray()));
148  plhs[0] = MxArray(lines);
149  if (nlhs>1)
150  plhs[1] = MxArray(width);
151  if (nlhs>2)
152  plhs[2] = MxArray(prec);
153  if (nlhs>3)
154  plhs[3] = MxArray(nfa);
155  }
156  else if (method == "drawSegments") {
157  nargchk(nrhs==4 && nlhs<=1);
158  Mat image(rhs[2].toMat());
159  //vector<Vec4f> lines(MxArrayToVectorVec<float,4>(rhs[3]));
160  vector<Vec4f> lines(rhs[3].toVector<Vec4f>());
161  obj->drawSegments(image, lines);
162  plhs[0] = MxArray(image);
163  }
164  else if (method == "compareSegments") {
165  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=2);
166  Size size(rhs[2].toSize());
167  Mat image;
168  for (int i=5; i<nrhs; i+=2) {
169  string key(rhs[i].toString());
170  if (key == "Image")
171  image = rhs[i+1].toMat();
172  else
173  mexErrMsgIdAndTxt("mexopencv:error",
174  "Unrecognized option %s", key.c_str());
175  }
176  if (image.empty()) {
177  image.create(size, CV_8UC3);
178  image.setTo(Scalar::all(0));
179  }
180  //vector<Vec4f> lines1(MxArrayToVectorVec<float,4>(rhs[3])),
181  // lines2(MxArrayToVectorVec<float,4>(rhs[4]));
182  vector<Vec4f> lines1(rhs[3].toVector<Vec4f>()),
183  lines2(rhs[4].toVector<Vec4f>());
184  int count = obj->compareSegments(size, lines1, lines2, image);
185  plhs[0] = MxArray(image);
186  if (nlhs>1)
187  plhs[1] = MxArray(count);
188  }
189  else
190  mexErrMsgIdAndTxt("mexopencv:error",
191  "Unrecognized operation %s", method.c_str());
192 }
T empty(T... args)
virtual int compareSegments(const Size &size, InputArray lines1, InputArray lines2, InputOutputArray _image=noArray())=0
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
#define CV_8U
LSD_REFINE_ADV
virtual void drawSegments(InputOutputArray _image, InputArray lines)=0
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
Ptr< LineSegmentDetector > createLineSegmentDetector(int _refine=LSD_REFINE_STD, double _scale=0.8, double _sigma_scale=0.6, double _quant=2.0, double _ang_th=22.5, double _log_eps=0, double _density_th=0.7, int _n_bins=1024)
virtual void clear()
virtual void read(const FileNode &fn)
#define CV_8UC3
InputOutputArray noArray()
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...
const ConstMap< string, int > LineSegmentDetectorModesMap
Line Segment Detector modes for option processing.
LSD_REFINE_STD
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
T count(T... args)
FileNode getFirstTopLevelNode() const
STL class.
bool empty() const
virtual String getDefaultName() const
LSD_REFINE_NONE
Global constant definitions.
virtual void detect(InputArray _image, OutputArray _lines, OutputArray width=noArray(), OutputArray prec=noArray(), OutputArray nfa=noArray())=0
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
void create(int rows, int cols, int type)
Mat & setTo(InputArray value, InputArray mask=noArray())
virtual void save(const String &filename) const
virtual bool empty() const
static Scalar_< double > all(double v0)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
map< int, Ptr< LineSegmentDetector > > obj_
Object container.
bool empty() const
cv::Mat toMat() const
size_t size() const