matx.hpp 47 KB

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