mexopencv  3.4.1
MEX interface for OpenCV library
putText.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  int fontFace = cv::FONT_HERSHEY_SIMPLEX;
30  int fontStyle = 0;
31  double fontScale = 1.0;
32  Scalar color;
33  vector<Vec4d> colors;
34  int thickness = 1;
35  int lineType = cv::LINE_8;
36  bool bottomLeftOrigin = false;
37  for (int i=3; i<nrhs; i+=2) {
38  string key(rhs[i].toString());
39  if (key == "FontFace")
40  fontFace = FontFace[rhs[i+1].toString()];
41  else if (key == "FontStyle")
42  fontStyle = FontStyle[rhs[i+1].toString()];
43  else if (key == "FontScale")
44  fontScale = rhs[i+1].toDouble();
45  else if (key == "Color")
46  color = (rhs[i+1].isChar()) ?
47  ColorType[rhs[i+1].toString()] : rhs[i+1].toScalar();
48  else if (key == "Colors")
49  colors = MxArrayToVectorVec<double,4>(rhs[i+1]);
50  else if (key == "Thickness")
51  thickness = (rhs[i+1].isChar()) ?
52  ThicknessType[rhs[i+1].toString()] : rhs[i+1].toInt();
53  else if (key == "LineType")
54  lineType = (rhs[i+1].isChar()) ?
55  LineType[rhs[i+1].toString()] : rhs[i+1].toInt();
56  else if (key == "BottomLeftOrigin")
57  bottomLeftOrigin = rhs[i+1].toBool();
58  else
59  mexErrMsgIdAndTxt("mexopencv:error",
60  "Unrecognized option %s", key.c_str());
61  }
62  fontFace |= fontStyle;
63 
64  // Process
65  Mat img(rhs[0].toMat());
66  if (rhs[1].isChar()) {
67  string text(rhs[1].toString());
68  Point org(rhs[2].toPoint());
69  putText(img, text, org, fontFace, fontScale, color, thickness,
70  lineType, bottomLeftOrigin);
71  }
72  else {
73  vector<string> text(rhs[1].toVector<string>());
74  vector<Point> org(rhs[2].toVector<Point>());
75  if (text.size() != org.size())
76  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
77  if (!colors.empty() && colors.size() != text.size())
78  mexErrMsgIdAndTxt("mexopencv:error", "Length mismatch");
79  for (size_t i = 0; i < text.size(); ++i)
80  putText(img, text[i], org[i], fontFace, fontScale,
81  (colors.empty() ? color : Scalar(colors[i])),
82  thickness, lineType, bottomLeftOrigin);
83  }
84  plhs[0] = MxArray(img);
85 }
const ConstMap< std::string, int > FontStyle
Font styles for drawing.
Definition: mexopencv.hpp:145
Scalar_< double > Scalar
T empty(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: putText.cpp:20
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)
FONT_HERSHEY_SIMPLEX
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
const ConstMap< std::string, cv::Scalar > ColorType
Translates MATLAB color names (see ColorSpec) into OpenCV scalars.
Definition: mexopencv.hpp:55
cv::Mat toMat() const
const ConstMap< std::string, int > FontFace
Font faces for drawing.
Definition: mexopencv.hpp:134