matx.hpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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. CV_NODISCARD_STD static Matx all(_Tp alpha);
  135. CV_NODISCARD_STD static Matx zeros();
  136. CV_NODISCARD_STD static Matx ones();
  137. CV_NODISCARD_STD static Matx eye();
  138. CV_NODISCARD_STD 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. CV_NODISCARD_STD 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. CV_NODISCARD_STD 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. static Vec ones();
  323. static Vec randn(_Tp a, _Tp b);
  324. static Vec randu(_Tp a, _Tp b);
  325. static Vec zeros();
  326. #ifdef CV_CXX11
  327. static Vec diag(_Tp alpha) = delete;
  328. static Vec eye() = delete;
  329. #endif
  330. //! per-element multiplication
  331. Vec mul(const Vec<_Tp, cn>& v) const;
  332. //! conjugation (makes sense for complex numbers and quaternions)
  333. Vec conj() const;
  334. /*!
  335. cross product of the two 3D vectors.
  336. For other dimensionalities the exception is raised
  337. */
  338. Vec cross(const Vec& v) const;
  339. //! conversion to another data type
  340. template<typename T2> operator Vec<T2, cn>() const;
  341. /*! element access */
  342. const _Tp& operator [](int i) const;
  343. _Tp& operator[](int i);
  344. const _Tp& operator ()(int i) const;
  345. _Tp& operator ()(int i);
  346. #ifdef CV_CXX11
  347. Vec<_Tp, cn>& operator=(const Vec<_Tp, cn>& rhs) = default;
  348. #endif
  349. Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp);
  350. Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp);
  351. template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);
  352. };
  353. /** @name Shorter aliases for the most popular specializations of Vec<T,n>
  354. @{
  355. */
  356. typedef Vec<uchar, 2> Vec2b;
  357. typedef Vec<uchar, 3> Vec3b;
  358. typedef Vec<uchar, 4> Vec4b;
  359. typedef Vec<short, 2> Vec2s;
  360. typedef Vec<short, 3> Vec3s;
  361. typedef Vec<short, 4> Vec4s;
  362. typedef Vec<ushort, 2> Vec2w;
  363. typedef Vec<ushort, 3> Vec3w;
  364. typedef Vec<ushort, 4> Vec4w;
  365. typedef Vec<int, 2> Vec2i;
  366. typedef Vec<int, 3> Vec3i;
  367. typedef Vec<int, 4> Vec4i;
  368. typedef Vec<int, 6> Vec6i;
  369. typedef Vec<int, 8> Vec8i;
  370. typedef Vec<float, 2> Vec2f;
  371. typedef Vec<float, 3> Vec3f;
  372. typedef Vec<float, 4> Vec4f;
  373. typedef Vec<float, 6> Vec6f;
  374. typedef Vec<double, 2> Vec2d;
  375. typedef Vec<double, 3> Vec3d;
  376. typedef Vec<double, 4> Vec4d;
  377. typedef Vec<double, 6> Vec6d;
  378. /** @} */
  379. /*!
  380. traits
  381. */
  382. template<typename _Tp, int cn> class DataType< Vec<_Tp, cn> >
  383. {
  384. public:
  385. typedef Vec<_Tp, cn> value_type;
  386. typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
  387. typedef _Tp channel_type;
  388. typedef value_type vec_type;
  389. enum { generic_type = 0,
  390. channels = cn,
  391. fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
  392. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  393. depth = DataType<channel_type>::depth,
  394. type = CV_MAKETYPE(depth, channels),
  395. #endif
  396. _dummy_enum_finalizer = 0
  397. };
  398. };
  399. namespace traits {
  400. template<typename _Tp, int cn>
  401. struct Depth< Vec<_Tp, cn> > { enum { value = Depth<_Tp>::value }; };
  402. template<typename _Tp, int cn>
  403. struct Type< Vec<_Tp, cn> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, cn) }; };
  404. } // namespace
  405. /** @brief Comma-separated Vec Initializer
  406. */
  407. template<typename _Tp, int m> class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1>
  408. {
  409. public:
  410. VecCommaInitializer(Vec<_Tp, m>* _vec);
  411. template<typename T2> VecCommaInitializer<_Tp, m>& operator , (T2 val);
  412. Vec<_Tp, m> operator *() const;
  413. };
  414. template<typename _Tp, int cn> static Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v);
  415. //! @} core_basic
  416. //! @cond IGNORED
  417. ///////////////////////////////////// helper classes /////////////////////////////////////
  418. namespace internal
  419. {
  420. template<typename _Tp, int m> struct Matx_DetOp
  421. {
  422. double operator ()(const Matx<_Tp, m, m>& a) const
  423. {
  424. Matx<_Tp, m, m> temp = a;
  425. double p = LU(temp.val, m*sizeof(_Tp), m, 0, 0, 0);
  426. if( p == 0 )
  427. return p;
  428. for( int i = 0; i < m; i++ )
  429. p *= temp(i, i);
  430. return p;
  431. }
  432. };
  433. template<typename _Tp> struct Matx_DetOp<_Tp, 1>
  434. {
  435. double operator ()(const Matx<_Tp, 1, 1>& a) const
  436. {
  437. return a(0,0);
  438. }
  439. };
  440. template<typename _Tp> struct Matx_DetOp<_Tp, 2>
  441. {
  442. double operator ()(const Matx<_Tp, 2, 2>& a) const
  443. {
  444. return a(0,0)*a(1,1) - a(0,1)*a(1,0);
  445. }
  446. };
  447. template<typename _Tp> struct Matx_DetOp<_Tp, 3>
  448. {
  449. double operator ()(const Matx<_Tp, 3, 3>& a) const
  450. {
  451. return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
  452. a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
  453. a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1));
  454. }
  455. };
  456. template<typename _Tp> Vec<_Tp, 2> inline conjugate(const Vec<_Tp, 2>& v)
  457. {
  458. return Vec<_Tp, 2>(v[0], -v[1]);
  459. }
  460. template<typename _Tp> Vec<_Tp, 4> inline conjugate(const Vec<_Tp, 4>& v)
  461. {
  462. return Vec<_Tp, 4>(v[0], -v[1], -v[2], -v[3]);
  463. }
  464. } // internal
  465. ////////////////////////////////// Matx Implementation ///////////////////////////////////
  466. template<typename _Tp, int m, int n> inline
  467. Matx<_Tp, m, n>::Matx()
  468. {
  469. for(int i = 0; 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)
  473. {
  474. val[0] = v0;
  475. for(int i = 1; i < channels; i++) val[i] = _Tp(0);
  476. }
  477. template<typename _Tp, int m, int n> inline
  478. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1)
  479. {
  480. CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
  481. val[0] = v0; val[1] = v1;
  482. for(int i = 2; i < channels; i++) val[i] = _Tp(0);
  483. }
  484. template<typename _Tp, int m, int n> inline
  485. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2)
  486. {
  487. CV_StaticAssert(channels >= 3, "Matx should have at least 3 elements.");
  488. val[0] = v0; val[1] = v1; val[2] = v2;
  489. for(int i = 3; i < channels; i++) val[i] = _Tp(0);
  490. }
  491. template<typename _Tp, int m, int n> inline
  492. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
  493. {
  494. CV_StaticAssert(channels >= 4, "Matx should have at least 4 elements.");
  495. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  496. for(int i = 4; i < channels; i++) val[i] = _Tp(0);
  497. }
  498. template<typename _Tp, int m, int n> inline
  499. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
  500. {
  501. CV_StaticAssert(channels >= 5, "Matx should have at least 5 elements.");
  502. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
  503. for(int i = 5; 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)
  507. {
  508. CV_StaticAssert(channels >= 6, "Matx should have at least 6 elements.");
  509. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  510. val[4] = v4; val[5] = v5;
  511. for(int i = 6; 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)
  515. {
  516. CV_StaticAssert(channels >= 7, "Matx should have at least 7 elements.");
  517. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  518. val[4] = v4; val[5] = v5; val[6] = v6;
  519. for(int i = 7; 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)
  523. {
  524. CV_StaticAssert(channels >= 8, "Matx should have at least 8 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. for(int i = 8; i < channels; i++) val[i] = _Tp(0);
  528. }
  529. template<typename _Tp, int m, int n> inline
  530. Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
  531. {
  532. CV_StaticAssert(channels >= 9, "Matx should have at least 9 elements.");
  533. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  534. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  535. val[8] = v8;
  536. for(int i = 9; i < channels; i++) val[i] = _Tp(0);
  537. }
  538. template<typename _Tp, int m, int n> inline
  539. 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)
  540. {
  541. CV_StaticAssert(channels >= 10, "Matx should have at least 10 elements.");
  542. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  543. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  544. val[8] = v8; val[9] = v9;
  545. for(int i = 10; i < channels; i++) val[i] = _Tp(0);
  546. }
  547. template<typename _Tp, int m, int n> inline
  548. 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)
  549. {
  550. CV_StaticAssert(channels >= 12, "Matx should have at least 12 elements.");
  551. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  552. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  553. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  554. for(int i = 12; i < channels; i++) val[i] = _Tp(0);
  555. }
  556. template<typename _Tp, int m, int n> inline
  557. 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)
  558. {
  559. CV_StaticAssert(channels >= 14, "Matx should have at least 14 elements.");
  560. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  561. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  562. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  563. val[12] = v12; val[13] = v13;
  564. for (int i = 14; i < channels; i++) val[i] = _Tp(0);
  565. }
  566. template<typename _Tp, int m, int n> inline
  567. 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)
  568. {
  569. CV_StaticAssert(channels >= 16, "Matx should have at least 16 elements.");
  570. val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
  571. val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
  572. val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
  573. val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15;
  574. for(int i = 16; i < channels; i++) val[i] = _Tp(0);
  575. }
  576. // WARNING: unreachable code using Ninja
  577. #if defined _MSC_VER && _MSC_VER >= 1920
  578. #pragma warning(push)
  579. #pragma warning(disable: 4702)
  580. #endif
  581. template<typename _Tp, int m, int n> inline
  582. Matx<_Tp, m, n>::Matx(const _Tp* values)
  583. {
  584. for( int i = 0; i < channels; i++ ) val[i] = values[i];
  585. }
  586. #if defined _MSC_VER && _MSC_VER >= 1920
  587. #pragma warning(pop)
  588. #endif
  589. #ifdef CV_CXX11
  590. template<typename _Tp, int m, int n> inline
  591. Matx<_Tp, m, n>::Matx(std::initializer_list<_Tp> list)
  592. {
  593. CV_DbgAssert(list.size() == channels);
  594. int i = 0;
  595. for(const auto& elem : list)
  596. {
  597. val[i++] = elem;
  598. }
  599. }
  600. #endif
  601. template<typename _Tp, int m, int n> inline
  602. Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha)
  603. {
  604. Matx<_Tp, m, n> M;
  605. for( int i = 0; i < m*n; i++ ) M.val[i] = alpha;
  606. return M;
  607. }
  608. template<typename _Tp, int m, int n> inline
  609. Matx<_Tp,m,n> Matx<_Tp,m,n>::zeros()
  610. {
  611. return all(0);
  612. }
  613. template<typename _Tp, int m, int n> inline
  614. Matx<_Tp,m,n> Matx<_Tp,m,n>::ones()
  615. {
  616. return all(1);
  617. }
  618. template<typename _Tp, int m, int n> inline
  619. Matx<_Tp,m,n> Matx<_Tp,m,n>::eye()
  620. {
  621. Matx<_Tp,m,n> M;
  622. for(int i = 0; i < shortdim; i++)
  623. M(i,i) = 1;
  624. return M;
  625. }
  626. template<typename _Tp, int m, int n> inline
  627. _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const
  628. {
  629. _Tp s = 0;
  630. for( int i = 0; i < channels; i++ ) s += val[i]*M.val[i];
  631. return s;
  632. }
  633. template<typename _Tp, int m, int n> inline
  634. double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const
  635. {
  636. double s = 0;
  637. for( int i = 0; i < channels; i++ ) s += (double)val[i]*M.val[i];
  638. return s;
  639. }
  640. template<typename _Tp, int m, int n> inline
  641. Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const typename Matx<_Tp,m,n>::diag_type& d)
  642. {
  643. Matx<_Tp,m,n> M;
  644. for(int i = 0; i < shortdim; i++)
  645. M(i,i) = d(i, 0);
  646. return M;
  647. }
  648. template<typename _Tp, int m, int n> template<typename T2>
  649. inline Matx<_Tp, m, n>::operator Matx<T2, m, n>() const
  650. {
  651. Matx<T2, m, n> M;
  652. for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast<T2>(val[i]);
  653. return M;
  654. }
  655. template<typename _Tp, int m, int n> template<int m1, int n1> inline
  656. Matx<_Tp, m1, n1> Matx<_Tp, m, n>::reshape() const
  657. {
  658. CV_StaticAssert(m1*n1 == m*n, "Input and destnarion matrices must have the same number of elements");
  659. return (const Matx<_Tp, m1, n1>&)*this;
  660. }
  661. template<typename _Tp, int m, int n>
  662. template<int m1, int n1> inline
  663. Matx<_Tp, m1, n1> Matx<_Tp, m, n>::get_minor(int base_row, int base_col) const
  664. {
  665. CV_DbgAssert(0 <= base_row && base_row+m1 <= m && 0 <= base_col && base_col+n1 <= n);
  666. Matx<_Tp, m1, n1> s;
  667. for( int di = 0; di < m1; di++ )
  668. for( int dj = 0; dj < n1; dj++ )
  669. s(di, dj) = (*this)(base_row+di, base_col+dj);
  670. return s;
  671. }
  672. template<typename _Tp, int m, int n> inline
  673. Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const
  674. {
  675. CV_DbgAssert((unsigned)i < (unsigned)m);
  676. return Matx<_Tp, 1, n>(&val[i*n]);
  677. }
  678. template<typename _Tp, int m, int n> inline
  679. Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const
  680. {
  681. CV_DbgAssert((unsigned)j < (unsigned)n);
  682. Matx<_Tp, m, 1> v;
  683. for( int i = 0; i < m; i++ )
  684. v.val[i] = val[i*n + j];
  685. return v;
  686. }
  687. template<typename _Tp, int m, int n> inline
  688. typename Matx<_Tp, m, n>::diag_type Matx<_Tp, m, n>::diag() const
  689. {
  690. diag_type d;
  691. for( int i = 0; i < shortdim; i++ )
  692. d.val[i] = val[i*n + i];
  693. return d;
  694. }
  695. template<typename _Tp, int m, int n> inline
  696. const _Tp& Matx<_Tp, m, n>::operator()(int row_idx, int col_idx) const
  697. {
  698. CV_DbgAssert( (unsigned)row_idx < (unsigned)m && (unsigned)col_idx < (unsigned)n );
  699. return this->val[row_idx*n + col_idx];
  700. }
  701. template<typename _Tp, int m, int n> inline
  702. _Tp& Matx<_Tp, m, n>::operator ()(int row_idx, int col_idx)
  703. {
  704. CV_DbgAssert( (unsigned)row_idx < (unsigned)m && (unsigned)col_idx < (unsigned)n );
  705. return val[row_idx*n + col_idx];
  706. }
  707. template<typename _Tp, int m, int n> inline
  708. const _Tp& Matx<_Tp, m, n>::operator ()(int i) const
  709. {
  710. CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
  711. CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
  712. return val[i];
  713. }
  714. template<typename _Tp, int m, int n> inline
  715. _Tp& Matx<_Tp, m, n>::operator ()(int i)
  716. {
  717. CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
  718. CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
  719. return val[i];
  720. }
  721. template<typename _Tp, int m, int n> inline
  722. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp)
  723. {
  724. for( int i = 0; i < channels; i++ )
  725. val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]);
  726. }
  727. template<typename _Tp, int m, int n> inline
  728. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp)
  729. {
  730. for( int i = 0; i < channels; i++ )
  731. val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]);
  732. }
  733. template<typename _Tp, int m, int n> template<typename _T2> inline
  734. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp)
  735. {
  736. for( int i = 0; i < channels; i++ )
  737. val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  738. }
  739. template<typename _Tp, int m, int n> inline
  740. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp)
  741. {
  742. for( int i = 0; i < channels; i++ )
  743. val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]);
  744. }
  745. template<typename _Tp, int m, int n> inline
  746. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp)
  747. {
  748. for( int i = 0; i < channels; i++ )
  749. val[i] = saturate_cast<_Tp>(a.val[i] / b.val[i]);
  750. }
  751. template<typename _Tp, int m, int n> template<int l> inline
  752. Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp)
  753. {
  754. for( int i = 0; i < m; i++ )
  755. for( int j = 0; j < n; j++ )
  756. {
  757. _Tp s = 0;
  758. for( int k = 0; k < l; k++ )
  759. s += a(i, k) * b(k, j);
  760. val[i*n + j] = s;
  761. }
  762. }
  763. template<typename _Tp, int m, int n> inline
  764. Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp)
  765. {
  766. for( int i = 0; i < m; i++ )
  767. for( int j = 0; j < n; j++ )
  768. val[i*n + j] = a(j, i);
  769. }
  770. template<typename _Tp, int m, int n> inline
  771. Matx<_Tp, m, n> Matx<_Tp, m, n>::mul(const Matx<_Tp, m, n>& a) const
  772. {
  773. return Matx<_Tp, m, n>(*this, a, Matx_MulOp());
  774. }
  775. template<typename _Tp, int m, int n> inline
  776. Matx<_Tp, m, n> Matx<_Tp, m, n>::div(const Matx<_Tp, m, n>& a) const
  777. {
  778. return Matx<_Tp, m, n>(*this, a, Matx_DivOp());
  779. }
  780. template<typename _Tp, int m, int n> inline
  781. Matx<_Tp, n, m> Matx<_Tp, m, n>::t() const
  782. {
  783. return Matx<_Tp, n, m>(*this, Matx_TOp());
  784. }
  785. template<typename _Tp, int m, int n> inline
  786. Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const
  787. {
  788. Matx<_Tp, n, 1> x = solve((const Matx<_Tp, m, 1>&)(rhs), method);
  789. return (Vec<_Tp, n>&)(x);
  790. }
  791. template<typename _Tp, int m> static inline
  792. double determinant(const Matx<_Tp, m, m>& a)
  793. {
  794. return cv::internal::Matx_DetOp<_Tp, m>()(a);
  795. }
  796. template<typename _Tp, int m, int n> static inline
  797. double trace(const Matx<_Tp, m, n>& a)
  798. {
  799. _Tp s = 0;
  800. for( int i = 0; i < std::min(m, n); i++ )
  801. s += a(i,i);
  802. return s;
  803. }
  804. template<typename _Tp, int m, int n> static inline
  805. double norm(const Matx<_Tp, m, n>& M)
  806. {
  807. return std::sqrt(normL2Sqr<_Tp, double>(M.val, m*n));
  808. }
  809. template<typename _Tp, int m, int n> static inline
  810. double norm(const Matx<_Tp, m, n>& M, int normType)
  811. {
  812. switch(normType) {
  813. case NORM_INF:
  814. return (double)normInf<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  815. case NORM_L1:
  816. return (double)normL1<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  817. case NORM_L2SQR:
  818. return (double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
  819. default:
  820. case NORM_L2:
  821. return std::sqrt((double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n));
  822. }
  823. }
  824. //////////////////////////////// matx comma initializer //////////////////////////////////
  825. template<typename _Tp, typename _T2, int m, int n> static inline
  826. MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val)
  827. {
  828. MatxCommaInitializer<_Tp, m, n> commaInitializer((Matx<_Tp, m, n>*)&mtx);
  829. return (commaInitializer, val);
  830. }
  831. template<typename _Tp, int m, int n> inline
  832. MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx)
  833. : dst(_mtx), idx(0)
  834. {}
  835. template<typename _Tp, int m, int n> template<typename _T2> inline
  836. MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value)
  837. {
  838. CV_DbgAssert( idx < m*n );
  839. dst->val[idx++] = saturate_cast<_Tp>(value);
  840. return *this;
  841. }
  842. template<typename _Tp, int m, int n> inline
  843. Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const
  844. {
  845. CV_DbgAssert( idx == n*m );
  846. return *dst;
  847. }
  848. /////////////////////////////////// Vec Implementation ///////////////////////////////////
  849. template<typename _Tp, int cn> inline
  850. Vec<_Tp, cn>::Vec() {}
  851. template<typename _Tp, int cn> inline
  852. Vec<_Tp, cn>::Vec(_Tp v0)
  853. : Matx<_Tp, cn, 1>(v0) {}
  854. template<typename _Tp, int cn> inline
  855. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1)
  856. : Matx<_Tp, cn, 1>(v0, v1) {}
  857. template<typename _Tp, int cn> inline
  858. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
  859. : Matx<_Tp, cn, 1>(v0, v1, v2) {}
  860. template<typename _Tp, int cn> inline
  861. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
  862. : Matx<_Tp, cn, 1>(v0, v1, v2, v3) {}
  863. template<typename _Tp, int cn> inline
  864. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
  865. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) {}
  866. template<typename _Tp, int cn> inline
  867. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
  868. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) {}
  869. template<typename _Tp, int cn> inline
  870. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
  871. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) {}
  872. template<typename _Tp, int cn> inline
  873. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
  874. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) {}
  875. template<typename _Tp, int cn> inline
  876. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
  877. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) {}
  878. template<typename _Tp, int cn> inline
  879. Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)
  880. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {}
  881. template<typename _Tp, int cn> inline
  882. 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)
  883. : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) {}
  884. template<typename _Tp, int cn> inline
  885. Vec<_Tp, cn>::Vec(const _Tp* values)
  886. : Matx<_Tp, cn, 1>(values) {}
  887. #ifdef CV_CXX11
  888. template<typename _Tp, int cn> inline
  889. Vec<_Tp, cn>::Vec(std::initializer_list<_Tp> list)
  890. : Matx<_Tp, cn, 1>(list) {}
  891. #endif
  892. template<typename _Tp, int cn> inline
  893. Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m)
  894. : Matx<_Tp, cn, 1>(m.val) {}
  895. template<typename _Tp, int cn> inline
  896. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp op)
  897. : Matx<_Tp, cn, 1>(a, b, op) {}
  898. template<typename _Tp, int cn> inline
  899. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp op)
  900. : Matx<_Tp, cn, 1>(a, b, op) {}
  901. template<typename _Tp, int cn> template<typename _T2> inline
  902. Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp op)
  903. : Matx<_Tp, cn, 1>(a, alpha, op) {}
  904. template<typename _Tp, int cn> inline
  905. Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha)
  906. {
  907. Vec v;
  908. for( int i = 0; i < cn; i++ ) v.val[i] = alpha;
  909. return v;
  910. }
  911. template<typename _Tp, int cn> inline
  912. Vec<_Tp, cn> Vec<_Tp, cn>::ones()
  913. {
  914. return Vec::all(1);
  915. }
  916. template<typename _Tp, int cn> inline
  917. Vec<_Tp, cn> Vec<_Tp, cn>::zeros()
  918. {
  919. return Vec::all(0);
  920. }
  921. template<typename _Tp, int cn> inline
  922. Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const
  923. {
  924. Vec<_Tp, cn> w;
  925. for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(this->val[i]*v.val[i]);
  926. return w;
  927. }
  928. template<> inline
  929. Vec<float, 2> Vec<float, 2>::conj() const
  930. {
  931. return cv::internal::conjugate(*this);
  932. }
  933. template<> inline
  934. Vec<double, 2> Vec<double, 2>::conj() const
  935. {
  936. return cv::internal::conjugate(*this);
  937. }
  938. template<> inline
  939. Vec<float, 4> Vec<float, 4>::conj() const
  940. {
  941. return cv::internal::conjugate(*this);
  942. }
  943. template<> inline
  944. Vec<double, 4> Vec<double, 4>::conj() const
  945. {
  946. return cv::internal::conjugate(*this);
  947. }
  948. template<typename _Tp, int cn> inline
  949. Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>&) const
  950. {
  951. CV_StaticAssert(cn == 3, "for arbitrary-size vector there is no cross-product defined");
  952. return Vec<_Tp, cn>();
  953. }
  954. template<> inline
  955. Vec<float, 3> Vec<float, 3>::cross(const Vec<float, 3>& v) const
  956. {
  957. return Vec<float,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
  958. this->val[2]*v.val[0] - this->val[0]*v.val[2],
  959. this->val[0]*v.val[1] - this->val[1]*v.val[0]);
  960. }
  961. template<> inline
  962. Vec<double, 3> Vec<double, 3>::cross(const Vec<double, 3>& v) const
  963. {
  964. return Vec<double,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
  965. this->val[2]*v.val[0] - this->val[0]*v.val[2],
  966. this->val[0]*v.val[1] - this->val[1]*v.val[0]);
  967. }
  968. template<typename _Tp, int cn> template<typename T2> inline
  969. Vec<_Tp, cn>::operator Vec<T2, cn>() const
  970. {
  971. Vec<T2, cn> v;
  972. for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast<T2>(this->val[i]);
  973. return v;
  974. }
  975. template<typename _Tp, int cn> inline
  976. const _Tp& Vec<_Tp, cn>::operator [](int i) const
  977. {
  978. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  979. return this->val[i];
  980. }
  981. template<typename _Tp, int cn> inline
  982. _Tp& Vec<_Tp, cn>::operator [](int i)
  983. {
  984. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  985. return this->val[i];
  986. }
  987. template<typename _Tp, int cn> inline
  988. const _Tp& Vec<_Tp, cn>::operator ()(int i) const
  989. {
  990. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  991. return this->val[i];
  992. }
  993. template<typename _Tp, int cn> inline
  994. _Tp& Vec<_Tp, cn>::operator ()(int i)
  995. {
  996. CV_DbgAssert( (unsigned)i < (unsigned)cn );
  997. return this->val[i];
  998. }
  999. template<typename _Tp, int cn> inline
  1000. Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v)
  1001. {
  1002. double nv = norm(v);
  1003. return v * (nv ? 1./nv : 0.);
  1004. }
  1005. //////////////////////////////// vec comma initializer //////////////////////////////////
  1006. template<typename _Tp, typename _T2, int cn> static inline
  1007. VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val)
  1008. {
  1009. VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec);
  1010. return (commaInitializer, val);
  1011. }
  1012. template<typename _Tp, int cn> inline
  1013. VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec)
  1014. : MatxCommaInitializer<_Tp, cn, 1>(_vec)
  1015. {}
  1016. template<typename _Tp, int cn> template<typename _T2> inline
  1017. VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value)
  1018. {
  1019. CV_DbgAssert( this->idx < cn );
  1020. this->dst->val[this->idx++] = saturate_cast<_Tp>(value);
  1021. return *this;
  1022. }
  1023. template<typename _Tp, int cn> inline
  1024. Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const
  1025. {
  1026. CV_DbgAssert( this->idx == cn );
  1027. return *this->dst;
  1028. }
  1029. //! @endcond
  1030. ///////////////////////////// Matx out-of-class operators ////////////////////////////////
  1031. //! @relates cv::Matx
  1032. //! @{
  1033. template<typename _Tp1, typename _Tp2, int m, int n> static inline
  1034. Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
  1035. {
  1036. for( int i = 0; i < m*n; i++ )
  1037. a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
  1038. return a;
  1039. }
  1040. template<typename _Tp1, typename _Tp2, int m, int n> static inline
  1041. Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
  1042. {
  1043. for( int i = 0; i < m*n; i++ )
  1044. a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
  1045. return a;
  1046. }
  1047. template<typename _Tp, int m, int n> static inline
  1048. Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1049. {
  1050. return Matx<_Tp, m, n>(a, b, Matx_AddOp());
  1051. }
  1052. template<typename _Tp, int m, int n> static inline
  1053. Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1054. {
  1055. return Matx<_Tp, m, n>(a, b, Matx_SubOp());
  1056. }
  1057. template<typename _Tp, int m, int n> static inline
  1058. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha)
  1059. {
  1060. for( int i = 0; i < m*n; i++ )
  1061. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1062. return a;
  1063. }
  1064. template<typename _Tp, int m, int n> static inline
  1065. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha)
  1066. {
  1067. for( int i = 0; i < m*n; i++ )
  1068. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1069. return a;
  1070. }
  1071. template<typename _Tp, int m, int n> static inline
  1072. Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha)
  1073. {
  1074. for( int i = 0; i < m*n; i++ )
  1075. a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
  1076. return a;
  1077. }
  1078. template<typename _Tp, int m, int n> static inline
  1079. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int 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 * (const Matx<_Tp, m, n>& a, float alpha)
  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 * (const Matx<_Tp, m, n>& a, double alpha)
  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 * (int 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 * (float alpha, const Matx<_Tp, m, n>& a)
  1100. {
  1101. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1102. }
  1103. template<typename _Tp, int m, int n> static inline
  1104. Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a)
  1105. {
  1106. return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
  1107. }
  1108. template<typename _Tp, int m, int n> static inline
  1109. Matx<_Tp, m, n>& operator /= (Matx<_Tp, m, n>& a, float alpha)
  1110. {
  1111. for( int i = 0; i < m*n; i++ )
  1112. a.val[i] = a.val[i] / alpha;
  1113. return a;
  1114. }
  1115. template<typename _Tp, int m, int n> static inline
  1116. Matx<_Tp, m, n>& operator /= (Matx<_Tp, m, n>& a, double alpha)
  1117. {
  1118. for( int i = 0; i < m*n; i++ )
  1119. a.val[i] = a.val[i] / alpha;
  1120. return a;
  1121. }
  1122. template<typename _Tp, int m, int n> static inline
  1123. Matx<_Tp, m, n> operator / (const Matx<_Tp, m, n>& a, float alpha)
  1124. {
  1125. return Matx<_Tp, m, n>(a, 1.f/alpha, Matx_ScaleOp());
  1126. }
  1127. template<typename _Tp, int m, int n> static inline
  1128. Matx<_Tp, m, n> operator / (const Matx<_Tp, m, n>& a, double alpha)
  1129. {
  1130. return Matx<_Tp, m, n>(a, 1./alpha, Matx_ScaleOp());
  1131. }
  1132. template<typename _Tp, int m, int n> static inline
  1133. Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a)
  1134. {
  1135. return Matx<_Tp, m, n>(a, -1, Matx_ScaleOp());
  1136. }
  1137. template<typename _Tp, int m, int n, int l> static inline
  1138. Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b)
  1139. {
  1140. return Matx<_Tp, m, n>(a, b, Matx_MatMulOp());
  1141. }
  1142. template<typename _Tp, int m, int n> static inline
  1143. Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b)
  1144. {
  1145. Matx<_Tp, m, 1> c(a, b, Matx_MatMulOp());
  1146. return (const Vec<_Tp, m>&)(c);
  1147. }
  1148. template<typename _Tp, int m, int n> static inline
  1149. bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1150. {
  1151. for( int i = 0; i < m*n; i++ )
  1152. if( a.val[i] != b.val[i] ) return false;
  1153. return true;
  1154. }
  1155. template<typename _Tp, int m, int n> static inline
  1156. bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
  1157. {
  1158. return !(a == b);
  1159. }
  1160. //! @}
  1161. ////////////////////////////// Vec out-of-class operators ////////////////////////////////
  1162. //! @relates cv::Vec
  1163. //! @{
  1164. template<typename _Tp1, typename _Tp2, int cn> static inline
  1165. Vec<_Tp1, cn>& operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
  1166. {
  1167. for( int i = 0; i < cn; i++ )
  1168. a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
  1169. return a;
  1170. }
  1171. template<typename _Tp1, typename _Tp2, int cn> static inline
  1172. Vec<_Tp1, cn>& operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
  1173. {
  1174. for( int i = 0; i < cn; i++ )
  1175. a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
  1176. return a;
  1177. }
  1178. template<typename _Tp, int cn> static inline
  1179. Vec<_Tp, cn> operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
  1180. {
  1181. return Vec<_Tp, cn>(a, b, Matx_AddOp());
  1182. }
  1183. template<typename _Tp, int cn> static inline
  1184. Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
  1185. {
  1186. return Vec<_Tp, cn>(a, b, Matx_SubOp());
  1187. }
  1188. template<typename _Tp, int cn> static inline
  1189. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, int alpha)
  1190. {
  1191. for( int i = 0; i < cn; i++ )
  1192. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1193. return a;
  1194. }
  1195. template<typename _Tp, int cn> static inline
  1196. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, float alpha)
  1197. {
  1198. for( int i = 0; i < cn; i++ )
  1199. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1200. return a;
  1201. }
  1202. template<typename _Tp, int cn> static inline
  1203. Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, double alpha)
  1204. {
  1205. for( int i = 0; i < cn; i++ )
  1206. a[i] = saturate_cast<_Tp>(a[i]*alpha);
  1207. return a;
  1208. }
  1209. template<typename _Tp, int cn> static inline
  1210. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, int alpha)
  1211. {
  1212. double ialpha = 1./alpha;
  1213. for( int i = 0; i < cn; i++ )
  1214. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1215. return a;
  1216. }
  1217. template<typename _Tp, int cn> static inline
  1218. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, float alpha)
  1219. {
  1220. float ialpha = 1.f/alpha;
  1221. for( int i = 0; i < cn; i++ )
  1222. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1223. return a;
  1224. }
  1225. template<typename _Tp, int cn> static inline
  1226. Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, double alpha)
  1227. {
  1228. double ialpha = 1./alpha;
  1229. for( int i = 0; i < cn; i++ )
  1230. a[i] = saturate_cast<_Tp>(a[i]*ialpha);
  1231. return a;
  1232. }
  1233. template<typename _Tp, int cn> static inline
  1234. Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, int 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 * (int 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, float 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 * (float 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, double alpha)
  1255. {
  1256. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1257. }
  1258. template<typename _Tp, int cn> static inline
  1259. Vec<_Tp, cn> operator * (double alpha, const Vec<_Tp, cn>& a)
  1260. {
  1261. return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
  1262. }
  1263. template<typename _Tp, int cn> static inline
  1264. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, int 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, float alpha)
  1270. {
  1271. return Vec<_Tp, cn>(a, 1.f/alpha, Matx_ScaleOp());
  1272. }
  1273. template<typename _Tp, int cn> static inline
  1274. Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, double alpha)
  1275. {
  1276. return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
  1277. }
  1278. template<typename _Tp, int cn> static inline
  1279. Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a)
  1280. {
  1281. Vec<_Tp,cn> t;
  1282. for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
  1283. return t;
  1284. }
  1285. template<typename _Tp> inline Vec<_Tp, 4> operator * (const Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
  1286. {
  1287. return Vec<_Tp, 4>(saturate_cast<_Tp>(v1[0]*v2[0] - v1[1]*v2[1] - v1[2]*v2[2] - v1[3]*v2[3]),
  1288. saturate_cast<_Tp>(v1[0]*v2[1] + v1[1]*v2[0] + v1[2]*v2[3] - v1[3]*v2[2]),
  1289. saturate_cast<_Tp>(v1[0]*v2[2] - v1[1]*v2[3] + v1[2]*v2[0] + v1[3]*v2[1]),
  1290. saturate_cast<_Tp>(v1[0]*v2[3] + v1[1]*v2[2] - v1[2]*v2[1] + v1[3]*v2[0]));
  1291. }
  1292. template<typename _Tp> inline Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
  1293. {
  1294. v1 = v1 * v2;
  1295. return v1;
  1296. }
  1297. //! @}
  1298. } // cv
  1299. #endif // OPENCV_CORE_MATX_HPP