matx.hpp 48 KB

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