matx.hpp 46 KB

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