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