mexopencv  3.4.1
MEX interface for OpenCV library
arrowedLine.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  double tipLength = 0.1;
35  for (int i=3; i<nrhs; i+=2) {
36  string key(rhs[i].toString());
37  if (key == "Color")
38  color = (rhs[i+1].isChar()) ?
39  ColorType[rhs[i+1].toString()] : rhs[i+1].toScalar();
40  else if (key == "Colors")
41  colors = MxArrayToVectorVec<double,4>(rhs[i+1]);
42  else if (key == "Thickness")
43  thickness = (rhs[i+1].isChar()) ?
44  ThicknessType[rhs[i+1].toString()] : rhs[i+1].toInt();
45  else if (key == "LineType")
46  lineType = (rhs[i+1].isChar()) ?
47  LineType[rhs[i+1].toString()] : rhs[i+1].toInt();
48  else if (key == "Shift")
49  shift = rhs[i+1].toInt();
50  else if (key == "TipLength")
51  tipLength = rhs[i+1].toDouble();
52  else
53  mexErrMsgIdAndTxt("mexopencv:error",
54  "Unrecognized option %s", key.c_str());
55  }
56 
57  // Process
58  Mat img(rhs[0].toMat());
59  if (rhs[1].isNumeric() && rhs[1].numel() == 2) {
60  Point pt1(rhs[1].toPoint()),
61  pt2(rhs[2].toPoint());
62  arrowedLine(img, pt1, pt2, color, thickness, lineType, shift, tipLength);
63  }
64  else {
65  vector<Point> pt1(rhs[1].toVector<Point>()),
66  pt2(rhs[2].toVector<Point>());
67  if (pt1.size() != pt2.size())
68  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
69  if (!colors.empty() && colors.size() != pt1.size())
70  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
71  for (size_t i = 0; i < pt1.size(); ++i)
72  arrowedLine(img, pt1[i], pt2[i],
73  (colors.empty() ? color : Scalar(colors[i])),
74  thickness, lineType, shift, tipLength);
75  }
76  plhs[0] = MxArray(img);
77 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: arrowedLine.cpp:20
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...
void arrowedLine(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int line_type=8, int shift=0, double tipLength=0.1)
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