mexopencv  0.1
mex interface for opencv library
LineSegmentDetector_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 // Persistent objects
13 namespace {
15 int last_id = 0;
17 map<int,Ptr<LineSegmentDetector> > obj_;
18 
20 const ConstMap<string,int> LineSegmentDetectorModesMap = ConstMap<string,int>
21  ("None", cv::LSD_REFINE_NONE) // No refinement applied.
22  ("Standard", cv::LSD_REFINE_STD) // Standard refinement is applied.
23  ("Advanced", cv::LSD_REFINE_ADV); // Advanced refinement.
24 }
25 
33 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
34 {
35  // Check the number of arguments
36  nargchk(nrhs>=2 && nlhs<=4);
37 
38  // Arguments vector
39  vector<MxArray> rhs(prhs, prhs+nrhs);
40  int id = rhs[0].toInt();
41  string method(rhs[1].toString());
42 
43  // Constructor is called. Create a new object from argument
44  if (method == "new") {
45  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
46  int refine = cv::LSD_REFINE_STD;
47  double scale = 0.8;
48  double sigma_scale = 0.6;
49  double quant = 2.0;
50  double ang_th = 22.5;
51  double log_eps = 0;
52  double density_th = 0.7;
53  int n_bins = 1024;
54  for (int i=2; i<nrhs; i+=2) {
55  string key(rhs[i].toString());
56  if (key=="Refine")
57  refine = LineSegmentDetectorModesMap[rhs[i+1].toString()];
58  else if (key=="Scale")
59  scale = rhs[i+1].toDouble();
60  else if (key=="SigmaScale")
61  sigma_scale = rhs[i+1].toDouble();
62  else if (key=="QuantError")
63  quant = rhs[i+1].toDouble();
64  else if (key=="AngleTol")
65  ang_th = rhs[i+1].toDouble();
66  else if (key=="DetectionThreshold")
67  log_eps = rhs[i+1].toDouble();
68  else if (key=="MinDensity")
69  density_th = rhs[i+1].toDouble();
70  else if (key=="NBins")
71  n_bins = rhs[i+1].toInt();
72  else
73  mexErrMsgIdAndTxt("mexopencv:error",
74  "Unrecognized option %s", key.c_str());
75  }
76  obj_[++last_id] = createLineSegmentDetector(refine, scale,
77  sigma_scale, quant, ang_th, log_eps, density_th, n_bins);
78  plhs[0] = MxArray(last_id);
79  return;
80  }
81 
82  // Big operation switch
83  Ptr<LineSegmentDetector> obj = obj_[id];
84  if (method == "delete") {
85  nargchk(nrhs==2 && nlhs==0);
86  obj_.erase(id);
87  }
88  else if (method == "clear") {
89  nargchk(nrhs==2 && nlhs==0);
90  obj->clear();
91  }
92  else if (method == "load") {
93  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
94  string objname;
95  bool loadFromString = false;
96  for (int i=3; i<nrhs; i+=2) {
97  string key(rhs[i].toString());
98  if (key=="ObjName")
99  objname = rhs[i+1].toString();
100  else if (key=="FromString")
101  loadFromString = rhs[i+1].toBool();
102  else
103  mexErrMsgIdAndTxt("mexopencv:error",
104  "Unrecognized option %s", key.c_str());
105  }
106  /*
107  obj_[id] = (loadFromString ?
108  Algorithm::loadFromString<LineSegmentDetector>(rhs[2].toString(), objname) :
109  Algorithm::load<LineSegmentDetector>(rhs[2].toString(), objname));
110  */
112  // HACK: workaround for missing LineSegmentDetector::create()
113  FileStorage fs(rhs[2].toString(), FileStorage::READ +
114  (loadFromString ? FileStorage::MEMORY : 0));
115  obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
116  if (obj.empty())
117  mexErrMsgIdAndTxt("mexopencv:error", "Failed to load algorithm");
118  //*/
119  }
120  else if (method == "save") {
121  nargchk(nrhs==3 && nlhs==0);
122  obj->save(rhs[2].toString());
123  }
124  else if (method == "empty") {
125  nargchk(nrhs==2 && nlhs<=1);
126  plhs[0] = MxArray(obj->empty());
127  }
128  else if (method == "getDefaultName") {
129  nargchk(nrhs==2 && nlhs<=1);
130  plhs[0] = MxArray(obj->getDefaultName());
131  }
132  else if (method == "detect") {
133  nargchk(nrhs==3 && nlhs<=4);
134  Mat image(rhs[2].toMat(CV_8U)), width, prec, nfa;
135  vector<Vec4f> lines;
136  obj->detect(image, lines,
137  (nlhs>1 ? width : noArray()),
138  (nlhs>2 ? prec : noArray()),
139  (nlhs>3 ? nfa : noArray()));
140  plhs[0] = MxArray(lines);
141  if (nlhs>1)
142  plhs[1] = MxArray(width);
143  if (nlhs>2)
144  plhs[2] = MxArray(prec);
145  if (nlhs>3)
146  plhs[3] = MxArray(nfa);
147  }
148  else if (method == "drawSegments") {
149  nargchk(nrhs==4 && nlhs<=1);
150  Mat image(rhs[2].toMat());
151  //vector<Vec4f> lines(MxArrayToVectorVec<float,4>(rhs[3]));
152  vector<Vec4f> lines(rhs[3].toVector<Vec4f>());
153  obj->drawSegments(image, lines);
154  plhs[0] = MxArray(image);
155  }
156  else if (method == "compareSegments") {
157  nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=2);
158  Size size(rhs[2].toSize());
159  Mat image;
160  for (int i=5; i<nrhs; i+=2) {
161  string key(rhs[i].toString());
162  if (key=="Image")
163  image = rhs[i+1].toMat();
164  else
165  mexErrMsgIdAndTxt("mexopencv:error",
166  "Unrecognized option %s", key.c_str());
167  }
168  if (image.empty()) {
169  image.create(size, CV_8UC3);
170  image.setTo(Scalar::all(0));
171  }
172  //vector<Vec4f> lines1(MxArrayToVectorVec<float,4>(rhs[3])),
173  // lines2(MxArrayToVectorVec<float,4>(rhs[4]));
174  vector<Vec4f> lines1(rhs[3].toVector<Vec4f>()),
175  lines2(rhs[4].toVector<Vec4f>());
176  int count = obj->compareSegments(size, lines1, lines2, image);
177  plhs[0] = MxArray(image);
178  if (nlhs>1)
179  plhs[1] = MxArray(count);
180  }
181  else
182  mexErrMsgIdAndTxt("mexopencv:error",
183  "Unrecognized operation %s", method.c_str());
184 }
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927