matx.hpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. /** @brief Generates uniformly distributed random numbers
  140. @param a Range boundary.
  141. @param b The other range boundary (boundaries don't have to be ordered, the lower boundary is inclusive,
  142. the upper one is exclusive).
  143. */
  144. static Matx randu(_Tp a, _Tp b);
  145. /** @brief Generates normally distributed random numbers
  146. @param a Mean value.
  147. @param b Standard deviation.
  148. */
  149. static Matx randn(_Tp a, _Tp b);
  150. //! dot product computed with the default precision
  151. _Tp dot(const Matx<_Tp, m, n>& v) const;
  152. //! dot product computed in double-precision arithmetics
  153. double ddot(const Matx<_Tp, m, n>& v) const;
  154. //! conversion to another data type
  155. template<typename T2> operator Matx<T2, m, n>() const;
  156. //! change the matrix shape
  157. template<int m1, int n1> Matx<_Tp, m1, n1> reshape() const;
  158. //! extract part of the matrix
  159. template<int m1, int n1> Matx<_Tp, m1, n1> get_minor(int base_row, int base_col) const;
  160. //! extract the matrix row
  161. Matx<_Tp, 1, n> row(int i) const;
  162. //! extract the matrix column
  163. Matx<_Tp, m, 1> col(int i) const;
  164. //! extract the matrix diagonal
  165. diag_type diag() const;
  166. //! transpose the matrix
  167. Matx<_Tp, n, m> t() const;
  168. //! invert the matrix
  169. Matx<_Tp, n, m> inv(int method=DECOMP_LU, bool *p_is_ok = NULL) const;
  170. //! solve linear system
  171. template<int l> Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const;
  172. Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const;
  173. //! multiply two matrices element-wise
  174. Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const;
  175. //! divide two matrices element-wise
  176. Matx<_Tp, m, n> div(const Matx<_Tp, m, n>& a) const;
  177. //! element access
  178. const _Tp& operator ()(int row, int col) const;
  179. _Tp& operator ()(int row, int col);
  180. //! 1D element access
  181. const _Tp& operator ()(int i) const;
  182. _Tp& operator ()(int i);
  183. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp);
  184. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp);
  185. template<typename _T2> Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp);
  186. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp);
  187. Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp);
  188. template<int l> Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp);
  189. Matx(const Matx<_Tp, n, m>& a, Matx_TOp);
  190. _Tp val[m*n]; //< matrix elements
  191. };
  192. typedef Matx<float, 1, 2> Matx12f;
  193. typedef Matx<double, 1, 2> Matx12d;
  194. typedef Matx<float, 1, 3> Matx13f;
  195. typedef Matx<double, 1, 3> Matx13d;
  196. typedef Matx<float, 1, 4> Matx14f;
  197. typedef Matx<double, 1, 4> Matx14d;
  198. typedef Matx<float, 1, 6> Matx16f;
  199. typedef Matx<double, 1, 6> Matx16d;
  200. typedef Matx<float, 2, 1> Matx21f;
  201. typedef Matx<double, 2, 1> Matx21d;
  202. typedef Matx<float, 3, 1> Matx31f;
  203. typedef Matx<double, 3, 1> Matx31d;
  204. typedef Matx<float, 4, 1> Matx41f;
  205. typedef Matx<double, 4, 1> Matx41d;
  206. typedef Matx<float, 6, 1> Matx61f;
  207. typedef Matx<double, 6, 1> Matx61d;
  208. typedef Matx<float, 2, 2> Matx22f;
  209. typedef Matx<double, 2, 2> Matx22d;
  210. typedef Matx<float, 2, 3> Matx23f;
  211. typedef Matx<double, 2, 3> Matx23d;
  212. typedef Matx<float, 3, 2> Matx32f;
  213. typedef Matx<double, 3, 2> Matx32d;
  214. typedef Matx<float, 3, 3> Matx33f;
  215. typedef Matx<double, 3, 3> Matx33d;
  216. typedef Matx<float, 3, 4> Matx34f;
  217. typedef Matx<double, 3, 4> Matx34d;
  218. typedef Matx<float, 4, 3> Matx43f;
  219. typedef Matx<double, 4, 3> Matx43d;
  220. typedef Matx<float, 4, 4> Matx44f;
  221. typedef Matx<double, 4, 4> Matx44d;
  222. typedef Matx<float, 6, 6> Matx66f;
  223. typedef Matx<double, 6, 6> Matx66d;
  224. /*!
  225. traits
  226. */
  227. template<typename _Tp, int m, int n> class DataType< Matx<_Tp, m, n> >
  228. {
  229. public:
  230. typedef Matx<_Tp, m, n> value_type;
  231. typedef Matx<typename DataType<_Tp>::work_type, m, n> work_type;
  232. typedef _Tp channel_type;
  233. typedef value_type vec_type;
  234. enum { generic_type = 0,
  235. channels = m * n,
  236. fmt = traits::SafeFmt<channel_type>::fmt + ((channels - 1) << 8)
  237. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  238. ,depth = DataType<channel_type>::depth
  239. ,type = CV_MAKETYPE(depth, channels)
  240. #endif
  241. };
  242. };
  243. namespace traits {
  244. template<typename _Tp, int m, int n>
  245. struct Depth< Matx<_Tp, m, n> > { enum { value = Depth<_Tp>::value }; };
  246. template<typename _Tp, int m, int n>
  247. struct Type< Matx<_Tp, m, n> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, n*m) }; };
  248. } // namespace
  249. /** @brief Comma-separated Matrix Initializer
  250. */
  251. template<typename _Tp, int m, int n> class MatxCommaInitializer
  252. {
  253. public:
  254. MatxCommaInitializer(Matx<_Tp, m, n>* _mtx);
  255. template<typename T2> MatxCommaInitializer<_Tp, m, n>& operator , (T2 val);
  256. Matx<_Tp, m, n> operator *() const;
  257. Matx<_Tp, m, n>* dst;
  258. int idx;
  259. };
  260. /*
  261. Utility methods
  262. */
  263. template<typename _Tp, int m> static double determinant(const Matx<_Tp, m, m>& a);
  264. template<typename _Tp, int m, int n> static double trace(const Matx<_Tp, m, n>& a);
  265. template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M);
  266. template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M, int normType);
  267. /////////////////////// Vec (used as element of multi-channel images /////////////////////
  268. /** @brief Template class for short numerical vectors, a partial case of Matx
  269. This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) on which you
  270. can perform basic arithmetical operations, access individual elements using [] operator etc. The
  271. vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., which
  272. elements are dynamically allocated in the heap.
  273. The template takes 2 parameters:
  274. @tparam _Tp element type
  275. @tparam cn the number of elements
  276. In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
  277. for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
  278. It is possible to convert Vec\<T,2\> to/from Point_, Vec\<T,3\> to/from Point3_ , and Vec\<T,4\>
  279. to CvScalar or Scalar_. Use operator[] to access the elements of Vec.
  280. All the expected vector operations are also implemented:
  281. - v1 = v2 + v3
  282. - v1 = v2 - v3
  283. - v1 = v2 \* scale
  284. - v1 = scale \* v2
  285. - v1 = -v2
  286. - v1 += v2 and other augmenting operations
  287. - v1 == v2, v1 != v2
  288. - norm(v1) (euclidean norm)
  289. The Vec class is commonly used to describe pixel types of multi-channel arrays. See Mat for details.
  290. */
  291. template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>
  292. {
  293. public:
  294. typedef _Tp value_type;
  295. enum {
  296. channels = cn,
  297. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  298. depth = Matx<_Tp, cn, 1>::depth,
  299. type = CV_MAKETYPE(depth, channels),
  300. #endif
  301. _dummy_enum_finalizer = 0
  302. };
  303. //! default constructor
  304. Vec();
  305. Vec(_Tp v0); //!< 1-element vector constructor
  306. Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor
  307. Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor
  308. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor
  309. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor
  310. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor
  311. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor
  312. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor
  313. Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor
  314. 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
  315. 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
  316. explicit Vec(const _Tp* values);
  317. #ifdef CV_CXX11
  318. Vec(std::initializer_list<_Tp>);
  319. #endif
  320. Vec(const Vec<_Tp, cn>& v);
  321. static Vec all(_Tp alpha);
  322. //! per-element multiplication
  323. Vec mul(const Vec<_Tp, cn>& v) const;
  324. //! conjugation (makes sense for complex numbers and quaternions)
  325. Vec conj() const;
  326. /*!
  327. cross product of the two 3D vectors.
  328. For other dimensionalities the exception is raised
  329. */
  330. Vec cross(const Vec& v) const;
  331. //! conversion to another data type
  332. template<typename T2> operator Vec<T2, cn>() const;
  333. /*! element access */
  334. const _Tp& operator [](int i) const;
  335. _Tp& operator[](int i);
  336. const _Tp& operator ()(int i) const;
  337. _Tp& operator ()(int i);
  338. #ifdef CV_CXX11
  339. Vec<_Tp, cn>& operator=(const Vec<_Tp, cn>& rhs) = default;
  340. #endif
  341. Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp);
  342. Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp);
  343. template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);
  344. };
  345. /** @name Shorter aliases for the most popular specializations of Vec<T,n>
  346. @{
  347. */
  348. typedef Vec<uchar, 2> Vec2b;
  349. typedef Vec<uchar, 3> Vec3b;
  350. typedef Vec<uchar, 4> Vec4b;
  351. typedef Vec<short, 2> Vec2s;
  352. typedef Vec<short, 3> Vec3s;
  353. typedef Vec<short, 4> Vec4s;
  354. typedef Vec<ushort, 2> Vec2w;
  355. typedef Vec<ushort, 3> Vec3w;
  356. typedef Vec<ushort, 4> Vec4w;
  357. typedef Vec<int, 2> Vec2i;
  358. typedef Vec<int, 3> Vec3i;
  359. typedef Vec<int, 4> Vec4i;
  360. typedef Vec<int, 6> Vec6i;
  361. typedef Vec<int, 8> Vec8i;
  362. typedef Vec<float, 2> Vec2f;
  363. typedef Vec<float, 3> Vec3f;
  364. typedef Vec<float, 4> Vec4f;
  365. typedef Vec<float, 6> Vec6f;
  366. typedef Vec<double, 2> Vec2d;
  367. typedef Vec<double, 3> Vec3d;
  368. typedef Vec<double, 4> Vec4d;
  369. typedef Vec<double, 6> Vec6d;
  370. /** @} */
  371. /*!
  372. traits
  373. */
  374. template<typename _Tp, int cn> class DataType< Vec<_Tp, cn> >
  375. {
  376. public:
  377. typedef Vec<_Tp, cn> value_type;
  378. typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
  379. typedef _Tp channel_type;
  380. typedef value_type vec_type;
  381. enum { generic_type = 0,
  382. channels = cn,
  383. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  384. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  385. depth = DataType<channel_type>::depth,
  386. type = CV_MAKETYPE(depth, channels),
  387. #endif
  388. _dummy_enum_finalizer = 0
  389. };
  390. };
  391. namespace traits {
  392. template<typename _Tp, int cn>
  393. struct Depth< Vec<_Tp, cn> > { enum { value = Depth<_Tp>::value }; };
  394. template<typename _Tp, int cn>
  395. struct Type< Vec<_Tp, cn> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, cn) }; };
  396. } // namespace
  397. /** @brief Comma-separated Vec Initializer
  398. */
  399. template<typename _Tp, int m> class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1>
  400. {
  401. public:
  402. VecCommaInitializer(Vec<_Tp, m>* _vec);
  403. template<typename T2> VecCommaInitializer<_Tp, m>& operator , (T2 val);
  404. Vec<_Tp, m> operator *() const;
  405. };
  406. template<typename _Tp, int cn> static Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v);
  407. //! @} core_basic
  408. //! @cond IGNORED
  409. ///////////////////////////////////// helper classes /////////////////////////////////////
  410. namespace internal
  411. {
  412. template<typename _Tp, int m> struct Matx_DetOp
  413. {
  414. double operator ()(const Matx<_Tp, m, m>& a) const
  415. {
  416. Matx<_Tp, m, m> temp = a;
  417. double p = LU(temp.val, m*sizeof(_Tp), m, 0, 0, 0);
  418. if( p == 0 )
  419. return p;
  420. for( int i = 0; i < m; i++ )
  421. p *= temp(i, i);
  422. return p;
  423. }
  424. };
  425. template<typename _Tp> struct Matx_DetOp<_Tp, 1>
  426. {
  427. double operator ()(const Matx<_Tp, 1, 1>& a) const
  428. {
  429. return a(0,0);
  430. }
  431. };
  432. template<typename _Tp> struct Matx_DetOp<_Tp, 2>
  433. {
  434. double operator ()(const Matx<_Tp, 2, 2>& a) const
  435. {
  436. return a(0,0)*a(1,1) - a(0,1)*a(1,0);
  437. }
  438. };
  439. template<typename _Tp> struct Matx_DetOp<_Tp, 3>
  440. {
  441. double operator ()(const Matx<_Tp, 3, 3>& a) const
  442. {
  443. return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
  444. a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
  445. a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1));
  446. }
  447. };
  448. template<typename _Tp> Vec<_Tp, 2> inline conjugate(const Vec<_Tp, 2>& v)
  449. {
  450. return Vec<_Tp, 2>(v[0], -v[1]);
  451. }
  452. template<typename _Tp> Vec<_Tp, 4> inline conjugate(const Vec<_Tp, 4>& v)
  453. {
  454. return Vec<_Tp, 4>(v[0], -v[1], -v[2], -v[3]);
  455. }
  456. } // internal
  457. ////////////////////////////////// Matx Implementation ///////////////////////////////////
  458. template<typename _Tp, int m, int n> inline
  459. Matx<_Tp, m, n>::Matx()
  460. {
  461. for(int i = 0; i < channels; i++) val[i] = _Tp(0);
  462. }
  463. template<typename _Tp, int m, int n> inline
  464. Matx<_Tp, m, n>::Matx(_Tp v0)
  465. {
  466. val[0] = v0;
  467. for(int i = 1; i < channels; i++) val[i] = _Tp(0);
  468. }
  469. template<typename _Tp, int m, int n> inline
  470. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1)
  471. {
  472. CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
  473. val[0] = v0; val[1] = v1;
  474. for(int i = 2; i < channels; i++) val[i] = _Tp(0);
  475. }
  476. template<typename _Tp, int m, int n> inline
  477. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2)
  478. {
  479. CV_StaticAssert(channels >= 3, "Matx should have at least 3 elements.");
  480. val[0] = v0; val[1] = v1; val[2] = v2;
  481. for(int i = 3; i < channels; i++) val[i] = _Tp(0);
  482. }
  483. template<typename _Tp, int m, int n> inline
  484. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
  485. {
  486. CV_StaticAssert(channels >= 4, "Matx should have at least 4 elements.");
  487. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  488. for(int i = 4; i < channels; i++) val[i] = _Tp(0);
  489. }
  490. template<typename _Tp, int m, int n> inline
  491. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
  492. {
  493. CV_StaticAssert(channels >= 5, "Matx should have at least 5 elements.");
  494. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
  495. for(int i = 5; i < channels; i++) val[i] = _Tp(0);
  496. }
  497. template<typename _Tp, int m, int n> inline
  498. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
  499. {
  500. CV_StaticAssert(channels >= 6, "Matx should have at least 6 elements.");
  501. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  502. val[4] = v4; val[5] = v5;
  503. for(int i = 6; i < channels; i++) val[i] = _Tp(0);
  504. }
  505. template<typename _Tp, int m, int n> inline
  506. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
  507. {
  508. CV_StaticAssert(channels >= 7, "Matx should have at least 7 elements.");
  509. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  510. val[4] = v4; val[5] = v5; val[6] = v6;
  511. for(int i = 7; i < channels; i++) val[i] = _Tp(0);
  512. }
  513. template<typename _Tp, int m, int n> inline
  514. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
  515. {
  516. CV_StaticAssert(channels >= 8, "Matx should have at least 8 elements.");
  517. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  518. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  519. for(int i = 8; 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)
  523. {
  524. CV_StaticAssert(channels >= 9, "Matx should have at least 9 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;
  528. for(int i = 9; 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)
  532. {
  533. CV_StaticAssert(channels >= 10, "Matx should have at least 10 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;
  537. for(int i = 10; 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)
  541. {
  542. CV_StaticAssert(channels >= 12, "Matx should have at least 12 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. for(int i = 12; i < channels; i++) val[i] = _Tp(0);
  547. }
  548. template<typename _Tp, int m, int n> inline
  549. 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)
  550. {
  551. CV_StaticAssert(channels >= 14, "Matx should have at least 14 elements.");
  552. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  553. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  554. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  555. val[12] = v12; val[13] = v13;
  556. for (int i = 14; i < channels; i++) val[i] = _Tp(0);
  557. }
  558. template<typename _Tp, int m, int n> inline
  559. 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)
  560. {
  561. CV_StaticAssert(channels >= 16, "Matx should have at least 16 elements.");
  562. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  563. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  564. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  565. val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15;
  566. for(int i = 16; i < channels; i++) val[i] = _Tp(0);
  567. }
  568. template<typename _Tp, int m, int n> inline
  569. Matx<_Tp, m, n>::Matx(const _Tp* values)
  570. {
  571. for( int i = 0; i < channels; i++ ) val[i] = values[i];
  572. }
  573. #ifdef CV_CXX11
  574. template<typename _Tp, int m, int n> inline
  575. Matx<_Tp, m, n>::Matx(std::initializer_list<_Tp> list)
  576. {
  577. CV_DbgAssert(list.size() == channels);
  578. int i = 0;
  579. for(const auto& elem : list)
  580. {
  581. val[i++] = elem;
  582. }
  583. }
  584. #endif
  585. template<typename _Tp, int m, int n> inline
  586. Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha)
  587. {
  588. Matx<_Tp, m, n> M;
  589. for( int i = 0; i < m*n; i++ ) M.val[i] = alpha;
  590. return M;
  591. }
  592. template<typename _Tp, int m, int n> inline
  593. Matx<_Tp,m,n> Matx<_Tp,m,n>::zeros()
  594. {
  595. return all(0);
  596. }
  597. template<typename _Tp, int m, int n> inline
  598. Matx<_Tp,m,n> Matx<_Tp,m,n>::ones()
  599. {
  600. return all(1);
  601. }
  602. template<typename _Tp, int m, int n> inline
  603. Matx<_Tp,m,n> Matx<_Tp,m,n>::eye()
  604. {
  605. Matx<_Tp,m,n> M;
  606. for(int i = 0; i < shortdim; i++)
  607. M(i,i) = 1;
  608. return M;
  609. }
  610. template<typename _Tp, int m, int n> inline
  611. _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const
  612. {
  613. _Tp s = 0;
  614. for( int i = 0; i < channels; i++ ) s += val[i]*M.val[i];
  615. return s;
  616. }
  617. template<typename _Tp, int m, int n> inline
  618. double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const
  619. {
  620. double s = 0;
  621. for( int i = 0; i < channels; i++ ) s += (double)val[i]*M.val[i];
  622. return s;
  623. }
  624. template<typename _Tp, int m, int n> inline
  625. Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const typename Matx<_Tp,m,n>::diag_type& d)
  626. {
  627. Matx<_Tp,m,n> M;
  628. for(int i = 0; i < shortdim; i++)
  629. M(i,i) = d(i, 0);
  630. return M;
  631. }
  632. template<typename _Tp, int m, int n> template<typename T2>
  633. inline Matx<_Tp, m, n>::operator Matx<T2, m, n>() const
  634. {
  635. Matx<T2, m, n> M;
  636. for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast<T2>(val[i]);
  637. return M;
  638. }
  639. template<typename _Tp, int m, int n> template<int m1, int n1> inline
  640. Matx<_Tp, m1, n1> Matx<_Tp, m, n>::reshape() const
  641. {
  642. CV_StaticAssert(m1*n1 == m*n, "Input and destnarion matrices must have the same number of elements");
  643. return (const Matx<_Tp, m1, n1>&)*this;
  644. }
  645. template<typename _Tp, int m, int n>
  646. template<int m1, int n1> inline
  647. Matx<_Tp, m1, n1> Matx<_Tp, m, n>::get_minor(int base_row, int base_col) const
  648. {
  649. CV_DbgAssert(0 <= base_row && base_row+m1 <= m && 0 <= base_col && base_col+n1 <= n);
  650. Matx<_Tp, m1, n1> s;
  651. for( int di = 0; di < m1; di++ )
  652. for( int dj = 0; dj < n1; dj++ )
  653. s(di, dj) = (*this)(base_row+di, base_col+dj);
  654. return s;
  655. }
  656. template<typename _Tp, int m, int n> inline
  657. Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const
  658. {
  659. CV_DbgAssert((unsigned)i < (unsigned)m);
  660. return Matx<_Tp, 1, n>(&val[i*n]);
  661. }
  662. template<typename _Tp, int m, int n> inline
  663. Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const
  664. {
  665. CV_DbgAssert((unsigned)j < (unsigned)n);
  666. Matx<_Tp, m, 1> v;
  667. for( int i = 0; i < m; i++ )
  668. v.val[i] = val[i*n + j];
  669. return v;
  670. }
  671. template<typename _Tp, int m, int n> inline
  672. typename Matx<_Tp, m, n>::diag_type Matx<_Tp, m, n>::diag() const
  673. {
  674. diag_type d;
  675. for( int i = 0; i < shortdim; i++ )
  676. d.val[i] = val[i*n + i];
  677. return d;
  678. }
  679. template<typename _Tp, int m, int n> inline
  680. const _Tp& Matx<_Tp, m, n>::operator()(int row_idx, int col_idx) const
  681. {
  682. CV_DbgAssert( (unsigned)row_idx < (unsigned)m && (unsigned)col_idx < (unsigned)n );
  683. return this->val[row_idx*n + col_idx];
  684. }
  685. template<typename _Tp, int m, int n> inline
  686. _Tp& Matx<_Tp, m, n>::operator ()(int row_idx, int col_idx)
  687. {
  688. CV_DbgAssert( (unsigned)row_idx < (unsigned)m && (unsigned)col_idx < (unsigned)n );
  689. return val[row_idx*n + col_idx];
  690. }
  691. template<typename _Tp, int m, int n> inline
  692. const _Tp& Matx<_Tp, m, n>::operator ()(int i) const
  693. {
  694. CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
  695. CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
  696. return val[i];
  697. }
  698. template<typename _Tp, int m, int n> inline
  699. _Tp& Matx<_Tp, m, n>::operator ()(int i)
  700. {
  701. CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
  702. CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
  703. return val[i];
  704. }
  705. template<typename _Tp, int m, int n> inline
  706. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp)
  707. {
  708. for( int i = 0; i < channels; i++ )
  709. val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]);
  710. }
  711. template<typename _Tp, int m, int n> inline
  712. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp)
  713. {
  714. for( int i = 0; i < channels; i++ )
  715. val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]);
  716. }
  717. template<typename _Tp, int m, int n> template<typename _T2> inline
  718. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp)
  719. {
  720. for( int i = 0; i < channels; i++ )
  721. val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  722. }
  723. template<typename _Tp, int m, int n> inline
  724. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp)
  725. {
  726. for( int i = 0; i < channels; i++ )
  727. val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]);
  728. }
  729. template<typename _Tp, int m, int n> inline
  730. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp)
  731. {
  732. for( int i = 0; i < channels; i++ )
  733. val[i] = saturate_cast<_Tp>(a.val[i] / b.val[i]);
  734. }
  735. template<typename _Tp, int m, int n> template<int l> inline
  736. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp)
  737. {
  738. for( int i = 0; i < m; i++ )
  739. for( int j = 0; j < n; j++ )
  740. {
  741. _Tp s = 0;
  742. for( int k = 0; k < l; k++ )
  743. s += a(i, k) * b(k, j);
  744. val[i*n + j] = s;
  745. }
  746. }
  747. template<typename _Tp, int m, int n> inline
  748. Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp)
  749. {
  750. for( int i = 0; i < m; i++ )
  751. for( int j = 0; j < n; j++ )
  752. val[i*n + j] = a(j, i);
  753. }
  754. template<typename _Tp, int m, int n> inline
  755. Matx<_Tp, m, n> Matx<_Tp, m, n>::mul(const Matx<_Tp, m, n>& a) const
  756. {
  757. return Matx<_Tp, m, n>(*this, a, Matx_MulOp());
  758. }
  759. template<typename _Tp, int m, int n> inline
  760. Matx<_Tp, m, n> Matx<_Tp, m, n>::div(const Matx<_Tp, m, n>& a) const
  761. {
  762. return Matx<_Tp, m, n>(*this, a, Matx_DivOp());
  763. }
  764. template<typename _Tp, int m, int n> inline
  765. Matx<_Tp, n, m> Matx<_Tp, m, n>::t() const
  766. {
  767. return Matx<_Tp, n, m>(*this, Matx_TOp());
  768. }
  769. template<typename _Tp, int m, int n> inline
  770. Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const
  771. {
  772. Matx<_Tp, n, 1> x = solve((const Matx<_Tp, m, 1>&)(rhs), method);
  773. return (Vec<_Tp, n>&)(x);
  774. }
  775. template<typename _Tp, int m> static inline
  776. double determinant(const Matx<_Tp, m, m>& a)
  777. {
  778. return cv::internal::Matx_DetOp<_Tp, m>()(a);
  779. }
  780. template<typename _Tp, int m, int n> static inline
  781. double trace(const Matx<_Tp, m, n>& a)
  782. {
  783. _Tp s = 0;
  784. for( int i = 0; i < std::min(m, n); i++ )
  785. s += a(i,i);
  786. return s;
  787. }
  788. template<typename _Tp, int m, int n> static inline
  789. double norm(const Matx<_Tp, m, n>& M)
  790. {
  791. return std::sqrt(normL2Sqr<_Tp, double>(M.val, m*n));
  792. }
  793. template<typename _Tp, int m, int n> static inline
  794. double norm(const Matx<_Tp, m, n>& M, int normType)
  795. {
  796. switch(normType) {
  797. case NORM_INF:
  798. return (double)normInf<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  799. case NORM_L1:
  800. return (double)normL1<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  801. case NORM_L2SQR:
  802. return (double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  803. default:
  804. case NORM_L2:
  805. return std::sqrt((double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n));
  806. }
  807. }
  808. //////////////////////////////// matx comma initializer //////////////////////////////////
  809. template<typename _Tp, typename _T2, int m, int n> static inline
  810. MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val)
  811. {
  812. MatxCommaInitializer<_Tp, m, n> commaInitializer((Matx<_Tp, m, n>*)&mtx);
  813. return (commaInitializer, val);
  814. }
  815. template<typename _Tp, int m, int n> inline
  816. MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx)
  817. : dst(_mtx), idx(0)
  818. {}
  819. template<typename _Tp, int m, int n> template<typename _T2> inline
  820. MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value)
  821. {
  822. CV_DbgAssert( idx < m*n );
  823. dst->val[idx++] = saturate_cast<_Tp>(value);
  824. return *this;
  825. }
  826. template<typename _Tp, int m, int n> inline
  827. Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const
  828. {
  829. CV_DbgAssert( idx == n*m );
  830. return *dst;
  831. }
  832. /////////////////////////////////// Vec Implementation ///////////////////////////////////
  833. template<typename _Tp, int cn> inline
  834. Vec<_Tp, cn>::Vec() {}
  835. template<typename _Tp, int cn> inline
  836. Vec<_Tp, cn>::Vec(_Tp v0)
  837. : Matx<_Tp, cn, 1>(v0) {}
  838. template<typename _Tp, int cn> inline
  839. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1)
  840. : Matx<_Tp, cn, 1>(v0, v1) {}
  841. template<typename _Tp, int cn> inline
  842. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
  843. : Matx<_Tp, cn, 1>(v0, v1, v2) {}
  844. template<typename _Tp, int cn> inline
  845. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
  846. : Matx<_Tp, cn, 1>(v0, v1, v2, v3) {}
  847. template<typename _Tp, int cn> inline
  848. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
  849. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) {}
  850. template<typename _Tp, int cn> inline
  851. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
  852. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) {}
  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)
  855. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) {}
  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)
  858. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) {}
  859. template<typename _Tp, int cn> inline
  860. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
  861. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) {}
  862. template<typename _Tp, int cn> inline
  863. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)
  864. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {}
  865. template<typename _Tp, int cn> inline
  866. 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)
  867. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) {}
  868. template<typename _Tp, int cn> inline
  869. Vec<_Tp, cn>::Vec(const _Tp* values)
  870. : Matx<_Tp, cn, 1>(values) {}
  871. #ifdef CV_CXX11
  872. template<typename _Tp, int cn> inline
  873. Vec<_Tp, cn>::Vec(std::initializer_list<_Tp> list)
  874. : Matx<_Tp, cn, 1>(list) {}
  875. #endif
  876. template<typename _Tp, int cn> inline
  877. Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m)
  878. : Matx<_Tp, cn, 1>(m.val) {}
  879. template<typename _Tp, int cn> inline
  880. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp op)
  881. : Matx<_Tp, cn, 1>(a, b, op) {}
  882. template<typename _Tp, int cn> inline
  883. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp op)
  884. : Matx<_Tp, cn, 1>(a, b, op) {}
  885. template<typename _Tp, int cn> template<typename _T2> inline
  886. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp op)
  887. : Matx<_Tp, cn, 1>(a, alpha, op) {}
  888. template<typename _Tp, int cn> inline
  889. Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha)
  890. {
  891. Vec v;
  892. for( int i = 0; i < cn; i++ ) v.val[i] = alpha;
  893. return v;
  894. }
  895. template<typename _Tp, int cn> inline
  896. Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const
  897. {
  898. Vec<_Tp, cn> w;
  899. for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(this->val[i]*v.val[i]);
  900. return w;
  901. }
  902. template<> inline
  903. Vec<float, 2> Vec<float, 2>::conj() const
  904. {
  905. return cv::internal::conjugate(*this);
  906. }
  907. template<> inline
  908. Vec<double, 2> Vec<double, 2>::conj() const
  909. {
  910. return cv::internal::conjugate(*this);
  911. }
  912. template<> inline
  913. Vec<float, 4> Vec<float, 4>::conj() const
  914. {
  915. return cv::internal::conjugate(*this);
  916. }
  917. template<> inline
  918. Vec<double, 4> Vec<double, 4>::conj() const
  919. {
  920. return cv::internal::conjugate(*this);
  921. }
  922. template<typename _Tp, int cn> inline
  923. Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>&) const
  924. {
  925. CV_StaticAssert(cn == 3, "for arbitrary-size vector there is no cross-product defined");
  926. return Vec<_Tp, cn>();
  927. }
  928. template<> inline
  929. Vec<float, 3> Vec<float, 3>::cross(const Vec<float, 3>& v) const
  930. {
  931. return Vec<float,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
  932. this->val[2]*v.val[0] - this->val[0]*v.val[2],
  933. this->val[0]*v.val[1] - this->val[1]*v.val[0]);
  934. }
  935. template<> inline
  936. Vec<double, 3> Vec<double, 3>::cross(const Vec<double, 3>& v) const
  937. {
  938. return Vec<double,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
  939. this->val[2]*v.val[0] - this->val[0]*v.val[2],
  940. this->val[0]*v.val[1] - this->val[1]*v.val[0]);
  941. }
  942. template<typename _Tp, int cn> template<typename T2> inline
  943. Vec<_Tp, cn>::operator Vec<T2, cn>() const
  944. {
  945. Vec<T2, cn> v;
  946. for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast<T2>(this->val[i]);
  947. return v;
  948. }
  949. template<typename _Tp, int cn> inline
  950. const _Tp& Vec<_Tp, cn>::operator [](int i) const
  951. {
  952. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  953. return this->val[i];
  954. }
  955. template<typename _Tp, int cn> inline
  956. _Tp& Vec<_Tp, cn>::operator [](int i)
  957. {
  958. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  959. return this->val[i];
  960. }
  961. template<typename _Tp, int cn> inline
  962. const _Tp& Vec<_Tp, cn>::operator ()(int i) const
  963. {
  964. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  965. return this->val[i];
  966. }
  967. template<typename _Tp, int cn> inline
  968. _Tp& Vec<_Tp, cn>::operator ()(int i)
  969. {
  970. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  971. return this->val[i];
  972. }
  973. template<typename _Tp, int cn> inline
  974. Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v)
  975. {
  976. double nv = norm(v);
  977. return v * (nv ? 1./nv : 0.);
  978. }
  979. //////////////////////////////// vec comma initializer //////////////////////////////////
  980. template<typename _Tp, typename _T2, int cn> static inline
  981. VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val)
  982. {
  983. VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec);
  984. return (commaInitializer, val);
  985. }
  986. template<typename _Tp, int cn> inline
  987. VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec)
  988. : MatxCommaInitializer<_Tp, cn, 1>(_vec)
  989. {}
  990. template<typename _Tp, int cn> template<typename _T2> inline
  991. VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value)
  992. {
  993. CV_DbgAssert( this->idx < cn );
  994. this->dst->val[this->idx++] = saturate_cast<_Tp>(value);
  995. return *this;
  996. }
  997. template<typename _Tp, int cn> inline
  998. Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const
  999. {
  1000. CV_DbgAssert( this->idx == cn );
  1001. return *this->dst;
  1002. }
  1003. //! @endcond
  1004. ///////////////////////////// Matx out-of-class operators ////////////////////////////////
  1005. //! @relates cv::Matx
  1006. //! @{
  1007. template<typename _Tp1, typename _Tp2, int m, int n> static inline
  1008. Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
  1009. {
  1010. for( int i = 0; i < m*n; i++ )
  1011. a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
  1012. return a;
  1013. }
  1014. template<typename _Tp1, typename _Tp2, int m, int n> static inline
  1015. Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
  1016. {
  1017. for( int i = 0; i < m*n; i++ )
  1018. a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
  1019. return a;
  1020. }
  1021. template<typename _Tp, int m, int n> static inline
  1022. Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1023. {
  1024. return Matx<_Tp, m, n>(a, b, Matx_AddOp());
  1025. }
  1026. template<typename _Tp, int m, int n> static inline
  1027. Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1028. {
  1029. return Matx<_Tp, m, n>(a, b, Matx_SubOp());
  1030. }
  1031. template<typename _Tp, int m, int n> static inline
  1032. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha)
  1033. {
  1034. for( int i = 0; i < m*n; i++ )
  1035. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1036. return a;
  1037. }
  1038. template<typename _Tp, int m, int n> static inline
  1039. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha)
  1040. {
  1041. for( int i = 0; i < m*n; i++ )
  1042. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1043. return a;
  1044. }
  1045. template<typename _Tp, int m, int n> static inline
  1046. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha)
  1047. {
  1048. for( int i = 0; i < m*n; i++ )
  1049. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1050. return a;
  1051. }
  1052. template<typename _Tp, int m, int n> static inline
  1053. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int alpha)
  1054. {
  1055. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1056. }
  1057. template<typename _Tp, int m, int n> static inline
  1058. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, float alpha)
  1059. {
  1060. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1061. }
  1062. template<typename _Tp, int m, int n> static inline
  1063. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, double alpha)
  1064. {
  1065. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1066. }
  1067. template<typename _Tp, int m, int n> static inline
  1068. Matx<_Tp, m, n> operator * (int alpha, const Matx<_Tp, m, n>& a)
  1069. {
  1070. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1071. }
  1072. template<typename _Tp, int m, int n> static inline
  1073. Matx<_Tp, m, n> operator * (float alpha, const Matx<_Tp, m, n>& a)
  1074. {
  1075. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1076. }
  1077. template<typename _Tp, int m, int n> static inline
  1078. Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a)
  1079. {
  1080. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1081. }
  1082. template<typename _Tp, int m, int n> static inline
  1083. Matx<_Tp, m, n>& operator /= (Matx<_Tp, m, n>& a, float alpha)
  1084. {
  1085. for( int i = 0; i < m*n; i++ )
  1086. a.val[i] = a.val[i] / alpha;
  1087. return a;
  1088. }
  1089. template<typename _Tp, int m, int n> static inline
  1090. Matx<_Tp, m, n>& operator /= (Matx<_Tp, m, n>& a, double alpha)
  1091. {
  1092. for( int i = 0; i < m*n; i++ )
  1093. a.val[i] = a.val[i] / alpha;
  1094. return a;
  1095. }
  1096. template<typename _Tp, int m, int n> static inline
  1097. Matx<_Tp, m, n> operator / (const Matx<_Tp, m, n>& a, float alpha)
  1098. {
  1099. return Matx<_Tp, m, n>(a, 1.f/alpha, Matx_ScaleOp());
  1100. }
  1101. template<typename _Tp, int m, int n> static inline
  1102. Matx<_Tp, m, n> operator / (const Matx<_Tp, m, n>& a, double alpha)
  1103. {
  1104. return Matx<_Tp, m, n>(a, 1./alpha, Matx_ScaleOp());
  1105. }
  1106. template<typename _Tp, int m, int n> static inline
  1107. Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a)
  1108. {
  1109. return Matx<_Tp, m, n>(a, -1, Matx_ScaleOp());
  1110. }
  1111. template<typename _Tp, int m, int n, int l> static inline
  1112. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b)
  1113. {
  1114. return Matx<_Tp, m, n>(a, b, Matx_MatMulOp());
  1115. }
  1116. template<typename _Tp, int m, int n> static inline
  1117. Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b)
  1118. {
  1119. Matx<_Tp, m, 1> c(a, b, Matx_MatMulOp());
  1120. return (const Vec<_Tp, m>&)(c);
  1121. }
  1122. template<typename _Tp, int m, int n> static inline
  1123. bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1124. {
  1125. for( int i = 0; i < m*n; i++ )
  1126. if( a.val[i] != b.val[i] ) return false;
  1127. return true;
  1128. }
  1129. template<typename _Tp, int m, int n> static inline
  1130. bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1131. {
  1132. return !(a == b);
  1133. }
  1134. //! @}
  1135. ////////////////////////////// Vec out-of-class operators ////////////////////////////////
  1136. //! @relates cv::Vec
  1137. //! @{
  1138. template<typename _Tp1, typename _Tp2, int cn> static inline
  1139. Vec<_Tp1, cn>& operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
  1140. {
  1141. for( int i = 0; i < cn; i++ )
  1142. a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
  1143. return a;
  1144. }
  1145. template<typename _Tp1, typename _Tp2, int cn> static inline
  1146. Vec<_Tp1, cn>& operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
  1147. {
  1148. for( int i = 0; i < cn; i++ )
  1149. a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
  1150. return a;
  1151. }
  1152. template<typename _Tp, int cn> static inline
  1153. Vec<_Tp, cn> operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
  1154. {
  1155. return Vec<_Tp, cn>(a, b, Matx_AddOp());
  1156. }
  1157. template<typename _Tp, int cn> static inline
  1158. Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
  1159. {
  1160. return Vec<_Tp, cn>(a, b, Matx_SubOp());
  1161. }
  1162. template<typename _Tp, int cn> static inline
  1163. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, int alpha)
  1164. {
  1165. for( int i = 0; i < cn; i++ )
  1166. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1167. return a;
  1168. }
  1169. template<typename _Tp, int cn> static inline
  1170. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, float alpha)
  1171. {
  1172. for( int i = 0; i < cn; i++ )
  1173. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1174. return a;
  1175. }
  1176. template<typename _Tp, int cn> static inline
  1177. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, double alpha)
  1178. {
  1179. for( int i = 0; i < cn; i++ )
  1180. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1181. return a;
  1182. }
  1183. template<typename _Tp, int cn> static inline
  1184. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, int alpha)
  1185. {
  1186. double ialpha = 1./alpha;
  1187. for( int i = 0; i < cn; i++ )
  1188. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1189. return a;
  1190. }
  1191. template<typename _Tp, int cn> static inline
  1192. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, float alpha)
  1193. {
  1194. float ialpha = 1.f/alpha;
  1195. for( int i = 0; i < cn; i++ )
  1196. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1197. return a;
  1198. }
  1199. template<typename _Tp, int cn> static inline
  1200. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, double alpha)
  1201. {
  1202. double ialpha = 1./alpha;
  1203. for( int i = 0; i < cn; i++ )
  1204. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1205. return a;
  1206. }
  1207. template<typename _Tp, int cn> static inline
  1208. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, int alpha)
  1209. {
  1210. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1211. }
  1212. template<typename _Tp, int cn> static inline
  1213. Vec<_Tp, cn> operator * (int alpha, const Vec<_Tp, cn>& a)
  1214. {
  1215. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1216. }
  1217. template<typename _Tp, int cn> static inline
  1218. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, float alpha)
  1219. {
  1220. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1221. }
  1222. template<typename _Tp, int cn> static inline
  1223. Vec<_Tp, cn> operator * (float alpha, const Vec<_Tp, cn>& a)
  1224. {
  1225. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1226. }
  1227. template<typename _Tp, int cn> static inline
  1228. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, double alpha)
  1229. {
  1230. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1231. }
  1232. template<typename _Tp, int cn> static inline
  1233. Vec<_Tp, cn> operator * (double alpha, const Vec<_Tp, cn>& a)
  1234. {
  1235. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1236. }
  1237. template<typename _Tp, int cn> static inline
  1238. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, int alpha)
  1239. {
  1240. return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
  1241. }
  1242. template<typename _Tp, int cn> static inline
  1243. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, float alpha)
  1244. {
  1245. return Vec<_Tp, cn>(a, 1.f/alpha, Matx_ScaleOp());
  1246. }
  1247. template<typename _Tp, int cn> static inline
  1248. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, double alpha)
  1249. {
  1250. return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
  1251. }
  1252. template<typename _Tp, int cn> static inline
  1253. Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a)
  1254. {
  1255. Vec<_Tp,cn> t;
  1256. for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
  1257. return t;
  1258. }
  1259. template<typename _Tp> inline Vec<_Tp, 4> operator * (const Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
  1260. {
  1261. return Vec<_Tp, 4>(saturate_cast<_Tp>(v1[0]*v2[0] - v1[1]*v2[1] - v1[2]*v2[2] - v1[3]*v2[3]),
  1262. saturate_cast<_Tp>(v1[0]*v2[1] + v1[1]*v2[0] + v1[2]*v2[3] - v1[3]*v2[2]),
  1263. saturate_cast<_Tp>(v1[0]*v2[2] - v1[1]*v2[3] + v1[2]*v2[0] + v1[3]*v2[1]),
  1264. saturate_cast<_Tp>(v1[0]*v2[3] + v1[1]*v2[2] - v1[2]*v2[1] + v1[3]*v2[0]));
  1265. }
  1266. template<typename _Tp> inline Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
  1267. {
  1268. v1 = v1 * v2;
  1269. return v1;
  1270. }
  1271. //! @}
  1272. } // cv
  1273. #endif // OPENCV_CORE_MATX_HPP