17 map<int,Ptr<LineSegmentDetector> > obj_;
21 (
"None", cv::LSD_REFINE_NONE)
22 (
"Standard", cv::LSD_REFINE_STD)
23 (
"Advanced", cv::LSD_REFINE_ADV);
33 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
39 vector<MxArray> rhs(prhs, prhs+nrhs);
40 int id = rhs[0].toInt();
41 string method(rhs[1].toString());
44 if (method ==
"new") {
45 nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
46 int refine = cv::LSD_REFINE_STD;
48 double sigma_scale = 0.6;
52 double density_th = 0.7;
54 for (
int i=2; i<nrhs; i+=2) {
55 string key(rhs[i].toString());
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();
73 mexErrMsgIdAndTxt(
"mexopencv:error",
74 "Unrecognized option %s", key.c_str());
76 obj_[++last_id] = createLineSegmentDetector(refine, scale,
77 sigma_scale, quant, ang_th, log_eps, density_th, n_bins);
83 Ptr<LineSegmentDetector> obj = obj_[id];
84 if (method ==
"delete") {
88 else if (method ==
"clear") {
92 else if (method ==
"load") {
93 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
95 bool loadFromString =
false;
96 for (
int i=3; i<nrhs; i+=2) {
97 string key(rhs[i].toString());
99 objname = rhs[i+1].toString();
100 else if (key==
"FromString")
101 loadFromString = rhs[i+1].toBool();
103 mexErrMsgIdAndTxt(
"mexopencv:error",
104 "Unrecognized option %s", key.c_str());
113 FileStorage fs(rhs[2].toString(), FileStorage::READ +
114 (loadFromString ? FileStorage::MEMORY : 0));
115 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
117 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
120 else if (method ==
"save") {
122 obj->save(rhs[2].toString());
124 else if (method ==
"empty") {
126 plhs[0] =
MxArray(obj->empty());
128 else if (method ==
"getDefaultName") {
130 plhs[0] =
MxArray(obj->getDefaultName());
132 else if (method ==
"detect") {
134 Mat image(rhs[2].toMat(CV_8U)), width, prec, nfa;
136 obj->detect(image, lines,
137 (nlhs>1 ? width : noArray()),
138 (nlhs>2 ? prec : noArray()),
139 (nlhs>3 ? nfa : noArray()));
148 else if (method ==
"drawSegments") {
150 Mat image(rhs[2].toMat());
152 vector<Vec4f> lines(rhs[3].toVector<Vec4f>());
153 obj->drawSegments(image, lines);
156 else if (method ==
"compareSegments") {
157 nargchk(nrhs>=5 && (nrhs%2)==1 && nlhs<=2);
158 Size size(rhs[2].toSize());
160 for (
int i=5; i<nrhs; i+=2) {
161 string key(rhs[i].toString());
163 image = rhs[i+1].toMat();
165 mexErrMsgIdAndTxt(
"mexopencv:error",
166 "Unrecognized option %s", key.c_str());
169 image.create(size, CV_8UC3);
170 image.setTo(Scalar::all(0));
174 vector<Vec4f> lines1(rhs[3].toVector<Vec4f>()),
175 lines2(rhs[4].toVector<Vec4f>());
176 int count = obj->compareSegments(size, lines1, lines2, image);
182 mexErrMsgIdAndTxt(
"mexopencv:error",
183 "Unrecognized operation %s", method.c_str());
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
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.