matx.hpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_MATX_HPP
  44. #define OPENCV_CORE_MATX_HPP
  45. #ifndef __cplusplus
  46. # error matx.hpp header must be compiled as C++
  47. #endif
  48. #include "opencv2/core/cvdef.h"
  49. #include "opencv2/core/base.hpp"
  50. #include "opencv2/core/traits.hpp"
  51. #include "opencv2/core/saturate.hpp"
  52. #ifdef CV_CXX11
  53. #include <initializer_list>
  54. #endif
  55. namespace cv
  56. {
  57. //! @addtogroup core_basic
  58. //! @{
  59. ////////////////////////////// Small Matrix ///////////////////////////
  60. //! @cond IGNORED
  61. // FIXIT Remove this (especially CV_EXPORTS modifier)
  62. struct CV_EXPORTS Matx_AddOp { Matx_AddOp() {} Matx_AddOp(const Matx_AddOp&) {} };
  63. struct CV_EXPORTS Matx_SubOp { Matx_SubOp() {} Matx_SubOp(const Matx_SubOp&) {} };
  64. struct CV_EXPORTS Matx_ScaleOp { Matx_ScaleOp() {} Matx_ScaleOp(const Matx_ScaleOp&) {} };
  65. struct CV_EXPORTS Matx_MulOp { Matx_MulOp() {} Matx_MulOp(const Matx_MulOp&) {} };
  66. struct CV_EXPORTS Matx_DivOp { Matx_DivOp() {} Matx_DivOp(const Matx_DivOp&) {} };
  67. struct CV_EXPORTS Matx_MatMulOp { Matx_MatMulOp() {} Matx_MatMulOp(const Matx_MatMulOp&) {} };
  68. struct CV_EXPORTS Matx_TOp { Matx_TOp() {} Matx_TOp(const Matx_TOp&) {} };
  69. //! @endcond
  70. /** @brief Template class for small matrices whose type and size are known at compilation time
  71. If you need a more flexible type, use Mat . The elements of the matrix M are accessible using the
  72. M(i,j) notation. Most of the common matrix operations (see also @ref MatrixExpressions ) are
  73. available. To do an operation on Matx that is not implemented, you can easily convert the matrix to
  74. Mat and backwards:
  75. @code{.cpp}
  76. Matx33f m(1, 2, 3,
  77. 4, 5, 6,
  78. 7, 8, 9);
  79. cout << sum(Mat(m*m.t())) << endl;
  80. @endcode
  81. Except of the plain constructor which takes a list of elements, Matx can be initialized from a C-array:
  82. @code{.cpp}
  83. float values[] = { 1, 2, 3};
  84. Matx31f m(values);
  85. @endcode
  86. In case if C++11 features are available, std::initializer_list can be also used to initialize Matx:
  87. @code{.cpp}
  88. Matx31f m = { 1, 2, 3};
  89. @endcode
  90. */
  91. template<typename _Tp, int m, int n> class Matx
  92. {
  93. public:
  94. enum {
  95. rows = m,
  96. cols = n,
  97. channels = rows*cols,
  98. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  99. depth = traits::Type<_Tp>::value,
  100. type = CV_MAKETYPE(depth, channels),
  101. #endif
  102. shortdim = (m < n ? m : n)
  103. };
  104. typedef _Tp value_type;
  105. typedef Matx<_Tp, m, n> mat_type;
  106. typedef Matx<_Tp, shortdim, 1> diag_type;
  107. //! default constructor
  108. Matx();
  109. explicit Matx(_Tp v0); //!< 1x1 matrix
  110. Matx(_Tp v0, _Tp v1); //!< 1x2 or 2x1 matrix
  111. Matx(_Tp v0, _Tp v1, _Tp v2); //!< 1x3 or 3x1 matrix
  112. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 1x4, 2x2 or 4x1 matrix
  113. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 1x5 or 5x1 matrix
  114. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 1x6, 2x3, 3x2 or 6x1 matrix
  115. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 1x7 or 7x1 matrix
  116. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 1x8, 2x4, 4x2 or 8x1 matrix
  117. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 1x9, 3x3 or 9x1 matrix
  118. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 1x10, 2x5 or 5x2 or 10x1 matrix
  119. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
  120. _Tp v4, _Tp v5, _Tp v6, _Tp v7,
  121. _Tp v8, _Tp v9, _Tp v10, _Tp v11); //!< 1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 matrix
  122. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
  123. _Tp v4, _Tp v5, _Tp v6, _Tp v7,
  124. _Tp v8, _Tp v9, _Tp v10, _Tp v11,
  125. _Tp v12, _Tp v13); //!< 1x14, 2x7, 7x2 or 14x1 matrix
  126. Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
  127. _Tp v4, _Tp v5, _Tp v6, _Tp v7,
  128. _Tp v8, _Tp v9, _Tp v10, _Tp v11,
  129. _Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix
  130. explicit Matx(const _Tp* vals); //!< initialize from a plain array
  131. #ifdef CV_CXX11
  132. Matx(std::initializer_list<_Tp>); //!< initialize from an initializer list
  133. #endif
  134. static Matx all(_Tp alpha);
  135. static Matx zeros();
  136. static Matx ones();
  137. static Matx eye();
  138. static Matx diag(const diag_type& d);
  139. static Matx randu(_Tp a, _Tp b);
  140. static Matx randn(_Tp a, _Tp b);
  141. //! dot product computed with the default precision
  142. _Tp dot(const Matx<_Tp, m, n>& v) const;
  143. //! dot product computed in double-precision arithmetics
  144. double ddot(const Matx<_Tp, m, n>& v) const;
  145. //! conversion to another data type
  146. template<typename T2> operator Matx<T2, m, n>() const;
  147. //! change the matrix shape
  148. template<int m1, int n1> Matx<_Tp, m1, n1> reshape() const;
  149. //! extract part of the matrix
  150. template<int m1, int n1> Matx<_Tp, m1, n1> get_minor(int base_row, int base_col) const;
  151. //! extract the matrix row
  152. Matx<_Tp, 1, n> row(int i) const;
  153. //! extract the matrix column
  154. Matx<_Tp, m, 1> col(int i) const;
  155. //! extract the matrix diagonal
  156. diag_type diag() const;
  157. //! transpose the matrix
  158. Matx<_Tp, n, m> t() const;
  159. //! invert the matrix
  160. Matx<_Tp, n, m> inv(int method=DECOMP_LU, bool *p_is_ok = NULL) const;
  161. //! solve linear system
  162. template<int l> Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const;
  163. Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const;
  164. //! multiply two matrices element-wise
  165. Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const;
  166. //! divide two matrices element-wise
  167. Matx<_Tp, m, n> div(const Matx<_Tp, m, n>& a) const;
  168. //! element access
  169. const _Tp& operator ()(int row, int col) const;
  170. _Tp& operator ()(int row, int col);
  171. //! 1D element access
  172. const _Tp& operator ()(int i) const;
  173. _Tp& operator ()(int i);
  174. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp);
  175. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp);
  176. template<typename _T2> Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp);
  177. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp);
  178. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp);
  179. template<int l> Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp);
  180. Matx(const Matx<_Tp, n, m>& a, Matx_TOp);
  181. _Tp val[m*n]; //< matrix elements
  182. };
  183. typedef Matx<float, 1, 2> Matx12f;
  184. typedef Matx<double, 1, 2> Matx12d;
  185. typedef Matx<float, 1, 3> Matx13f;
  186. typedef Matx<double, 1, 3> Matx13d;
  187. typedef Matx<float, 1, 4> Matx14f;
  188. typedef Matx<double, 1, 4> Matx14d;
  189. typedef Matx<float, 1, 6> Matx16f;
  190. typedef Matx<double, 1, 6> Matx16d;
  191. typedef Matx<float, 2, 1> Matx21f;
  192. typedef Matx<double, 2, 1> Matx21d;
  193. typedef Matx<float, 3, 1> Matx31f;
  194. typedef Matx<double, 3, 1> Matx31d;
  195. typedef Matx<float, 4, 1> Matx41f;
  196. typedef Matx<double, 4, 1> Matx41d;
  197. typedef Matx<float, 6, 1> Matx61f;
  198. typedef Matx<double, 6, 1> Matx61d;
  199. typedef Matx<float, 2, 2> Matx22f;
  200. typedef Matx<double, 2, 2> Matx22d;
  201. typedef Matx<float, 2, 3> Matx23f;
  202. typedef Matx<double, 2, 3> Matx23d;
  203. typedef Matx<float, 3, 2> Matx32f;
  204. typedef Matx<double, 3, 2> Matx32d;
  205. typedef Matx<float, 3, 3> Matx33f;
  206. typedef Matx<double, 3, 3> Matx33d;
  207. typedef Matx<float, 3, 4> Matx34f;
  208. typedef Matx<double, 3, 4> Matx34d;
  209. typedef Matx<float, 4, 3> Matx43f;
  210. typedef Matx<double, 4, 3> Matx43d;
  211. typedef Matx<float, 4, 4> Matx44f;
  212. typedef Matx<double, 4, 4> Matx44d;
  213. typedef Matx<float, 6, 6> Matx66f;
  214. typedef Matx<double, 6, 6> Matx66d;
  215. /*!
  216. traits
  217. */
  218. template<typename _Tp, int m, int n> class DataType< Matx<_Tp, m, n> >
  219. {
  220. public:
  221. typedef Matx<_Tp, m, n> value_type;
  222. typedef Matx<typename DataType<_Tp>::work_type, m, n> work_type;
  223. typedef _Tp channel_type;
  224. typedef value_type vec_type;
  225. enum { generic_type = 0,
  226. channels = m * n,
  227. fmt = traits::SafeFmt<channel_type>::fmt + ((channels - 1) << 8)
  228. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  229. ,depth = DataType<channel_type>::depth
  230. ,type = CV_MAKETYPE(depth, channels)
  231. #endif
  232. };
  233. };
  234. namespace traits {
  235. template<typename _Tp, int m, int n>
  236. struct Depth< Matx<_Tp, m, n> > { enum { value = Depth<_Tp>::value }; };
  237. template<typename _Tp, int m, int n>
  238. struct Type< Matx<_Tp, m, n> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, n*m) }; };
  239. } // namespace
  240. /** @brief Comma-separated Matrix Initializer
  241. */
  242. template<typename _Tp, int m, int n> class MatxCommaInitializer
  243. {
  244. public:
  245. MatxCommaInitializer(Matx<_Tp, m, n>* _mtx);
  246. template<typename T2> MatxCommaInitializer<_Tp, m, n>& operator , (T2 val);
  247. Matx<_Tp, m, n> operator *() const;
  248. Matx<_Tp, m, n>* dst;
  249. int idx;
  250. };
  251. /*
  252. Utility methods
  253. */
  254. template<typename _Tp, int m> static double determinant(const Matx<_Tp, m, m>& a);
  255. template<typename _Tp, int m, int n> static double trace(const Matx<_Tp, m, n>& a);
  256. template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M);
  257. template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M, int normType);
  258. /////////////////////// Vec (used as element of multi-channel images /////////////////////
  259. /** @brief Template class for short numerical vectors, a partial case of Matx
  260. This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) on which you
  261. can perform basic arithmetical operations, access individual elements using [] operator etc. The
  262. vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., which
  263. elements are dynamically allocated in the heap.
  264. The template takes 2 parameters:
  265. @tparam _Tp element type
  266. @tparam cn the number of elements
  267. In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
  268. for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
  269. It is possible to convert Vec\<T,2\> to/from Point_, Vec\<T,3\> to/from Point3_ , and Vec\<T,4\>
  270. to CvScalar or Scalar_. Use operator[] to access the elements of Vec.
  271. All the expected vector operations are also implemented:
  272. - v1 = v2 + v3
  273. - v1 = v2 - v3
  274. - v1 = v2 \* scale
  275. - v1 = scale \* v2
  276. - v1 = -v2
  277. - v1 += v2 and other augmenting operations
  278. - v1 == v2, v1 != v2
  279. - norm(v1) (euclidean norm)
  280. The Vec class is commonly used to describe pixel types of multi-channel arrays. See Mat for details.
  281. */
  282. template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>
  283. {
  284. public:
  285. typedef _Tp value_type;
  286. enum {
  287. channels = cn,
  288. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  289. depth = Matx<_Tp, cn, 1>::depth,
  290. type = CV_MAKETYPE(depth, channels),
  291. #endif
  292. _dummy_enum_finalizer = 0
  293. };
  294. //! default constructor
  295. Vec();
  296. Vec(_Tp v0); //!< 1-element vector constructor
  297. Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor
  298. Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor
  299. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor
  300. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor
  301. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor
  302. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor
  303. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor
  304. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor
  305. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 10-element vector constructor
  306. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13); //!< 14-element vector constructor
  307. explicit Vec(const _Tp* values);
  308. #ifdef CV_CXX11
  309. Vec(std::initializer_list<_Tp>);
  310. #endif
  311. Vec(const Vec<_Tp, cn>& v);
  312. static Vec all(_Tp alpha);
  313. //! per-element multiplication
  314. Vec mul(const Vec<_Tp, cn>& v) const;
  315. //! conjugation (makes sense for complex numbers and quaternions)
  316. Vec conj() const;
  317. /*!
  318. cross product of the two 3D vectors.
  319. For other dimensionalities the exception is raised
  320. */
  321. Vec cross(const Vec& v) const;
  322. //! conversion to another data type
  323. template<typename T2> operator Vec<T2, cn>() const;
  324. /*! element access */
  325. const _Tp& operator [](int i) const;
  326. _Tp& operator[](int i);
  327. const _Tp& operator ()(int i) const;
  328. _Tp& operator ()(int i);
  329. #ifdef CV_CXX11
  330. Vec<_Tp, cn>& operator=(const Vec<_Tp, cn>& rhs) = default;
  331. #endif
  332. Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp);
  333. Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp);
  334. template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);
  335. };
  336. /** @name Shorter aliases for the most popular specializations of Vec<T,n>
  337. @{
  338. */
  339. typedef Vec<uchar, 2> Vec2b;
  340. typedef Vec<uchar, 3> Vec3b;
  341. typedef Vec<uchar, 4> Vec4b;
  342. typedef Vec<short, 2> Vec2s;
  343. typedef Vec<short, 3> Vec3s;
  344. typedef Vec<short, 4> Vec4s;
  345. typedef Vec<ushort, 2> Vec2w;
  346. typedef Vec<ushort, 3> Vec3w;
  347. typedef Vec<ushort, 4> Vec4w;
  348. typedef Vec<int, 2> Vec2i;
  349. typedef Vec<int, 3> Vec3i;
  350. typedef Vec<int, 4> Vec4i;
  351. typedef Vec<int, 6> Vec6i;
  352. typedef Vec<int, 8> Vec8i;
  353. typedef Vec<float, 2> Vec2f;
  354. typedef Vec<float, 3> Vec3f;
  355. typedef Vec<float, 4> Vec4f;
  356. typedef Vec<float, 6> Vec6f;
  357. typedef Vec<double, 2> Vec2d;
  358. typedef Vec<double, 3> Vec3d;
  359. typedef Vec<double, 4> Vec4d;
  360. typedef Vec<double, 6> Vec6d;
  361. /** @} */
  362. /*!
  363. traits
  364. */
  365. template<typename _Tp, int cn> class DataType< Vec<_Tp, cn> >
  366. {
  367. public:
  368. typedef Vec<_Tp, cn> value_type;
  369. typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
  370. typedef _Tp channel_type;
  371. typedef value_type vec_type;
  372. enum { generic_type = 0,
  373. channels = cn,
  374. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  375. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  376. depth = DataType<channel_type>::depth,
  377. type = CV_MAKETYPE(depth, channels),
  378. #endif
  379. _dummy_enum_finalizer = 0
  380. };
  381. };
  382. namespace traits {
  383. template<typename _Tp, int cn>
  384. struct Depth< Vec<_Tp, cn> > { enum { value = Depth<_Tp>::value }; };
  385. template<typename _Tp, int cn>
  386. struct Type< Vec<_Tp, cn> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, cn) }; };
  387. } // namespace
  388. /** @brief Comma-separated Vec Initializer
  389. */
  390. template<typename _Tp, int m> class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1>
  391. {
  392. public:
  393. VecCommaInitializer(Vec<_Tp, m>* _vec);
  394. template<typename T2> VecCommaInitializer<_Tp, m>& operator , (T2 val);
  395. Vec<_Tp, m> operator *() const;
  396. };
  397. template<typename _Tp, int cn> static Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v);
  398. //! @} core_basic
  399. //! @cond IGNORED
  400. ///////////////////////////////////// helper classes /////////////////////////////////////
  401. namespace internal
  402. {
  403. template<typename _Tp, int m> struct Matx_DetOp
  404. {
  405. double operator ()(const Matx<_Tp, m, m>& a) const
  406. {
  407. Matx<_Tp, m, m> temp = a;
  408. double p = LU(temp.val, m*sizeof(_Tp), m, 0, 0, 0);
  409. if( p == 0 )
  410. return p;
  411. for( int i = 0; i < m; i++ )
  412. p *= temp(i, i);
  413. return p;
  414. }
  415. };
  416. template<typename _Tp> struct Matx_DetOp<_Tp, 1>
  417. {
  418. double operator ()(const Matx<_Tp, 1, 1>& a) const
  419. {
  420. return a(0,0);
  421. }
  422. };
  423. template<typename _Tp> struct Matx_DetOp<_Tp, 2>
  424. {
  425. double operator ()(const Matx<_Tp, 2, 2>& a) const
  426. {
  427. return a(0,0)*a(1,1) - a(0,1)*a(1,0);
  428. }
  429. };
  430. template<typename _Tp> struct Matx_DetOp<_Tp, 3>
  431. {
  432. double operator ()(const Matx<_Tp, 3, 3>& a) const
  433. {
  434. return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
  435. a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
  436. a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1));
  437. }
  438. };
  439. template<typename _Tp> Vec<_Tp, 2> inline conjugate(const Vec<_Tp, 2>& v)
  440. {
  441. return Vec<_Tp, 2>(v[0], -v[1]);
  442. }
  443. template<typename _Tp> Vec<_Tp, 4> inline conjugate(const Vec<_Tp, 4>& v)
  444. {
  445. return Vec<_Tp, 4>(v[0], -v[1], -v[2], -v[3]);
  446. }
  447. } // internal
  448. ////////////////////////////////// Matx Implementation ///////////////////////////////////
  449. template<typename _Tp, int m, int n> inline
  450. Matx<_Tp, m, n>::Matx()
  451. {
  452. for(int i = 0; i < channels; i++) val[i] = _Tp(0);
  453. }
  454. template<typename _Tp, int m, int n> inline
  455. Matx<_Tp, m, n>::Matx(_Tp v0)
  456. {
  457. val[0] = v0;
  458. for(int i = 1; i < channels; i++) val[i] = _Tp(0);
  459. }
  460. template<typename _Tp, int m, int n> inline
  461. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1)
  462. {
  463. CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
  464. val[0] = v0; val[1] = v1;
  465. for(int i = 2; i < channels; i++) val[i] = _Tp(0);
  466. }
  467. template<typename _Tp, int m, int n> inline
  468. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2)
  469. {
  470. CV_StaticAssert(channels >= 3, "Matx should have at least 3 elements.");
  471. val[0] = v0; val[1] = v1; val[2] = v2;
  472. for(int i = 3; i < channels; i++) val[i] = _Tp(0);
  473. }
  474. template<typename _Tp, int m, int n> inline
  475. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
  476. {
  477. CV_StaticAssert(channels >= 4, "Matx should have at least 4 elements.");
  478. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  479. for(int i = 4; i < channels; i++) val[i] = _Tp(0);
  480. }
  481. template<typename _Tp, int m, int n> inline
  482. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
  483. {
  484. CV_StaticAssert(channels >= 5, "Matx should have at least 5 elements.");
  485. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
  486. for(int i = 5; i < channels; i++) val[i] = _Tp(0);
  487. }
  488. template<typename _Tp, int m, int n> inline
  489. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
  490. {
  491. CV_StaticAssert(channels >= 6, "Matx should have at least 6 elements.");
  492. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  493. val[4] = v4; val[5] = v5;
  494. for(int i = 6; i < channels; i++) val[i] = _Tp(0);
  495. }
  496. template<typename _Tp, int m, int n> inline
  497. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
  498. {
  499. CV_StaticAssert(channels >= 7, "Matx should have at least 7 elements.");
  500. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  501. val[4] = v4; val[5] = v5; val[6] = v6;
  502. for(int i = 7; i < channels; i++) val[i] = _Tp(0);
  503. }
  504. template<typename _Tp, int m, int n> inline
  505. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
  506. {
  507. CV_StaticAssert(channels >= 8, "Matx should have at least 8 elements.");
  508. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  509. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  510. for(int i = 8; i < channels; i++) val[i] = _Tp(0);
  511. }
  512. template<typename _Tp, int m, int n> inline
  513. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
  514. {
  515. CV_StaticAssert(channels >= 9, "Matx should have at least 9 elements.");
  516. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  517. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  518. val[8] = v8;
  519. for(int i = 9; i < channels; i++) val[i] = _Tp(0);
  520. }
  521. template<typename _Tp, int m, int n> inline
  522. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)
  523. {
  524. CV_StaticAssert(channels >= 10, "Matx should have at least 10 elements.");
  525. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  526. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  527. val[8] = v8; val[9] = v9;
  528. for(int i = 10; i < channels; i++) val[i] = _Tp(0);
  529. }
  530. template<typename _Tp, int m, int n> inline
  531. Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11)
  532. {
  533. CV_StaticAssert(channels >= 12, "Matx should have at least 12 elements.");
  534. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  535. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  536. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  537. for(int i = 12; i < channels; i++) val[i] = _Tp(0);
  538. }
  539. template<typename _Tp, int m, int n> inline
  540. Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13)
  541. {
  542. CV_StaticAssert(channels >= 14, "Matx should have at least 14 elements.");
  543. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  544. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  545. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  546. val[12] = v12; val[13] = v13;
  547. for (int i = 14; i < channels; i++) val[i] = _Tp(0);
  548. }
  549. template<typename _Tp, int m, int n> inline
  550. Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13, _Tp v14, _Tp v15)
  551. {
  552. CV_StaticAssert(channels >= 16, "Matx should have at least 16 elements.");
  553. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  554. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  555. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  556. val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15;
  557. for(int i = 16; i < channels; i++) val[i] = _Tp(0);
  558. }
  559. template<typename _Tp, int m, int n> inline
  560. Matx<_Tp, m, n>::Matx(const _Tp* values)
  561. {
  562. for( int i = 0; i < channels; i++ ) val[i] = values[i];
  563. }
  564. #ifdef CV_CXX11
  565. template<typename _Tp, int m, int n> inline
  566. Matx<_Tp, m, n>::Matx(std::initializer_list<_Tp> list)
  567. {
  568. CV_DbgAssert(list.size() == channels);
  569. int i = 0;
  570. for(const auto& elem : list)
  571. {
  572. val[i++] = elem;
  573. }
  574. }
  575. #endif
  576. template<typename _Tp, int m, int n> inline
  577. Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha)
  578. {
  579. Matx<_Tp, m, n> M;
  580. for( int i = 0; i < m*n; i++ ) M.val[i] = alpha;
  581. return M;
  582. }
  583. template<typename _Tp, int m, int n> inline
  584. Matx<_Tp,m,n> Matx<_Tp,m,n>::zeros()
  585. {
  586. return all(0);
  587. }
  588. template<typename _Tp, int m, int n> inline
  589. Matx<_Tp,m,n> Matx<_Tp,m,n>::ones()
  590. {
  591. return all(1);
  592. }
  593. template<typename _Tp, int m, int n> inline
  594. Matx<_Tp,m,n> Matx<_Tp,m,n>::eye()
  595. {
  596. Matx<_Tp,m,n> M;
  597. for(int i = 0; i < shortdim; i++)
  598. M(i,i) = 1;
  599. return M;
  600. }
  601. template<typename _Tp, int m, int n> inline
  602. _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const
  603. {
  604. _Tp s = 0;
  605. for( int i = 0; i < channels; i++ ) s += val[i]*M.val[i];
  606. return s;
  607. }
  608. template<typename _Tp, int m, int n> inline
  609. double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const
  610. {
  611. double s = 0;
  612. for( int i = 0; i < channels; i++ ) s += (double)val[i]*M.val[i];
  613. return s;
  614. }
  615. template<typename _Tp, int m, int n> inline
  616. Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const typename Matx<_Tp,m,n>::diag_type& d)
  617. {
  618. Matx<_Tp,m,n> M;
  619. for(int i = 0; i < shortdim; i++)
  620. M(i,i) = d(i, 0);
  621. return M;
  622. }
  623. template<typename _Tp, int m, int n> template<typename T2>
  624. inline Matx<_Tp, m, n>::operator Matx<T2, m, n>() const
  625. {
  626. Matx<T2, m, n> M;
  627. for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast<T2>(val[i]);
  628. return M;
  629. }
  630. template<typename _Tp, int m, int n> template<int m1, int n1> inline
  631. Matx<_Tp, m1, n1> Matx<_Tp, m, n>::reshape() const
  632. {
  633. CV_StaticAssert(m1*n1 == m*n, "Input and destnarion matrices must have the same number of elements");
  634. return (const Matx<_Tp, m1, n1>&)*this;
  635. }
  636. template<typename _Tp, int m, int n>
  637. template<int m1, int n1> inline
  638. Matx<_Tp, m1, n1> Matx<_Tp, m, n>::get_minor(int base_row, int base_col) const
  639. {
  640. CV_DbgAssert(0 <= base_row && base_row+m1 <= m && 0 <= base_col && base_col+n1 <= n);
  641. Matx<_Tp, m1, n1> s;
  642. for( int di = 0; di < m1; di++ )
  643. for( int dj = 0; dj < n1; dj++ )
  644. s(di, dj) = (*this)(base_row+di, base_col+dj);
  645. return s;
  646. }
  647. template<typename _Tp, int m, int n> inline
  648. Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const
  649. {
  650. CV_DbgAssert((unsigned)i < (unsigned)m);
  651. return Matx<_Tp, 1, n>(&val[i*n]);
  652. }
  653. template<typename _Tp, int m, int n> inline
  654. Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const
  655. {
  656. CV_DbgAssert((unsigned)j < (unsigned)n);
  657. Matx<_Tp, m, 1> v;
  658. for( int i = 0; i < m; i++ )
  659. v.val[i] = val[i*n + j];
  660. return v;
  661. }
  662. template<typename _Tp, int m, int n> inline
  663. typename Matx<_Tp, m, n>::diag_type Matx<_Tp, m, n>::diag() const
  664. {
  665. diag_type d;
  666. for( int i = 0; i < shortdim; i++ )
  667. d.val[i] = val[i*n + i];
  668. return d;
  669. }
  670. template<typename _Tp, int m, int n> inline
  671. const _Tp& Matx<_Tp, m, n>::operator()(int row_idx, int col_idx) const
  672. {
  673. CV_DbgAssert( (unsigned)row_idx < (unsigned)m && (unsigned)col_idx < (unsigned)n );
  674. return this->val[row_idx*n + col_idx];
  675. }
  676. template<typename _Tp, int m, int n> inline
  677. _Tp& Matx<_Tp, m, n>::operator ()(int row_idx, int col_idx)
  678. {
  679. CV_DbgAssert( (unsigned)row_idx < (unsigned)m && (unsigned)col_idx < (unsigned)n );
  680. return val[row_idx*n + col_idx];
  681. }
  682. template<typename _Tp, int m, int n> inline
  683. const _Tp& Matx<_Tp, m, n>::operator ()(int i) const
  684. {
  685. CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
  686. CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
  687. return val[i];
  688. }
  689. template<typename _Tp, int m, int n> inline
  690. _Tp& Matx<_Tp, m, n>::operator ()(int i)
  691. {
  692. CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
  693. CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
  694. return val[i];
  695. }
  696. template<typename _Tp, int m, int n> inline
  697. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp)
  698. {
  699. for( int i = 0; i < channels; i++ )
  700. val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]);
  701. }
  702. template<typename _Tp, int m, int n> inline
  703. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp)
  704. {
  705. for( int i = 0; i < channels; i++ )
  706. val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]);
  707. }
  708. template<typename _Tp, int m, int n> template<typename _T2> inline
  709. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp)
  710. {
  711. for( int i = 0; i < channels; i++ )
  712. val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  713. }
  714. template<typename _Tp, int m, int n> inline
  715. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp)
  716. {
  717. for( int i = 0; i < channels; i++ )
  718. val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]);
  719. }
  720. template<typename _Tp, int m, int n> inline
  721. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp)
  722. {
  723. for( int i = 0; i < channels; i++ )
  724. val[i] = saturate_cast<_Tp>(a.val[i] / b.val[i]);
  725. }
  726. template<typename _Tp, int m, int n> template<int l> inline
  727. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp)
  728. {
  729. for( int i = 0; i < m; i++ )
  730. for( int j = 0; j < n; j++ )
  731. {
  732. _Tp s = 0;
  733. for( int k = 0; k < l; k++ )
  734. s += a(i, k) * b(k, j);
  735. val[i*n + j] = s;
  736. }
  737. }
  738. template<typename _Tp, int m, int n> inline
  739. Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp)
  740. {
  741. for( int i = 0; i < m; i++ )
  742. for( int j = 0; j < n; j++ )
  743. val[i*n + j] = a(j, i);
  744. }
  745. template<typename _Tp, int m, int n> inline
  746. Matx<_Tp, m, n> Matx<_Tp, m, n>::mul(const Matx<_Tp, m, n>& a) const
  747. {
  748. return Matx<_Tp, m, n>(*this, a, Matx_MulOp());
  749. }
  750. template<typename _Tp, int m, int n> inline
  751. Matx<_Tp, m, n> Matx<_Tp, m, n>::div(const Matx<_Tp, m, n>& a) const
  752. {
  753. return Matx<_Tp, m, n>(*this, a, Matx_DivOp());
  754. }
  755. template<typename _Tp, int m, int n> inline
  756. Matx<_Tp, n, m> Matx<_Tp, m, n>::t() const
  757. {
  758. return Matx<_Tp, n, m>(*this, Matx_TOp());
  759. }
  760. template<typename _Tp, int m, int n> inline
  761. Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const
  762. {
  763. Matx<_Tp, n, 1> x = solve((const Matx<_Tp, m, 1>&)(rhs), method);
  764. return (Vec<_Tp, n>&)(x);
  765. }
  766. template<typename _Tp, int m> static inline
  767. double determinant(const Matx<_Tp, m, m>& a)
  768. {
  769. return cv::internal::Matx_DetOp<_Tp, m>()(a);
  770. }
  771. template<typename _Tp, int m, int n> static inline
  772. double trace(const Matx<_Tp, m, n>& a)
  773. {
  774. _Tp s = 0;
  775. for( int i = 0; i < std::min(m, n); i++ )
  776. s += a(i,i);
  777. return s;
  778. }
  779. template<typename _Tp, int m, int n> static inline
  780. double norm(const Matx<_Tp, m, n>& M)
  781. {
  782. return std::sqrt(normL2Sqr<_Tp, double>(M.val, m*n));
  783. }
  784. template<typename _Tp, int m, int n> static inline
  785. double norm(const Matx<_Tp, m, n>& M, int normType)
  786. {
  787. switch(normType) {
  788. case NORM_INF:
  789. return (double)normInf<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  790. case NORM_L1:
  791. return (double)normL1<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  792. case NORM_L2SQR:
  793. return (double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  794. default:
  795. case NORM_L2:
  796. return std::sqrt((double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n));
  797. }
  798. }
  799. //////////////////////////////// matx comma initializer //////////////////////////////////
  800. template<typename _Tp, typename _T2, int m, int n> static inline
  801. MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val)
  802. {
  803. MatxCommaInitializer<_Tp, m, n> commaInitializer((Matx<_Tp, m, n>*)&mtx);
  804. return (commaInitializer, val);
  805. }
  806. template<typename _Tp, int m, int n> inline
  807. MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx)
  808. : dst(_mtx), idx(0)
  809. {}
  810. template<typename _Tp, int m, int n> template<typename _T2> inline
  811. MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value)
  812. {
  813. CV_DbgAssert( idx < m*n );
  814. dst->val[idx++] = saturate_cast<_Tp>(value);
  815. return *this;
  816. }
  817. template<typename _Tp, int m, int n> inline
  818. Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const
  819. {
  820. CV_DbgAssert( idx == n*m );
  821. return *dst;
  822. }
  823. /////////////////////////////////// Vec Implementation ///////////////////////////////////
  824. template<typename _Tp, int cn> inline
  825. Vec<_Tp, cn>::Vec() {}
  826. template<typename _Tp, int cn> inline
  827. Vec<_Tp, cn>::Vec(_Tp v0)
  828. : Matx<_Tp, cn, 1>(v0) {}
  829. template<typename _Tp, int cn> inline
  830. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1)
  831. : Matx<_Tp, cn, 1>(v0, v1) {}
  832. template<typename _Tp, int cn> inline
  833. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
  834. : Matx<_Tp, cn, 1>(v0, v1, v2) {}
  835. template<typename _Tp, int cn> inline
  836. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
  837. : Matx<_Tp, cn, 1>(v0, v1, v2, v3) {}
  838. template<typename _Tp, int cn> inline
  839. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
  840. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) {}
  841. template<typename _Tp, int cn> inline
  842. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
  843. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) {}
  844. template<typename _Tp, int cn> inline
  845. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
  846. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) {}
  847. template<typename _Tp, int cn> inline
  848. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
  849. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) {}
  850. template<typename _Tp, int cn> inline
  851. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
  852. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) {}
  853. template<typename _Tp, int cn> inline
  854. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)
  855. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {}
  856. template<typename _Tp, int cn> inline
  857. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13)
  858. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) {}
  859. template<typename _Tp, int cn> inline
  860. Vec<_Tp, cn>::Vec(const _Tp* values)
  861. : Matx<_Tp, cn, 1>(values) {}
  862. #ifdef CV_CXX11
  863. template<typename _Tp, int cn> inline
  864. Vec<_Tp, cn>::Vec(std::initializer_list<_Tp> list)
  865. : Matx<_Tp, cn, 1>(list) {}
  866. #endif
  867. template<typename _Tp, int cn> inline
  868. Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m)
  869. : Matx<_Tp, cn, 1>(m.val) {}
  870. template<typename _Tp, int cn> inline
  871. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp op)
  872. : Matx<_Tp, cn, 1>(a, b, op) {}
  873. template<typename _Tp, int cn> inline
  874. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp op)
  875. : Matx<_Tp, cn, 1>(a, b, op) {}
  876. template<typename _Tp, int cn> template<typename _T2> inline
  877. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp op)
  878. : Matx<_Tp, cn, 1>(a, alpha, op) {}
  879. template<typename _Tp, int cn> inline
  880. Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha)
  881. {
  882. Vec v;
  883. for( int i = 0; i < cn; i++ ) v.val[i] = alpha;
  884. return v;
  885. }
  886. template<typename _Tp, int cn> inline
  887. Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const
  888. {
  889. Vec<_Tp, cn> w;
  890. for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(this->val[i]*v.val[i]);
  891. return w;
  892. }
  893. template<> inline
  894. Vec<float, 2> Vec<float, 2>::conj() const
  895. {
  896. return cv::internal::conjugate(*this);
  897. }
  898. template<> inline
  899. Vec<double, 2> Vec<double, 2>::conj() const
  900. {
  901. return cv::internal::conjugate(*this);
  902. }
  903. template<> inline
  904. Vec<float, 4> Vec<float, 4>::conj() const
  905. {
  906. return cv::internal::conjugate(*this);
  907. }
  908. template<> inline
  909. Vec<double, 4> Vec<double, 4>::conj() const
  910. {
  911. return cv::internal::conjugate(*this);
  912. }
  913. template<typename _Tp, int cn> inline
  914. Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>&) const
  915. {
  916. CV_StaticAssert(cn == 3, "for arbitrary-size vector there is no cross-product defined");
  917. return Vec<_Tp, cn>();
  918. }
  919. template<> inline
  920. Vec<float, 3> Vec<float, 3>::cross(const Vec<float, 3>& v) const
  921. {
  922. return Vec<float,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
  923. this->val[2]*v.val[0] - this->val[0]*v.val[2],
  924. this->val[0]*v.val[1] - this->val[1]*v.val[0]);
  925. }
  926. template<> inline
  927. Vec<double, 3> Vec<double, 3>::cross(const Vec<double, 3>& v) const
  928. {
  929. return Vec<double,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
  930. this->val[2]*v.val[0] - this->val[0]*v.val[2],
  931. this->val[0]*v.val[1] - this->val[1]*v.val[0]);
  932. }
  933. template<typename _Tp, int cn> template<typename T2> inline
  934. Vec<_Tp, cn>::operator Vec<T2, cn>() const
  935. {
  936. Vec<T2, cn> v;
  937. for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast<T2>(this->val[i]);
  938. return v;
  939. }
  940. template<typename _Tp, int cn> inline
  941. const _Tp& Vec<_Tp, cn>::operator [](int i) const
  942. {
  943. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  944. return this->val[i];
  945. }
  946. template<typename _Tp, int cn> inline
  947. _Tp& Vec<_Tp, cn>::operator [](int i)
  948. {
  949. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  950. return this->val[i];
  951. }
  952. template<typename _Tp, int cn> inline
  953. const _Tp& Vec<_Tp, cn>::operator ()(int i) const
  954. {
  955. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  956. return this->val[i];
  957. }
  958. template<typename _Tp, int cn> inline
  959. _Tp& Vec<_Tp, cn>::operator ()(int i)
  960. {
  961. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  962. return this->val[i];
  963. }
  964. template<typename _Tp, int cn> inline
  965. Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v)
  966. {
  967. double nv = norm(v);
  968. return v * (nv ? 1./nv : 0.);
  969. }
  970. //////////////////////////////// vec comma initializer //////////////////////////////////
  971. template<typename _Tp, typename _T2, int cn> static inline
  972. VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val)
  973. {
  974. VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec);
  975. return (commaInitializer, val);
  976. }
  977. template<typename _Tp, int cn> inline
  978. VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec)
  979. : MatxCommaInitializer<_Tp, cn, 1>(_vec)
  980. {}
  981. template<typename _Tp, int cn> template<typename _T2> inline
  982. VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value)
  983. {
  984. CV_DbgAssert( this->idx < cn );
  985. this->dst->val[this->idx++] = saturate_cast<_Tp>(value);
  986. return *this;
  987. }
  988. template<typename _Tp, int cn> inline
  989. Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const
  990. {
  991. CV_DbgAssert( this->idx == cn );
  992. return *this->dst;
  993. }
  994. //! @endcond
  995. ///////////////////////////// Matx out-of-class operators ////////////////////////////////
  996. //! @relates cv::Matx
  997. //! @{
  998. template<typename _Tp1, typename _Tp2, int m, int n> static inline
  999. Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
  1000. {
  1001. for( int i = 0; i < m*n; i++ )
  1002. a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
  1003. return a;
  1004. }
  1005. template<typename _Tp1, typename _Tp2, int m, int n> static inline
  1006. Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
  1007. {
  1008. for( int i = 0; i < m*n; i++ )
  1009. a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
  1010. return a;
  1011. }
  1012. template<typename _Tp, int m, int n> static inline
  1013. Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1014. {
  1015. return Matx<_Tp, m, n>(a, b, Matx_AddOp());
  1016. }
  1017. template<typename _Tp, int m, int n> static inline
  1018. Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1019. {
  1020. return Matx<_Tp, m, n>(a, b, Matx_SubOp());
  1021. }
  1022. template<typename _Tp, int m, int n> static inline
  1023. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha)
  1024. {
  1025. for( int i = 0; i < m*n; i++ )
  1026. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1027. return a;
  1028. }
  1029. template<typename _Tp, int m, int n> static inline
  1030. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha)
  1031. {
  1032. for( int i = 0; i < m*n; i++ )
  1033. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1034. return a;
  1035. }
  1036. template<typename _Tp, int m, int n> static inline
  1037. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha)
  1038. {
  1039. for( int i = 0; i < m*n; i++ )
  1040. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1041. return a;
  1042. }
  1043. template<typename _Tp, int m, int n> static inline
  1044. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int alpha)
  1045. {
  1046. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1047. }
  1048. template<typename _Tp, int m, int n> static inline
  1049. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, float alpha)
  1050. {
  1051. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1052. }
  1053. template<typename _Tp, int m, int n> static inline
  1054. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, double alpha)
  1055. {
  1056. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1057. }
  1058. template<typename _Tp, int m, int n> static inline
  1059. Matx<_Tp, m, n> operator * (int alpha, const Matx<_Tp, m, n>& a)
  1060. {
  1061. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1062. }
  1063. template<typename _Tp, int m, int n> static inline
  1064. Matx<_Tp, m, n> operator * (float alpha, const Matx<_Tp, m, n>& a)
  1065. {
  1066. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1067. }
  1068. template<typename _Tp, int m, int n> static inline
  1069. Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a)
  1070. {
  1071. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1072. }
  1073. template<typename _Tp, int m, int n> static inline
  1074. Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a)
  1075. {
  1076. return Matx<_Tp, m, n>(a, -1, Matx_ScaleOp());
  1077. }
  1078. template<typename _Tp, int m, int n, int l> static inline
  1079. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b)
  1080. {
  1081. return Matx<_Tp, m, n>(a, b, Matx_MatMulOp());
  1082. }
  1083. template<typename _Tp, int m, int n> static inline
  1084. Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b)
  1085. {
  1086. Matx<_Tp, m, 1> c(a, b, Matx_MatMulOp());
  1087. return (const Vec<_Tp, m>&)(c);
  1088. }
  1089. template<typename _Tp, int m, int n> static inline
  1090. bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1091. {
  1092. for( int i = 0; i < m*n; i++ )
  1093. if( a.val[i] != b.val[i] ) return false;
  1094. return true;
  1095. }
  1096. template<typename _Tp, int m, int n> static inline
  1097. bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1098. {
  1099. return !(a == b);
  1100. }
  1101. //! @}
  1102. ////////////////////////////// Vec out-of-class operators ////////////////////////////////
  1103. //! @relates cv::Vec
  1104. //! @{
  1105. template<typename _Tp1, typename _Tp2, int cn> static inline
  1106. Vec<_Tp1, cn>& operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
  1107. {
  1108. for( int i = 0; i < cn; i++ )
  1109. a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
  1110. return a;
  1111. }
  1112. template<typename _Tp1, typename _Tp2, int cn> static inline
  1113. Vec<_Tp1, cn>& operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
  1114. {
  1115. for( int i = 0; i < cn; i++ )
  1116. a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
  1117. return a;
  1118. }
  1119. template<typename _Tp, int cn> static inline
  1120. Vec<_Tp, cn> operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
  1121. {
  1122. return Vec<_Tp, cn>(a, b, Matx_AddOp());
  1123. }
  1124. template<typename _Tp, int cn> static inline
  1125. Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
  1126. {
  1127. return Vec<_Tp, cn>(a, b, Matx_SubOp());
  1128. }
  1129. template<typename _Tp, int cn> static inline
  1130. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, int alpha)
  1131. {
  1132. for( int i = 0; i < cn; i++ )
  1133. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1134. return a;
  1135. }
  1136. template<typename _Tp, int cn> static inline
  1137. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, float alpha)
  1138. {
  1139. for( int i = 0; i < cn; i++ )
  1140. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1141. return a;
  1142. }
  1143. template<typename _Tp, int cn> static inline
  1144. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, double alpha)
  1145. {
  1146. for( int i = 0; i < cn; i++ )
  1147. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1148. return a;
  1149. }
  1150. template<typename _Tp, int cn> static inline
  1151. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, int alpha)
  1152. {
  1153. double ialpha = 1./alpha;
  1154. for( int i = 0; i < cn; i++ )
  1155. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1156. return a;
  1157. }
  1158. template<typename _Tp, int cn> static inline
  1159. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, float alpha)
  1160. {
  1161. float ialpha = 1.f/alpha;
  1162. for( int i = 0; i < cn; i++ )
  1163. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1164. return a;
  1165. }
  1166. template<typename _Tp, int cn> static inline
  1167. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, double alpha)
  1168. {
  1169. double ialpha = 1./alpha;
  1170. for( int i = 0; i < cn; i++ )
  1171. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1172. return a;
  1173. }
  1174. template<typename _Tp, int cn> static inline
  1175. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, int alpha)
  1176. {
  1177. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1178. }
  1179. template<typename _Tp, int cn> static inline
  1180. Vec<_Tp, cn> operator * (int alpha, const Vec<_Tp, cn>& a)
  1181. {
  1182. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1183. }
  1184. template<typename _Tp, int cn> static inline
  1185. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, float alpha)
  1186. {
  1187. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1188. }
  1189. template<typename _Tp, int cn> static inline
  1190. Vec<_Tp, cn> operator * (float alpha, const Vec<_Tp, cn>& a)
  1191. {
  1192. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1193. }
  1194. template<typename _Tp, int cn> static inline
  1195. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, double alpha)
  1196. {
  1197. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1198. }
  1199. template<typename _Tp, int cn> static inline
  1200. Vec<_Tp, cn> operator * (double alpha, const Vec<_Tp, cn>& a)
  1201. {
  1202. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1203. }
  1204. template<typename _Tp, int cn> static inline
  1205. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, int alpha)
  1206. {
  1207. return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
  1208. }
  1209. template<typename _Tp, int cn> static inline
  1210. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, float alpha)
  1211. {
  1212. return Vec<_Tp, cn>(a, 1.f/alpha, Matx_ScaleOp());
  1213. }
  1214. template<typename _Tp, int cn> static inline
  1215. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, double alpha)
  1216. {
  1217. return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
  1218. }
  1219. template<typename _Tp, int cn> static inline
  1220. Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a)
  1221. {
  1222. Vec<_Tp,cn> t;
  1223. for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
  1224. return t;
  1225. }
  1226. template<typename _Tp> inline Vec<_Tp, 4> operator * (const Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
  1227. {
  1228. return Vec<_Tp, 4>(saturate_cast<_Tp>(v1[0]*v2[0] - v1[1]*v2[1] - v1[2]*v2[2] - v1[3]*v2[3]),
  1229. saturate_cast<_Tp>(v1[0]*v2[1] + v1[1]*v2[0] + v1[2]*v2[3] - v1[3]*v2[2]),
  1230. saturate_cast<_Tp>(v1[0]*v2[2] - v1[1]*v2[3] + v1[2]*v2[0] + v1[3]*v2[1]),
  1231. saturate_cast<_Tp>(v1[0]*v2[3] + v1[1]*v2[2] - v1[2]*v2[1] + v1[3]*v2[0]));
  1232. }
  1233. template<typename _Tp> inline Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
  1234. {
  1235. v1 = v1 * v2;
  1236. return v1;
  1237. }
  1238. //! @}
  1239. } // cv
  1240. #endif // OPENCV_CORE_MATX_HPP