mexopencv  3.4.1
MEX interface for OpenCV library
mexopencv.hpp
Go to the documentation of this file.
1 
11 #ifndef MEXOPENCV_HPP
12 #define MEXOPENCV_HPP
13 
14 #include "MxArray.hpp"
15 
16 /**************************************************************\
17 * Global constants *
18 \**************************************************************/
19 
28  ("uint8", CV_8U)
29  ("int8", CV_8S)
30  ("uint16", CV_16U)
31  ("int16", CV_16S)
32  //("uint32", CV_32S)
33  ("int32", CV_32S)
34  ("single", CV_32F)
35  ("double", CV_64F)
36  ("logical", CV_8U);
37 
43  (CV_8U, "uint8")
44  (CV_8S, "int8")
45  (CV_16U, "uint16")
46  (CV_16S, "int16")
47  (CV_32S, "int32")
48  (CV_32F, "single")
49  (CV_64F, "double");
50 
56  ("r", cv::Scalar( 0, 0,255))
57  ("g", cv::Scalar( 0,255, 0))
58  ("b", cv::Scalar(255, 0, 0))
59  ("c", cv::Scalar(255,255, 0))
60  ("m", cv::Scalar(255, 0,255))
61  ("y", cv::Scalar( 0,255,255))
62  ("k", cv::Scalar( 0, 0, 0))
63  ("w", cv::Scalar(255,255,255));
64 
67  ("Constant", cv::BORDER_CONSTANT) // iiiiii|abcdefgh|iiiiiii for some i
68  ("Replicate", cv::BORDER_REPLICATE) // aaaaaa|abcdefgh|hhhhhhh
69  ("Reflect", cv::BORDER_REFLECT) // fedcba|abcdefgh|hgfedcb
70  ("Reflect101", cv::BORDER_REFLECT_101) // gfedcb|abcdefgh|gfedcba
71  ("Wrap", cv::BORDER_WRAP) // cdefgh|abcdefgh|abcdefg
72  ("Transparent", cv::BORDER_TRANSPARENT) // uvwxyz|abcdefgh|ijklmno
73  ("Default", cv::BORDER_DEFAULT); // same as "Reflect101"
74 
77  (cv::BORDER_CONSTANT, "Constant")
78  (cv::BORDER_REPLICATE, "Replicate")
79  (cv::BORDER_REFLECT, "Reflect")
80  (cv::BORDER_REFLECT_101, "Reflect101")
81  (cv::BORDER_WRAP, "Wrap")
82  (cv::BORDER_TRANSPARENT, "Transparent");
83 
86  ("Nearest", cv::INTER_NEAREST) // nearest neighbor interpolation
87  ("Linear", cv::INTER_LINEAR) // bilinear interpolation
88  ("Cubic", cv::INTER_CUBIC) // bicubic interpolation
89  ("Area", cv::INTER_AREA) // area-based (or super) interpolation
90  ("Lanczos4", cv::INTER_LANCZOS4) // Lanczos interpolation over 8x8 neighborhood
91  ("LinearExact", cv::INTER_LINEAR_EXACT); // Bit exact bilinear interpolation
92 
95  ("Binary", cv::THRESH_BINARY) // val = (val > thresh) ? maxVal : 0
96  ("BinaryInv", cv::THRESH_BINARY_INV) // val = (val > thresh) ? 0 : maxVal
97  ("Trunc", cv::THRESH_TRUNC) // val = (val > thresh) ? thresh : val
98  ("ToZero", cv::THRESH_TOZERO) // val = (val > thresh) ? val : 0
99  ("ToZeroInv", cv::THRESH_TOZERO_INV); // val = (val > thresh) ? 0 : val
100 
103  ("User", cv::DIST_USER) // user-defined distance
104  ("L1", cv::DIST_L1) // distance = |x1-x2| + |y1-y2|
105  ("L2", cv::DIST_L2) // the simple euclidean distance
106  ("C", cv::DIST_C) // distance = max(|x1-x2|,|y1-y2|)
107  ("L12", cv::DIST_L12) // distance = 2*(sqrt(1+x*x/2) - 1)
108  ("Fair", cv::DIST_FAIR) // distance = c^2*(|x|/c-log(1+|x|/c)), c = 1.3998
109  ("Welsch", cv::DIST_WELSCH) // distance = c^2/2*(1-exp(-(x/c)^2)), c = 2.9846
110  ("Huber", cv::DIST_HUBER); // distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345
111 
114  (cv::DIST_USER, "User")
115  (cv::DIST_L1, "L1")
116  (cv::DIST_L2, "L2")
117  (cv::DIST_C, "C")
118  (cv::DIST_L12, "L12")
119  (cv::DIST_FAIR, "Fair")
120  (cv::DIST_WELSCH, "Welsch")
121  (cv::DIST_HUBER, "Huber");
122 
125  ("4", cv::LINE_4)
126  ("8", cv::LINE_8)
127  ("AA", cv::LINE_AA);
128 
131  ("Filled", cv::FILLED);
132 
135  ("HersheySimplex", cv::FONT_HERSHEY_SIMPLEX)
136  ("HersheyPlain", cv::FONT_HERSHEY_PLAIN)
137  ("HersheyDuplex", cv::FONT_HERSHEY_DUPLEX)
138  ("HersheyComplex", cv::FONT_HERSHEY_COMPLEX)
139  ("HersheyTriplex", cv::FONT_HERSHEY_TRIPLEX)
140  ("HersheyComplexSmall", cv::FONT_HERSHEY_COMPLEX_SMALL)
141  ("HersheyScriptSimplex", cv::FONT_HERSHEY_SCRIPT_SIMPLEX)
142  ("HersheyScriptComplex", cv::FONT_HERSHEY_SCRIPT_COMPLEX);
143 
146  ("Regular", 0)
147  ("Italic", cv::FONT_ITALIC);
148 
151  ("Inf", cv::NORM_INF)
152  ("L1", cv::NORM_L1)
153  ("L2", cv::NORM_L2)
154  ("L2Sqr", cv::NORM_L2SQR)
155  ("Hamming", cv::NORM_HAMMING)
156  ("Hamming2", cv::NORM_HAMMING2)
157  ("MinMax", cv::NORM_MINMAX);
158 
161  (cv::NORM_INF, "Inf")
162  (cv::NORM_L1, "L1")
163  (cv::NORM_L2, "L2")
164  (cv::NORM_L2SQR, "L2Sqr")
165  (cv::NORM_HAMMING, "Hamming")
166  (cv::NORM_HAMMING2, "Hamming2")
167  (cv::NORM_MINMAX, "MinMax");
168 
169 /**************************************************************\
170 * Helper Macros & Functions *
171 \**************************************************************/
172 
174 #define UPDATE_FLAG(NUM, TF, BIT) \
175  do { \
176  if ((TF)) { (NUM) |= (BIT); } \
177  else { (NUM) &= ~(BIT); } \
178  } while (0)
179 
181 inline void nargchk(bool cond)
182 {
183  if (!cond) {
184  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
185  }
186 }
187 
188 /**************************************************************\
189 * Conversion Functions: MxArray to vector *
190 \**************************************************************/
191 
207 template <typename T>
209 {
211  if (arr.isNumeric()) {
212  if (arr.numel() == 2)
213  vp.push_back(arr.toPoint_<T>());
214  else
215  arr.toMat(cv::traits::Depth<cv::Point_<T> >::value).reshape(2, 0).copyTo(vp);
216  }
217  else if (arr.isCell()) {
218  /*
219  std::vector<MxArray> va(arr.toVector<MxArray>());
220  vp.reserve(va.size());
221  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
222  vp.push_back(it->toPoint_<T>());
223  */
224  vp = arr.toVector(
225  std::const_mem_fun_ref_t<cv::Point_<T>, MxArray>(
226  &MxArray::toPoint_<T>));
227  }
228  else
229  mexErrMsgIdAndTxt("mexopencv:error",
230  "Unable to convert MxArray to std::vector<cv::Point_<T>>");
231  return vp;
232 }
233 
249 template <typename T>
251 {
253  if (arr.isNumeric()) {
254  if (arr.numel() == 3)
255  vp.push_back(arr.toPoint3_<T>());
256  else
257  arr.toMat(cv::traits::Depth<cv::Point3_<T> >::value).reshape(3, 0).copyTo(vp);
258  }
259  else if (arr.isCell()) {
260  /*
261  std::vector<MxArray> va(arr.toVector<MxArray>());
262  vp.reserve(va.size());
263  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
264  vp.push_back(it->toPoint3_<T>());
265  */
266  vp = arr.toVector(
267  std::const_mem_fun_ref_t<cv::Point3_<T>, MxArray>(
268  &MxArray::toPoint3_<T>));
269  }
270  else
271  mexErrMsgIdAndTxt("mexopencv:error",
272  "Unable to convert MxArray to std::vector<cv::Point3_<T>>");
273  return vp;
274 }
275 
291 template <typename T>
293 {
295  if (arr.isNumeric()) {
296  if (arr.numel() == 2)
297  vs.push_back(arr.toSize_<T>());
298  else
299  arr.toMat(cv::traits::Depth<cv::Size_<T> >::value).reshape(2, 0).copyTo(vs);
300  }
301  else if (arr.isCell()) {
302  /*
303  std::vector<MxArray> va(arr.toVector<MxArray>());
304  vs.reserve(va.size());
305  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
306  vs.push_back(it->toSize_<T>());
307  */
308  vs = arr.toVector(
309  std::const_mem_fun_ref_t<cv::Size_<T>, MxArray>(
310  &MxArray::toSize_<T>));
311  }
312  else
313  mexErrMsgIdAndTxt("mexopencv:error",
314  "Unable to convert MxArray to std::vector<cv::Size_<T>>");
315  return vs;
316 }
317 
334 template <typename T>
336 {
338  if (arr.isNumeric()) {
339  if (arr.numel() == 4)
340  vr.push_back(arr.toRect_<T>());
341  else
342  arr.toMat(cv::traits::Depth<cv::Rect_<T> >::value).reshape(4, 0).copyTo(vr);
343  }
344  else if (arr.isCell()) {
345  /*
346  std::vector<MxArray> va(arr.toVector<MxArray>());
347  vr.reserve(va.size());
348  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
349  vr.push_back(it->toRect_<T>());
350  */
351  vr = arr.toVector(
352  std::const_mem_fun_ref_t<cv::Rect_<T>, MxArray>(
353  &MxArray::toRect_<T>));
354  }
355  else
356  mexErrMsgIdAndTxt("mexopencv:error",
357  "Unable to convert MxArray to std::vector<cv::Rect_<T>>");
358  return vr;
359 }
360 
377 template <typename T, int cn>
379 {
381  if (arr.isNumeric()) {
382  if (arr.numel() == cn)
383  vv.push_back(arr.toVec<T,cn>());
384  else
385  arr.toMat(cv::traits::Depth<cv::Vec<T,cn> >::value).reshape(cn, 0).copyTo(vv);
386  }
387  else if (arr.isCell()) {
388  /*
389  std::vector<MxArray> va(arr.toVector<MxArray>());
390  vv.reserve(va.size());
391  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
392  vv.push_back(it->toVec<T,cn>());
393  */
394  vv = arr.toVector(
395  std::const_mem_fun_ref_t<cv::Vec<T,cn>, MxArray>(
396  &MxArray::toVec<T,cn>));
397  }
398  else
399  mexErrMsgIdAndTxt("mexopencv:error",
400  "Unable to convert MxArray to std::vector<cv::Vec<T,cn>>");
401  return vv;
402 }
403 
419 template <typename T, int m, int n>
421 {
423  if (arr.isNumeric()) {
424  vx.push_back(arr.toMatx<T,m,n>());
425  }
426  else if (arr.isCell()) {
427  /*
428  std::vector<MxArray> va(arr.toVector<MxArray>());
429  vx.reserve(va.size());
430  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
431  vx.push_back(it->toMatx<T,m,n>());
432  */
433  vx = arr.toVector(
434  std::const_mem_fun_ref_t<cv::Matx<T,m,n>, MxArray>(
435  &MxArray::toMatx<T,m,n>));
436  }
437  else
438  mexErrMsgIdAndTxt("mexopencv:error",
439  "Unable to convert MxArray to std::vector<cv::Matx<T,m,n>>");
440  return vx;
441 }
442 
443 /**************************************************************\
444 * Conversion Functions: MxArray to vector of vectors *
445 \**************************************************************/
446 
462 template <typename T>
464 {
465  /*
466  std::vector<MxArray> vva(arr.toVector<MxArray>());
467  std::vector<std::vector<T> > vv;
468  vv.reserve(vva.size());
469  for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
470  vv.push_back(it->toVector<T>());
471  }
472  return vv;
473  */
474  typedef std::vector<T> VecT;
475  std::const_mem_fun_ref_t<VecT, MxArray> func(&MxArray::toVector<T>);
476  return arr.toVector(func);
477 }
478 
495 template <typename T>
497 {
500  vvp.reserve(vva.size());
501  for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
502  /*
503  std::vector<MxArray> va(it->toVector<MxArray());
504  std::vector<cv::Point_<T> > vp;
505  for (std::vector<MxArray>::const_iterator jt = va.begin(); jt != va.end(); ++jt) {
506  vp.push_back(jt->toPoint_<T>());
507  }
508  vvp.push_back(vp);
509  */
510  vvp.push_back(MxArrayToVectorPoint<T>(*it));
511  }
512  return vvp;
513 }
514 
531 template <typename T>
533 {
536  vvp.reserve(vva.size());
537  for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
538  /*
539  std::vector<MxArray> va(it->toVector<MxArray());
540  std::vector<cv::Point3_<T> > vp;
541  for (std::vector<MxArray>::const_iterator jt = va.begin(); jt != va.end(); ++jt) {
542  vp.push_back(jt->toPoint3_<T>());
543  }
544  vvp.push_back(vp);
545  */
546  vvp.push_back(MxArrayToVectorPoint3<T>(*it));
547  }
548  return vvp;
549 }
550 
551 #endif
const ConstMap< std::string, int > FontStyle
Font styles for drawing.
Definition: mexopencv.hpp:145
Scalar_< double > Scalar
DIST_HUBER
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
std::vector< cv::Size_< T > > MxArrayToVectorSize(const MxArray &arr)
Convert an MxArray to std::vector<cv::Size_<T>>
Definition: mexopencv.hpp:292
FONT_HERSHEY_COMPLEX_SMALL
std::vector< cv::Vec< T, cn > > MxArrayToVectorVec(const MxArray &arr)
Convert an MxArray to std::vector<cv::Vec<T,cn>>
Definition: mexopencv.hpp:378
BORDER_REPLICATE
THRESH_TRUNC
mwSize numel() const
Number of elements in an array.
Definition: MxArray.hpp:546
BORDER_REFLECT
INTER_CUBIC
void copyTo(OutputArray m) const
NORM_HAMMING
cv::Point3_< T > toPoint3_() const
Convert MxArray to Point3_<T>.
Definition: MxArray.hpp:1075
#define CV_8U
NORM_INF
const ConstMap< std::string, int > InterpType
Interpolation type map for option processing.
Definition: mexopencv.hpp:85
THRESH_TOZERO_INV
const ConstMap< std::string, int > ThreshType
Thresholding type map for option processing.
Definition: mexopencv.hpp:94
THRESH_TOZERO
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:160
T end(T... args)
cv::Rect_< T > toRect_() const
Convert MxArray to Rect_<T>.
Definition: MxArray.hpp:1091
cv::Matx< T, m, n > toMatx() const
Convert MxArray to Matx<T,m,n>.
Definition: MxArray.hpp:1131
std::vector< cv::Point_< T > > MxArrayToVectorPoint(const MxArray &arr)
Convert an MxArray to std::vector<cv::Point_<T>>
Definition: mexopencv.hpp:208
const ConstMap< int, std::string > DistTypeInv
Inverse Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:113
FONT_HERSHEY_SCRIPT_SIMPLEX
const ConstMap< int, std::string > BorderTypeInv
Inverse border type map for option processing.
Definition: mexopencv.hpp:76
DIST_USER
FONT_HERSHEY_COMPLEX
string func
name of MATLAB function to evaluate (custom face detector)
Definition: Facemark_.cpp:21
T push_back(T... args)
DIST_WELSCH
FONT_HERSHEY_DUPLEX
cv::Point_< T > toPoint_() const
Convert MxArray to Point_<T>.
Definition: MxArray.hpp:1067
BORDER_REFLECT_101
Mat reshape(int cn, int rows=0) const
DIST_L12
bool isNumeric() const
Determine whether array is numeric.
Definition: MxArray.hpp:695
std::vector< std::vector< cv::Point3_< T > > > MxArrayToVectorVectorPoint3(const MxArray &arr)
Convert an MxArray to std::vector<std::vector<cv::Point3_<T>>>
Definition: mexopencv.hpp:532
#define CV_64F
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 > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:66
bool isCell() const
Determine whether input is cell array.
Definition: MxArray.hpp:610
NORM_HAMMING2
INTER_LANCZOS4
INTER_AREA
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
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:102
#define CV_16U
THRESH_BINARY
T size(T... args)
NORM_L2SQR
const ConstMap< std::string, int > ThicknessType
Thickness type for drawing.
Definition: mexopencv.hpp:130
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
Definition: MxArray.hpp:1151
STL class.
FONT_ITALIC
#define CV_32S
T begin(T... args)
INTER_LINEAR
std::vector< std::vector< cv::Point_< T > > > MxArrayToVectorVectorPoint(const MxArray &arr)
Convert an MxArray to std::vector<std::vector<cv::Point_<T>>>
Definition: mexopencv.hpp:496
FONT_HERSHEY_TRIPLEX
BORDER_TRANSPARENT
FONT_HERSHEY_SIMPLEX
cv::Vec< T, cn > toVec() const
Convert MxArray to Vec<T,cn>.
Definition: MxArray.hpp:1114
cv::Mat toMat(int depth=CV_USRTYPE1, bool transpose=true) const
Convert MxArray to cv::Mat.
Definition: MxArray.cpp:529
cv::Size_< T > toSize_() const
Convert MxArray to Size_<T>.
Definition: MxArray.hpp:1083
BORDER_CONSTANT
INTER_NEAREST
NORM_MINMAX
std::vector< std::vector< T > > MxArrayToVectorVectorPrimitive(const MxArray &arr)
Convert an MxArray to std::vector<std::vector<T>>
Definition: mexopencv.hpp:463
BORDER_DEFAULT
FONT_HERSHEY_PLAIN
const ConstMap< std::string, cv::Scalar > ColorType
Translates MATLAB color names (see ColorSpec) into OpenCV scalars.
Definition: mexopencv.hpp:55
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
DIST_FAIR
std::vector< cv::Point3_< T > > MxArrayToVectorPoint3(const MxArray &arr)
Convert an MxArray to std::vector<cv::Point3_<T>>
Definition: mexopencv.hpp:250
std::vector< cv::Matx< T, m, n > > MxArrayToVectorMatx(const MxArray &arr)
Convert an MxArray to std::vector<cv::Matx<T,m,n>>
Definition: mexopencv.hpp:420
const ConstMap< std::string, int > NormType
Norm type map for option processing.
Definition: mexopencv.hpp:150
FONT_HERSHEY_SCRIPT_COMPLEX
std::vector< cv::Rect_< T > > MxArrayToVectorRect(const MxArray &arr)
Convert an MxArray to std::vector<cv::Rect_<T>>
Definition: mexopencv.hpp:335
BORDER_WRAP
T reserve(T... args)
INTER_LINEAR_EXACT
const ConstMap< std::string, int > FontFace
Font faces for drawing.
Definition: mexopencv.hpp:134
THRESH_BINARY_INV