mexopencv  0.1
mex interface for opencv library
fitLine.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
19 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
20 {
21  // Check the number of arguments
22  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1);
23 
24  // Argument vector
25  vector<MxArray> rhs(prhs, prhs+nrhs);
26 
27  // Option processing
28  int distType = cv::DIST_L2;
29  double param = 0;
30  double reps = 0.01;
31  double aeps = 0.01;
32  for (int i=1; i<nrhs; i+=2) {
33  string key(rhs[i].toString());
34  if (key=="DistType")
35  distType = DistType[rhs[i+1].toString()];
36  else if (key=="Param")
37  param = rhs[i+1].toDouble();
38  else if (key=="RadiusEps")
39  reps = rhs[i+1].toDouble();
40  else if (key=="AngleEps")
41  aeps = rhs[i+1].toDouble();
42  else
43  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized option");
44  }
45 
46  // Process
47  Mat line; // 4x1 or 6x1
48  if (rhs[0].isNumeric()) {
49  Mat points(rhs[0].toMat(CV_32F));
50  fitLine(points, line, distType, param, reps, aeps);
51  }
52  else if (rhs[0].isCell() && !rhs[0].isEmpty()) {
53  mwSize n = rhs[0].at<MxArray>(0).numel();
54  if (n == 2) {
55  vector<Point2f> points(rhs[0].toVector<Point2f>());
56  fitLine(points, line, distType, param, reps, aeps);
57  }
58  else if (n == 3) {
59  vector<Point3f> points(rhs[0].toVector<Point3f>());
60  fitLine(points, line, distType, param, reps, aeps);
61  }
62  else
63  mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
64  }
65  else
66  mexErrMsgIdAndTxt("mexopencv:error","Invalid input");
67  plhs[0] = MxArray(line);
68 }
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:87
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: fitLine.cpp:19