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