mexopencv  3.4.1
MEX interface for OpenCV library
fitEllipse.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 namespace {
16 
19  ("Linear", FIT_ELLIPSE_LINEAR)
20  ("Direct", FIT_ELLIPSE_DIRECT)
21  ("AMS", FIT_ELLIPSE_AMS);
22 }
23 
31 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
32 {
33  // Check the number of arguments
34  nargchk(nrhs>=1 && (nrhs%2)==1 && nlhs<=1);
35 
36  // Argument vector
37  vector<MxArray> rhs(prhs, prhs+nrhs);
38 
39  // Option processing
40  int method = FIT_ELLIPSE_LINEAR;
41  for (int i=1; i<nrhs; i+=2) {
42  string key(rhs[i].toString());
43  if (key == "Method")
44  method = FitEllipseAlgMap[rhs[i+1].toString()];
45  else
46  mexErrMsgIdAndTxt("mexopencv:error",
47  "Unrecognized option %s", key.c_str());
48  }
49 
50  // Process
51  RotatedRect r;
52  if (rhs[0].isNumeric()) {
53  Mat points(rhs[0].toMat(rhs[0].isInt32() ? CV_32S : CV_32F));
54  switch (method) {
55  case FIT_ELLIPSE_LINEAR:
56  r = fitEllipse(points);
57  break;
58  case FIT_ELLIPSE_DIRECT:
59  r = fitEllipseDirect(points);
60  break;
61  case FIT_ELLIPSE_AMS:
62  r = fitEllipseAMS(points);
63  break;
64  }
65  }
66  else if (rhs[0].isCell()) {
67  vector<Point2f> points(rhs[0].toVector<Point2f>());
68  switch (method) {
69  case FIT_ELLIPSE_LINEAR:
70  r = fitEllipse(points);
71  break;
72  case FIT_ELLIPSE_DIRECT:
73  r = fitEllipseDirect(points);
74  break;
75  case FIT_ELLIPSE_AMS:
76  r = fitEllipseAMS(points);
77  break;
78  }
79  }
80  else
81  mexErrMsgIdAndTxt("mexopencv:error", "Invalid points argument");
82  plhs[0] = MxArray(r);
83 }
RotatedRect fitEllipseDirect(InputArray points)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: fitEllipse.cpp:31
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
#define CV_32F
const ConstMap< string, int > FitEllipseAlgMap
Fit ellipse algorithm for option processing.
Definition: fitEllipse.cpp:18
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...
RotatedRect fitEllipseAMS(InputArray points)
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
STL class.
#define CV_32S
Global constant definitions.
T c_str(T... args)
RotatedRect fitEllipse(InputArray points)
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
cv::Mat toMat() const