mexopencv  3.4.1
MEX interface for OpenCV library
fitLine.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 
20 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
21 {
22  // Check the number of arguments
23  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // Option processing
29  int distType = cv::DIST_L2;
30  double param = 0;
31  double reps = 0.01;
32  double aeps = 0.01;
33  for (int i=1; i<nrhs; i+=2) {
34  string key(rhs[i].toString());
35  if (key == "DistType")
36  distType = DistType[rhs[i+1].toString()];
37  else if (key == "Param")
38  param = rhs[i+1].toDouble();
39  else if (key == "RadiusEps")
40  reps = rhs[i+1].toDouble();
41  else if (key == "AngleEps")
42  aeps = rhs[i+1].toDouble();
43  else
44  mexErrMsgIdAndTxt("mexopencv:error",
45  "Unrecognized option %s", key.c_str());
46  }
47 
48  // Process
49  Mat line; // 4x1 or 6x1
50  if (rhs[0].isNumeric()) {
51  Mat points(rhs[0].toMat(CV_32F));
52  fitLine(points, line, distType, param, reps, aeps);
53  }
54  else if (rhs[0].isCell() && !rhs[0].isEmpty()) {
55  mwSize n = rhs[0].at<MxArray>(0).numel();
56  if (n == 2) {
57  vector<Point2f> points(rhs[0].toVector<Point2f>());
58  fitLine(points, line, distType, param, reps, aeps);
59  }
60  else if (n == 3) {
61  vector<Point3f> points(rhs[0].toVector<Point3f>());
62  fitLine(points, line, distType, param, reps, aeps);
63  }
64  else
65  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
66  }
67  else
68  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
69  plhs[0] = MxArray(line);
70 }
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
T at(T... args)
void fitLine(InputArray points, OutputArray line, int distType, double param, double reps, double aeps)
#define CV_32F
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
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...
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:102
STL class.
Global constant definitions.
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: fitLine.cpp:20
cv::Mat toMat() const