mexopencv  3.4.1
MEX interface for OpenCV library
line.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 pt1(rhs[1].toPoint()),
58  pt2(rhs[2].toPoint());
59  line(img, pt1, pt2, color, thickness, lineType, shift);
60  }
61  else {
62  vector<Point> pt1(rhs[1].toVector<Point>()),
63  pt2(rhs[2].toVector<Point>());
64  if (pt1.size() != pt2.size())
65  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
66  if (!colors.empty() && colors.size() != pt1.size())
67  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
68  for (size_t i = 0; i < pt1.size(); ++i)
69  line(img, pt1[i], pt2[i],
70  (colors.empty() ? color : Scalar(colors[i])),
71  thickness, lineType, shift);
72  }
73  plhs[0] = MxArray(img);
74 }
Scalar_< double > Scalar
T empty(T... args)
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void line(InputOutputArray img, Point pt1, Point pt2, 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)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: line.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