mexopencv  0.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 
53  ("Constant", cv::BORDER_CONSTANT) // iiiiii|abcdefgh|iiiiiii for some i
54  ("Replicate", cv::BORDER_REPLICATE) // aaaaaa|abcdefgh|hhhhhhh
55  ("Reflect", cv::BORDER_REFLECT) // fedcba|abcdefgh|hgfedcb
56  ("Reflect101", cv::BORDER_REFLECT_101) // gfedcb|abcdefgh|gfedcba
57  ("Wrap", cv::BORDER_WRAP) // cdefgh|abcdefgh|abcdefg
58  ("Transparent", cv::BORDER_TRANSPARENT) // uvwxyz|absdefgh|ijklmno
59  ("Default", cv::BORDER_DEFAULT); // same as "Reflect101"
60 
63  (cv::BORDER_CONSTANT, "Constant")
64  (cv::BORDER_REPLICATE, "Replicate")
65  (cv::BORDER_REFLECT, "Reflect")
66  (cv::BORDER_REFLECT_101, "Reflect101")
67  (cv::BORDER_WRAP, "Wrap")
68  (cv::BORDER_TRANSPARENT, "Transparent");
69 
72  ("Nearest", cv::INTER_NEAREST) // nearest neighbor interpolation
73  ("Linear", cv::INTER_LINEAR) // bilinear interpolation
74  ("Cubic", cv::INTER_CUBIC) // bicubic interpolation
75  ("Area", cv::INTER_AREA) // area-based (or super) interpolation
76  ("Lanczos4", cv::INTER_LANCZOS4); // Lanczos interpolation over 8x8 neighborhood
77 
80  ("Binary", cv::THRESH_BINARY) // val = (val > thresh) ? maxVal : 0
81  ("BinaryInv", cv::THRESH_BINARY_INV) // val = (val > thresh) ? 0 : maxVal
82  ("Trunc", cv::THRESH_TRUNC) // val = (val > thresh) ? thresh : val
83  ("ToZero", cv::THRESH_TOZERO) // val = (val > thresh) ? val : 0
84  ("ToZeroInv", cv::THRESH_TOZERO_INV); // val = (val > thresh) ? 0 : val
85 
88  ("User", cv::DIST_USER) // user-defined distance
89  ("L1", cv::DIST_L1) // distance = |x1-x2| + |y1-y2|
90  ("L2", cv::DIST_L2) // the simple euclidean distance
91  ("C", cv::DIST_C) // distance = max(|x1-x2|,|y1-y2|)
92  ("L12", cv::DIST_L12) // distance = 2*(sqrt(1+x*x/2) - 1)
93  ("Fair", cv::DIST_FAIR) // distance = c^2*(|x|/c-log(1+|x|/c)), c = 1.3998
94  ("Welsch", cv::DIST_WELSCH) // distance = c^2/2*(1-exp(-(x/c)^2)), c = 2.9846
95  ("Huber", cv::DIST_HUBER); // distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345
96 
99  (cv::DIST_USER, "User")
100  (cv::DIST_L1, "L1")
101  (cv::DIST_L2, "L2")
102  (cv::DIST_C, "C")
103  (cv::DIST_L12, "L12")
104  (cv::DIST_FAIR, "Fair")
105  (cv::DIST_WELSCH, "Welsch")
106  (cv::DIST_HUBER, "Huber");
107 
110  ("4", cv::LINE_4)
111  ("8", cv::LINE_8)
112  ("AA", cv::LINE_AA);
113 
116  ("Filled", cv::FILLED);
117 
120  ("HersheySimplex", cv::FONT_HERSHEY_SIMPLEX)
121  ("HersheyPlain", cv::FONT_HERSHEY_PLAIN)
122  ("HersheyDuplex", cv::FONT_HERSHEY_DUPLEX)
123  ("HersheyComplex", cv::FONT_HERSHEY_COMPLEX)
124  ("HersheyTriplex", cv::FONT_HERSHEY_TRIPLEX)
125  ("HersheyComplexSmall", cv::FONT_HERSHEY_COMPLEX_SMALL)
126  ("HersheyScriptSimplex", cv::FONT_HERSHEY_SCRIPT_SIMPLEX)
127  ("HersheyScriptComplex", cv::FONT_HERSHEY_SCRIPT_COMPLEX);
128 
131  ("Regular", 0)
132  ("Italic", cv::FONT_ITALIC);
133 
136  ("Inf", cv::NORM_INF)
137  ("L1", cv::NORM_L1)
138  ("L2", cv::NORM_L2)
139  ("L2Sqr", cv::NORM_L2SQR)
140  ("Hamming", cv::NORM_HAMMING)
141  ("Hamming2", cv::NORM_HAMMING2)
142  ("MinMax", cv::NORM_MINMAX);
143 
146  (cv::NORM_INF, "Inf")
147  (cv::NORM_L1, "L1")
148  (cv::NORM_L2, "L2")
149  (cv::NORM_L2SQR, "L2Sqr")
150  (cv::NORM_HAMMING, "Hamming")
151  (cv::NORM_HAMMING2, "Hamming2")
152  (cv::NORM_MINMAX, "MinMax");
153 
154 /**************************************************************\
155 * Helper Macros & Functions *
156 \**************************************************************/
157 
159 #define UPDATE_FLAG(NUM, TF, BIT) \
160  do { \
161  if ((TF)) { (NUM) |= (BIT); } \
162  else { (NUM) &= ~(BIT); } \
163  } while(0)
164 
166 inline void nargchk(bool cond)
167 {
168  if (!cond) {
169  mexErrMsgIdAndTxt("mexopencv:error", "Wrong number of arguments");
170  }
171 }
172 
173 /**************************************************************\
174 * Conversion Functions: MxArray to vector *
175 \**************************************************************/
176 
192 template <typename T>
193 std::vector<cv::Point_<T> > MxArrayToVectorPoint(const MxArray& arr)
194 {
195  std::vector<cv::Point_<T> > vp;
196  if (arr.isNumeric()) {
197  if (arr.numel() == 2)
198  vp.push_back(arr.toPoint_<T>());
199  else
200  arr.toMat(cv::DataType<T>::depth).reshape(2, 0).copyTo(vp);
201  }
202  else if (arr.isCell()) {
203  /*
204  std::vector<MxArray> va(arr.toVector<MxArray>());
205  vp.reserve(va.size());
206  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
207  vp.push_back(it->toPoint_<T>());
208  */
209  vp = arr.toVector(
210  std::const_mem_fun_ref_t<cv::Point_<T>, MxArray>(
211  &MxArray::toPoint_<T>));
212  }
213  else
214  mexErrMsgIdAndTxt("mexopencv:error",
215  "Unable to convert MxArray to std::vector<cv::Point_<T>>");
216  return vp;
217 }
218 
234 template <typename T>
235 std::vector<cv::Point3_<T> > MxArrayToVectorPoint3(const MxArray& arr)
236 {
237  std::vector<cv::Point3_<T> > vp;
238  if (arr.isNumeric()) {
239  if (arr.numel() == 3)
240  vp.push_back(arr.toPoint3_<T>());
241  else
242  arr.toMat(cv::DataType<T>::depth).reshape(3, 0).copyTo(vp);
243  }
244  else if (arr.isCell()) {
245  /*
246  std::vector<MxArray> va(arr.toVector<MxArray>());
247  vp.reserve(va.size());
248  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
249  vp.push_back(it->toPoint3_<T>());
250  */
251  vp = arr.toVector(
252  std::const_mem_fun_ref_t<cv::Point3_<T>, MxArray>(
253  &MxArray::toPoint3_<T>));
254  }
255  else
256  mexErrMsgIdAndTxt("mexopencv:error",
257  "Unable to convert MxArray to std::vector<cv::Point3_<T>>");
258  return vp;
259 }
260 
277 template <typename T>
278 std::vector<cv::Rect_<T> > MxArrayToVectorRect(const MxArray& arr)
279 {
280  std::vector<cv::Rect_<T> > vr;
281  if (arr.isNumeric()) {
282  if (arr.numel() == 4)
283  vr.push_back(arr.toRect_<T>());
284  else
285  arr.toMat(cv::DataType<T>::depth).reshape(4, 0).copyTo(vr);
286  }
287  else if (arr.isCell()) {
288  /*
289  std::vector<MxArray> va(arr.toVector<MxArray>());
290  vr.reserve(va.size());
291  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
292  vr.push_back(it->toRect_<T>());
293  */
294  vr = arr.toVector(
295  std::const_mem_fun_ref_t<cv::Rect_<T>, MxArray>(
296  &MxArray::toRect_<T>));
297  }
298  else
299  mexErrMsgIdAndTxt("mexopencv:error",
300  "Unable to convert MxArray to std::vector<cv::Rect_<T>>");
301  return vr;
302 }
303 
320 template <typename T, int cn>
321 std::vector<cv::Vec<T,cn> > MxArrayToVectorVec(const MxArray& arr)
322 {
323  std::vector<cv::Vec<T,cn> > vv;
324  if (arr.isNumeric()) {
325  if (arr.numel() == cn)
326  vv.push_back(arr.toVec<T,cn>());
327  else
328  arr.toMat(cv::Vec<T,cn>::depth).reshape(cn, 0).copyTo(vv);
329  }
330  else if (arr.isCell()) {
331  /*
332  std::vector<MxArray> va(arr.toVector<MxArray>());
333  vv.reserve(va.size());
334  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
335  vv.push_back(it->toVec<T,cn>());
336  */
337  vv = arr.toVector(
338  std::const_mem_fun_ref_t<cv::Vec<T,cn>, MxArray>(
339  &MxArray::toVec<T,cn>));
340  }
341  else
342  mexErrMsgIdAndTxt("mexopencv:error",
343  "Unable to convert MxArray to std::vector<cv::Vec<T,cn>>");
344  return vv;
345 }
346 
362 template <typename T, int m, int n>
363 std::vector<cv::Matx<T,m,n> > MxArrayToVectorMatx(const MxArray& arr)
364 {
365  std::vector<cv::Matx<T,m,n> > vx;
366  if (arr.isNumeric()) {
367  vx.push_back(arr.toMatx<T,m,n>());
368  }
369  else if (arr.isCell()) {
370  /*
371  std::vector<MxArray> va(arr.toVector<MxArray>());
372  vx.reserve(va.size());
373  for (std::vector<MxArray>::const_iterator it = va.begin(); it != va.end(); ++it)
374  vx.push_back(it->toMatx<T,m,n>());
375  */
376  vx = arr.toVector(
377  std::const_mem_fun_ref_t<cv::Matx<T,m,n>, MxArray>(
378  &MxArray::toMatx<T,m,n>));
379  }
380  else
381  mexErrMsgIdAndTxt("mexopencv:error",
382  "Unable to convert MxArray to std::vector<cv::Matx<T,m,n>>");
383  return vx;
384 }
385 
386 /**************************************************************\
387 * Conversion Functions: MxArray to vector of vectors *
388 \**************************************************************/
389 
405 template <typename T>
406 std::vector<std::vector<T> > MxArrayToVectorVectorPrimitive(const MxArray& arr)
407 {
408  /*
409  std::vector<MxArray> vva(arr.toVector<MxArray>());
410  std::vector<std::vector<T> > vv;
411  vv.reserve(vva.size());
412  for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
413  vv.push_back(it->toVector<T>());
414  }
415  return vv;
416  */
417  typedef std::vector<T> VecT;
418  std::const_mem_fun_ref_t<VecT, MxArray> func(&MxArray::toVector<T>);
419  return arr.toVector(func);
420 }
421 
438 template <typename T>
439 std::vector<std::vector<cv::Point_<T> > > MxArrayToVectorVectorPoint(const MxArray& arr)
440 {
441  std::vector<MxArray> vva(arr.toVector<MxArray>());
442  std::vector<std::vector<cv::Point_<T> > > vvp;
443  vvp.reserve(vva.size());
444  for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
445  /*
446  std::vector<MxArray> va(it->toVector<MxArray());
447  std::vector<cv::Point_<T> > vp;
448  for (std::vector<MxArray>::const_iterator jt = va.begin(); jt != va.end(); ++jt) {
449  vp.push_back(jt->toPoint_<T>());
450  }
451  vvp.push_back(vp);
452  */
453  vvp.push_back(MxArrayToVectorPoint<T>(*it));
454  }
455  return vvp;
456 }
457 
474 template <typename T>
475 std::vector<std::vector<cv::Point3_<T> > > MxArrayToVectorVectorPoint3(const MxArray& arr)
476 {
477  std::vector<MxArray> vva(arr.toVector<MxArray>());
478  std::vector<std::vector<cv::Point3_<T> > > vvp;
479  vvp.reserve(vva.size());
480  for (std::vector<MxArray>::const_iterator it = vva.begin(); it != vva.end(); ++it) {
481  /*
482  std::vector<MxArray> va(it->toVector<MxArray());
483  std::vector<cv::Point3_<T> > vp;
484  for (std::vector<MxArray>::const_iterator jt = va.begin(); jt != va.end(); ++jt) {
485  vp.push_back(jt->toPoint3_<T>());
486  }
487  vvp.push_back(vp);
488  */
489  vvp.push_back(MxArrayToVectorPoint3<T>(*it));
490  }
491  return vvp;
492 }
493 
494 #endif
const ConstMap< std::string, int > FontStyle
Font styles for drawing.
Definition: mexopencv.hpp:130
const ConstMap< std::string, int > ClassNameMap
Translates class name used in MATLAB to equivalent OpenCV depth.
Definition: mexopencv.hpp:27
cv::Point_< T > toPoint_() const
Convert MxArray to Point_<T>.
Definition: MxArray.hpp:1067
std::vector< cv::Vec< T, cn > > MxArrayToVectorVec(const MxArray &arr)
Convert an MxArray to std::vector<cv::Vec<T,cn>>
Definition: mexopencv.hpp:321
const ConstMap< std::string, int > InterpType
Interpolation type map for option processing.
Definition: mexopencv.hpp:71
const ConstMap< std::string, int > ThreshType
Thresholding type map for option processing.
Definition: mexopencv.hpp:79
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
Definition: mexopencv.hpp:145
std::vector< cv::Point_< T > > MxArrayToVectorPoint(const MxArray &arr)
Convert an MxArray to std::vector<cv::Point_<T>>
Definition: mexopencv.hpp:193
const ConstMap< int, std::string > DistTypeInv
Inverse Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:98
const ConstMap< int, std::string > BorderTypeInv
Inverse border type map for option processing.
Definition: mexopencv.hpp:62
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
Definition: MxArray.hpp:1151
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:475
const ConstMap< std::string, int > BorderType
Border type map for option processing.
Definition: mexopencv.hpp:52
MxArray and ConstMap declaration.
cv::Vec< T, cn > toVec() const
Convert MxArray to Vec<T,cn>.
Definition: MxArray.hpp:1114
const ConstMap< std::string, int > LineType
Line type for drawing.
Definition: mexopencv.hpp:109
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
const ConstMap< std::string, int > DistType
Distance types for Distance Transform and M-estimators.
Definition: mexopencv.hpp:87
cv::Point3_< T > toPoint3_() const
Convert MxArray to Point3_<T>.
Definition: MxArray.hpp:1075
const ConstMap< std::string, int > ThicknessType
Thickness type for drawing.
Definition: mexopencv.hpp:115
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Definition: mexopencv.hpp:42
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:439
cv::Matx< T, m, n > toMatx() const
Convert MxArray to Matx<T,m,n>.
Definition: MxArray.hpp:1131
mwSize numel() const
Number of elements in an array.
Definition: MxArray.hpp:546
cv::Rect_< T > toRect_() const
Convert MxArray to Rect_<T>.
Definition: MxArray.hpp:1091
std::vector< std::vector< T > > MxArrayToVectorVectorPrimitive(const MxArray &arr)
Convert an MxArray to std::vector<std::vector<T>>
Definition: mexopencv.hpp:406
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
std::vector< cv::Point3_< T > > MxArrayToVectorPoint3(const MxArray &arr)
Convert an MxArray to std::vector<cv::Point3_<T>>
Definition: mexopencv.hpp:235
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:363
const ConstMap< std::string, int > NormType
Norm type map for option processing.
Definition: mexopencv.hpp:135
cv::Mat toMat(int depth=CV_USRTYPE1, bool transpose=true) const
Convert MxArray to cv::Mat.
Definition: MxArray.cpp:529
std::vector< cv::Rect_< T > > MxArrayToVectorRect(const MxArray &arr)
Convert an MxArray to std::vector<cv::Rect_<T>>
Definition: mexopencv.hpp:278
bool isNumeric() const
Determine whether array is numeric.
Definition: MxArray.hpp:695
const ConstMap< std::string, int > FontFace
Font faces for drawing.
Definition: mexopencv.hpp:119
bool isCell() const
Determine whether input is cell array.
Definition: MxArray.hpp:610