mexopencv  3.4.1
MEX interface for OpenCV library
ellipse.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>=2 && nlhs<=1);
24 
25  // Argument vector
26  vector<MxArray> rhs(prhs, prhs+nrhs);
27 
28  // cv::ellipse has two overloaded variants
29  bool rrect_variant = rhs[1].isStruct();
30  nargchk(rrect_variant ? ((nrhs%2)==0) : (nrhs>=3 && (nrhs%2)==1));
31 
32  // Option processing
33  double angle = 0;
34  double startAngle = 0;
35  double endAngle = 360;
36  Scalar color;
37  vector<Vec4d> colors;
38  int thickness = 1;
39  int lineType = cv::LINE_8;
40  int shift = 0;
41  for (int i=(rrect_variant ? 2 : 3); i<nrhs; i+=2) {
42  string key(rhs[i].toString());
43  if (key == "Angle" && !rrect_variant)
44  angle = rhs[i+1].toDouble();
45  else if (key == "StartAngle" && !rrect_variant)
46  startAngle = rhs[i+1].toDouble();
47  else if (key == "EndAngle" && !rrect_variant)
48  endAngle = rhs[i+1].toDouble();
49  else if (key == "Color")
50  color = (rhs[i+1].isChar()) ?
51  ColorType[rhs[i+1].toString()] : rhs[i+1].toScalar();
52  else if (key == "Colors")
53  colors = MxArrayToVectorVec<double,4>(rhs[i+1]);
54  else if (key == "Thickness")
55  thickness = (rhs[i+1].isChar()) ?
56  ThicknessType[rhs[i+1].toString()] : rhs[i+1].toInt();
57  else if (key == "LineType")
58  lineType = (rhs[i+1].isChar()) ?
59  LineType[rhs[i+1].toString()] : rhs[i+1].toInt();
60  else if (key == "Shift" && !rrect_variant)
61  shift = rhs[i+1].toInt();
62  else
63  mexErrMsgIdAndTxt("mexopencv:error",
64  "Unrecognized option %s", key.c_str());
65  }
66 
67  // Process
68  Mat img(rhs[0].toMat());
69  if (!rrect_variant) {
70  Point center(rhs[1].toPoint());
71  Size axes(rhs[2].toSize());
72  ellipse(img, center, axes, angle, startAngle, endAngle,
73  color, thickness, lineType, shift);
74  }
75  else {
76  if (rhs[1].numel() == 1) {
77  RotatedRect box(rhs[1].toRotatedRect());
78  ellipse(img, box, color, thickness, lineType);
79  }
80  else {
81  vector<RotatedRect> box(rhs[1].toVector<RotatedRect>());
82  if (!colors.empty() && colors.size() != box.size())
83  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
84  for (size_t i = 0; i < box.size(); ++i)
85  ellipse(img, box[i],
86  (colors.empty() ? color : Scalar(colors[i])),
87  thickness, lineType);
88  }
89  }
90  plhs[0] = MxArray(img);
91 }
Scalar_< double > Scalar
T empty(T... args)
STL namespace.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: ellipse.cpp:20
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, 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...
const ConstMap< std::string, int > LineType
Line type for drawing.
Definition: mexopencv.hpp:124
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
T size(T... args)
const ConstMap< std::string, int > ThicknessType
Thickness type for drawing.
Definition: mexopencv.hpp:130
STL class.
Global constant definitions.
T c_str(T... args)
const ConstMap< std::string, cv::Scalar > ColorType
Translates MATLAB color names (see ColorSpec) into OpenCV scalars.
Definition: mexopencv.hpp:55
cv::Mat toMat() const