1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #ifndef OPENCV_CORE_MATX_HPP
45 #define OPENCV_CORE_MATX_HPP
48 # error matx.hpp header must be compiled as C++
51 #include "opencv2/core/cvdef.h"
52 #include "opencv2/core/base.hpp"
53 #include "opencv2/core/traits.hpp"
54 #include "opencv2/core/saturate.hpp"
56 #include <initializer_list>
61 //! @addtogroup core_basic
64 ////////////////////////////// Small Matrix ///////////////////////////
67 // FIXIT Remove this (especially CV_EXPORTS modifier)
68 struct CV_EXPORTS Matx_AddOp { Matx_AddOp() {} Matx_AddOp(const Matx_AddOp&) {} };
69 struct CV_EXPORTS Matx_SubOp { Matx_SubOp() {} Matx_SubOp(const Matx_SubOp&) {} };
70 struct CV_EXPORTS Matx_ScaleOp { Matx_ScaleOp() {} Matx_ScaleOp(const Matx_ScaleOp&) {} };
71 struct CV_EXPORTS Matx_MulOp { Matx_MulOp() {} Matx_MulOp(const Matx_MulOp&) {} };
72 struct CV_EXPORTS Matx_DivOp { Matx_DivOp() {} Matx_DivOp(const Matx_DivOp&) {} };
73 struct CV_EXPORTS Matx_MatMulOp { Matx_MatMulOp() {} Matx_MatMulOp(const Matx_MatMulOp&) {} };
74 struct CV_EXPORTS Matx_TOp { Matx_TOp() {} Matx_TOp(const Matx_TOp&) {} };
77 /** @brief Template class for small matrices whose type and size are known at compilation time
79 If you need a more flexible type, use Mat . The elements of the matrix M are accessible using the
80 M(i,j) notation. Most of the common matrix operations (see also @ref MatrixExpressions ) are
81 available. To do an operation on Matx that is not implemented, you can easily convert the matrix to
87 cout << sum(Mat(m*m.t())) << endl;
89 Except of the plain constructor which takes a list of elements, Matx can be initialized from a C-array:
91 float values[] = { 1, 2, 3};
94 In case if C++11 features are available, std::initializer_list can be also used to initialize Matx:
96 Matx31f m = { 1, 2, 3};
99 template<typename _Tp, int m, int n> class Matx
105 channels = rows*cols,
106 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
107 depth = traits::Type<_Tp>::value,
108 type = CV_MAKETYPE(depth, channels),
110 shortdim = (m < n ? m : n)
113 typedef _Tp value_type;
114 typedef Matx<_Tp, m, n> mat_type;
115 typedef Matx<_Tp, shortdim, 1> diag_type;
117 //! default constructor
120 explicit Matx(_Tp v0); //!< 1x1 matrix
121 Matx(_Tp v0, _Tp v1); //!< 1x2 or 2x1 matrix
122 Matx(_Tp v0, _Tp v1, _Tp v2); //!< 1x3 or 3x1 matrix
123 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 1x4, 2x2 or 4x1 matrix
124 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 1x5 or 5x1 matrix
125 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 1x6, 2x3, 3x2 or 6x1 matrix
126 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 1x7 or 7x1 matrix
127 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 1x8, 2x4, 4x2 or 8x1 matrix
128 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 1x9, 3x3 or 9x1 matrix
129 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
130 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
131 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
132 _Tp v8, _Tp v9, _Tp v10, _Tp v11); //!< 1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 matrix
133 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
134 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
135 _Tp v8, _Tp v9, _Tp v10, _Tp v11,
136 _Tp v12, _Tp v13); //!< 1x14, 2x7, 7x2 or 14x1 matrix
137 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
138 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
139 _Tp v8, _Tp v9, _Tp v10, _Tp v11,
140 _Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix
141 explicit Matx(const _Tp* vals); //!< initialize from a plain array
143 Matx(std::initializer_list<_Tp>); //!< initialize from an initializer list
145 static Matx all(_Tp alpha);
149 static Matx diag(const diag_type& d);
150 static Matx randu(_Tp a, _Tp b);
151 static Matx randn(_Tp a, _Tp b);
153 //! dot product computed with the default precision
154 _Tp dot(const Matx<_Tp, m, n>& v) const;
156 //! dot product computed in double-precision arithmetics
157 double ddot(const Matx<_Tp, m, n>& v) const;
159 //! conversion to another data type
160 template<typename T2> operator Matx<T2, m, n>() const;
162 //! change the matrix shape
163 template<int m1, int n1> Matx<_Tp, m1, n1> reshape() const;
165 //! extract part of the matrix
166 template<int m1, int n1> Matx<_Tp, m1, n1> get_minor(int i, int j) const;
168 //! extract the matrix row
169 Matx<_Tp, 1, n> row(int i) const;
171 //! extract the matrix column
172 Matx<_Tp, m, 1> col(int i) const;
174 //! extract the matrix diagonal
175 diag_type diag() const;
177 //! transpose the matrix
178 Matx<_Tp, n, m> t() const;
180 //! invert the matrix
181 Matx<_Tp, n, m> inv(int method=DECOMP_LU, bool *p_is_ok = NULL) const;
183 //! solve linear system
184 template<int l> Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const;
185 Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const;
187 //! multiply two matrices element-wise
188 Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const;
190 //! divide two matrices element-wise
191 Matx<_Tp, m, n> div(const Matx<_Tp, m, n>& a) const;
194 const _Tp& operator ()(int i, int j) const;
195 _Tp& operator ()(int i, int j);
197 //! 1D element access
198 const _Tp& operator ()(int i) const;
199 _Tp& operator ()(int i);
201 Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp);
202 Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp);
203 template<typename _T2> Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp);
204 Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp);
205 Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp);
206 template<int l> Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp);
207 Matx(const Matx<_Tp, n, m>& a, Matx_TOp);
209 _Tp val[m*n]; //< matrix elements
212 typedef Matx<float, 1, 2> Matx12f;
213 typedef Matx<double, 1, 2> Matx12d;
214 typedef Matx<float, 1, 3> Matx13f;
215 typedef Matx<double, 1, 3> Matx13d;
216 typedef Matx<float, 1, 4> Matx14f;
217 typedef Matx<double, 1, 4> Matx14d;
218 typedef Matx<float, 1, 6> Matx16f;
219 typedef Matx<double, 1, 6> Matx16d;
221 typedef Matx<float, 2, 1> Matx21f;
222 typedef Matx<double, 2, 1> Matx21d;
223 typedef Matx<float, 3, 1> Matx31f;
224 typedef Matx<double, 3, 1> Matx31d;
225 typedef Matx<float, 4, 1> Matx41f;
226 typedef Matx<double, 4, 1> Matx41d;
227 typedef Matx<float, 6, 1> Matx61f;
228 typedef Matx<double, 6, 1> Matx61d;
230 typedef Matx<float, 2, 2> Matx22f;
231 typedef Matx<double, 2, 2> Matx22d;
232 typedef Matx<float, 2, 3> Matx23f;
233 typedef Matx<double, 2, 3> Matx23d;
234 typedef Matx<float, 3, 2> Matx32f;
235 typedef Matx<double, 3, 2> Matx32d;
237 typedef Matx<float, 3, 3> Matx33f;
238 typedef Matx<double, 3, 3> Matx33d;
240 typedef Matx<float, 3, 4> Matx34f;
241 typedef Matx<double, 3, 4> Matx34d;
242 typedef Matx<float, 4, 3> Matx43f;
243 typedef Matx<double, 4, 3> Matx43d;
245 typedef Matx<float, 4, 4> Matx44f;
246 typedef Matx<double, 4, 4> Matx44d;
247 typedef Matx<float, 6, 6> Matx66f;
248 typedef Matx<double, 6, 6> Matx66d;
253 template<typename _Tp, int m, int n> class DataType< Matx<_Tp, m, n> >
256 typedef Matx<_Tp, m, n> value_type;
257 typedef Matx<typename DataType<_Tp>::work_type, m, n> work_type;
258 typedef _Tp channel_type;
259 typedef value_type vec_type;
261 enum { generic_type = 0,
263 fmt = traits::SafeFmt<channel_type>::fmt + ((channels - 1) << 8)
264 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
265 ,depth = DataType<channel_type>::depth
266 ,type = CV_MAKETYPE(depth, channels)
272 template<typename _Tp, int m, int n>
273 struct Depth< Matx<_Tp, m, n> > { enum { value = Depth<_Tp>::value }; };
274 template<typename _Tp, int m, int n>
275 struct Type< Matx<_Tp, m, n> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, n*m) }; };
279 /** @brief Comma-separated Matrix Initializer
281 template<typename _Tp, int m, int n> class MatxCommaInitializer
284 MatxCommaInitializer(Matx<_Tp, m, n>* _mtx);
285 template<typename T2> MatxCommaInitializer<_Tp, m, n>& operator , (T2 val);
286 Matx<_Tp, m, n> operator *() const;
288 Matx<_Tp, m, n>* dst;
295 template<typename _Tp, int m> static double determinant(const Matx<_Tp, m, m>& a);
296 template<typename _Tp, int m, int n> static double trace(const Matx<_Tp, m, n>& a);
297 template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M);
298 template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M, int normType);
302 /////////////////////// Vec (used as element of multi-channel images /////////////////////
304 /** @brief Template class for short numerical vectors, a partial case of Matx
306 This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) on which you
307 can perform basic arithmetical operations, access individual elements using [] operator etc. The
308 vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., which
309 elements are dynamically allocated in the heap.
311 The template takes 2 parameters:
312 @tparam _Tp element type
313 @tparam cn the number of elements
315 In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
316 for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
318 It is possible to convert Vec\<T,2\> to/from Point_, Vec\<T,3\> to/from Point3_ , and Vec\<T,4\>
319 to CvScalar or Scalar_. Use operator[] to access the elements of Vec.
321 All the expected vector operations are also implemented:
327 - v1 += v2 and other augmenting operations
329 - norm(v1) (euclidean norm)
330 The Vec class is commonly used to describe pixel types of multi-channel arrays. See Mat for details.
332 template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>
335 typedef _Tp value_type;
338 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
339 depth = Matx<_Tp, cn, 1>::depth,
340 type = CV_MAKETYPE(depth, channels),
342 _dummy_enum_finalizer = 0
345 //! default constructor
348 Vec(_Tp v0); //!< 1-element vector constructor
349 Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor
350 Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor
351 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor
352 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor
353 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor
354 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor
355 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor
356 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor
357 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
358 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
359 explicit Vec(const _Tp* values);
361 Vec(std::initializer_list<_Tp>);
363 Vec(const Vec<_Tp, cn>& v);
365 static Vec all(_Tp alpha);
367 //! per-element multiplication
368 Vec mul(const Vec<_Tp, cn>& v) const;
370 //! conjugation (makes sense for complex numbers and quaternions)
374 cross product of the two 3D vectors.
376 For other dimensionalities the exception is raised
378 Vec cross(const Vec& v) const;
379 //! conversion to another data type
380 template<typename T2> operator Vec<T2, cn>() const;
382 /*! element access */
383 const _Tp& operator [](int i) const;
384 _Tp& operator[](int i);
385 const _Tp& operator ()(int i) const;
386 _Tp& operator ()(int i);
388 Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp);
389 Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp);
390 template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);
393 /** @name Shorter aliases for the most popular specializations of Vec<T,n>
396 typedef Vec<uchar, 2> Vec2b;
397 typedef Vec<uchar, 3> Vec3b;
398 typedef Vec<uchar, 4> Vec4b;
400 typedef Vec<short, 2> Vec2s;
401 typedef Vec<short, 3> Vec3s;
402 typedef Vec<short, 4> Vec4s;
404 typedef Vec<ushort, 2> Vec2w;
405 typedef Vec<ushort, 3> Vec3w;
406 typedef Vec<ushort, 4> Vec4w;
408 typedef Vec<int, 2> Vec2i;
409 typedef Vec<int, 3> Vec3i;
410 typedef Vec<int, 4> Vec4i;
411 typedef Vec<int, 6> Vec6i;
412 typedef Vec<int, 8> Vec8i;
414 typedef Vec<float, 2> Vec2f;
415 typedef Vec<float, 3> Vec3f;
416 typedef Vec<float, 4> Vec4f;
417 typedef Vec<float, 6> Vec6f;
419 typedef Vec<double, 2> Vec2d;
420 typedef Vec<double, 3> Vec3d;
421 typedef Vec<double, 4> Vec4d;
422 typedef Vec<double, 6> Vec6d;
428 template<typename _Tp, int cn> class DataType< Vec<_Tp, cn> >
431 typedef Vec<_Tp, cn> value_type;
432 typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
433 typedef _Tp channel_type;
434 typedef value_type vec_type;
436 enum { generic_type = 0,
438 fmt = DataType<channel_type>::fmt + ((channels - 1) << 8),
439 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
440 depth = DataType<channel_type>::depth,
441 type = CV_MAKETYPE(depth, channels),
443 _dummy_enum_finalizer = 0
448 template<typename _Tp, int cn>
449 struct Depth< Vec<_Tp, cn> > { enum { value = Depth<_Tp>::value }; };
450 template<typename _Tp, int cn>
451 struct Type< Vec<_Tp, cn> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, cn) }; };
455 /** @brief Comma-separated Vec Initializer
457 template<typename _Tp, int m> class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1>
460 VecCommaInitializer(Vec<_Tp, m>* _vec);
461 template<typename T2> VecCommaInitializer<_Tp, m>& operator , (T2 val);
462 Vec<_Tp, m> operator *() const;
465 template<typename _Tp, int cn> static Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v);
471 ///////////////////////////////////// helper classes /////////////////////////////////////
475 template<typename _Tp, int m> struct Matx_DetOp
477 double operator ()(const Matx<_Tp, m, m>& a) const
479 Matx<_Tp, m, m> temp = a;
480 double p = LU(temp.val, m*sizeof(_Tp), m, 0, 0, 0);
483 for( int i = 0; i < m; i++ )
489 template<typename _Tp> struct Matx_DetOp<_Tp, 1>
491 double operator ()(const Matx<_Tp, 1, 1>& a) const
497 template<typename _Tp> struct Matx_DetOp<_Tp, 2>
499 double operator ()(const Matx<_Tp, 2, 2>& a) const
501 return a(0,0)*a(1,1) - a(0,1)*a(1,0);
505 template<typename _Tp> struct Matx_DetOp<_Tp, 3>
507 double operator ()(const Matx<_Tp, 3, 3>& a) const
509 return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
510 a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
511 a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1));
515 template<typename _Tp> Vec<_Tp, 2> inline conjugate(const Vec<_Tp, 2>& v)
517 return Vec<_Tp, 2>(v[0], -v[1]);
520 template<typename _Tp> Vec<_Tp, 4> inline conjugate(const Vec<_Tp, 4>& v)
522 return Vec<_Tp, 4>(v[0], -v[1], -v[2], -v[3]);
529 ////////////////////////////////// Matx Implementation ///////////////////////////////////
531 template<typename _Tp, int m, int n> inline
532 Matx<_Tp, m, n>::Matx()
534 for(int i = 0; i < channels; i++) val[i] = _Tp(0);
537 template<typename _Tp, int m, int n> inline
538 Matx<_Tp, m, n>::Matx(_Tp v0)
541 for(int i = 1; i < channels; i++) val[i] = _Tp(0);
544 template<typename _Tp, int m, int n> inline
545 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1)
547 CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
548 val[0] = v0; val[1] = v1;
549 for(int i = 2; i < channels; i++) val[i] = _Tp(0);
552 template<typename _Tp, int m, int n> inline
553 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2)
555 CV_StaticAssert(channels >= 3, "Matx should have at least 3 elements.");
556 val[0] = v0; val[1] = v1; val[2] = v2;
557 for(int i = 3; i < channels; i++) val[i] = _Tp(0);
560 template<typename _Tp, int m, int n> inline
561 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
563 CV_StaticAssert(channels >= 4, "Matx should have at least 4 elements.");
564 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
565 for(int i = 4; i < channels; i++) val[i] = _Tp(0);
568 template<typename _Tp, int m, int n> inline
569 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
571 CV_StaticAssert(channels >= 5, "Matx should have at least 5 elements.");
572 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
573 for(int i = 5; i < channels; i++) val[i] = _Tp(0);
576 template<typename _Tp, int m, int n> inline
577 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
579 CV_StaticAssert(channels >= 6, "Matx should have at least 6 elements.");
580 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
581 val[4] = v4; val[5] = v5;
582 for(int i = 6; i < channels; i++) val[i] = _Tp(0);
585 template<typename _Tp, int m, int n> inline
586 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
588 CV_StaticAssert(channels >= 7, "Matx should have at least 7 elements.");
589 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
590 val[4] = v4; val[5] = v5; val[6] = v6;
591 for(int i = 7; i < channels; i++) val[i] = _Tp(0);
594 template<typename _Tp, int m, int n> inline
595 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
597 CV_StaticAssert(channels >= 8, "Matx should have at least 8 elements.");
598 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
599 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
600 for(int i = 8; i < channels; i++) val[i] = _Tp(0);
603 template<typename _Tp, int m, int n> inline
604 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
606 CV_StaticAssert(channels >= 9, "Matx should have at least 9 elements.");
607 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
608 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
610 for(int i = 9; i < channels; i++) val[i] = _Tp(0);
613 template<typename _Tp, int m, int n> inline
614 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)
616 CV_StaticAssert(channels >= 10, "Matx should have at least 10 elements.");
617 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
618 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
619 val[8] = v8; val[9] = v9;
620 for(int i = 10; i < channels; i++) val[i] = _Tp(0);
624 template<typename _Tp, int m, int n> inline
625 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)
627 CV_StaticAssert(channels >= 12, "Matx should have at least 12 elements.");
628 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
629 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
630 val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
631 for(int i = 12; i < channels; i++) val[i] = _Tp(0);
634 template<typename _Tp, int m, int n> inline
635 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)
637 CV_StaticAssert(channels >= 14, "Matx should have at least 14 elements.");
638 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
639 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
640 val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
641 val[12] = v12; val[13] = v13;
642 for (int i = 14; i < channels; i++) val[i] = _Tp(0);
646 template<typename _Tp, int m, int n> inline
647 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)
649 CV_StaticAssert(channels >= 16, "Matx should have at least 16 elements.");
650 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
651 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
652 val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
653 val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15;
654 for(int i = 16; i < channels; i++) val[i] = _Tp(0);
657 template<typename _Tp, int m, int n> inline
658 Matx<_Tp, m, n>::Matx(const _Tp* values)
660 for( int i = 0; i < channels; i++ ) val[i] = values[i];
663 template<typename _Tp, int m, int n> inline
664 Matx<_Tp, m, n>::Matx(std::initializer_list<_Tp> list)
666 CV_DbgAssert(list.size() == channels);
668 for(const auto& elem : list)
674 template<typename _Tp, int m, int n> inline
675 Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha)
678 for( int i = 0; i < m*n; i++ ) M.val[i] = alpha;
682 template<typename _Tp, int m, int n> inline
683 Matx<_Tp,m,n> Matx<_Tp,m,n>::zeros()
688 template<typename _Tp, int m, int n> inline
689 Matx<_Tp,m,n> Matx<_Tp,m,n>::ones()
694 template<typename _Tp, int m, int n> inline
695 Matx<_Tp,m,n> Matx<_Tp,m,n>::eye()
698 for(int i = 0; i < shortdim; i++)
703 template<typename _Tp, int m, int n> inline
704 _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const
707 for( int i = 0; i < channels; i++ ) s += val[i]*M.val[i];
711 template<typename _Tp, int m, int n> inline
712 double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const
715 for( int i = 0; i < channels; i++ ) s += (double)val[i]*M.val[i];
719 template<typename _Tp, int m, int n> inline
720 Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const typename Matx<_Tp,m,n>::diag_type& d)
723 for(int i = 0; i < shortdim; i++)
728 template<typename _Tp, int m, int n> template<typename T2>
729 inline Matx<_Tp, m, n>::operator Matx<T2, m, n>() const
732 for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast<T2>(val[i]);
736 template<typename _Tp, int m, int n> template<int m1, int n1> inline
737 Matx<_Tp, m1, n1> Matx<_Tp, m, n>::reshape() const
739 CV_StaticAssert(m1*n1 == m*n, "Input and destnarion matrices must have the same number of elements");
740 return (const Matx<_Tp, m1, n1>&)*this;
743 template<typename _Tp, int m, int n>
744 template<int m1, int n1> inline
745 Matx<_Tp, m1, n1> Matx<_Tp, m, n>::get_minor(int i, int j) const
747 CV_DbgAssert(0 <= i && i+m1 <= m && 0 <= j && j+n1 <= n);
749 for( int di = 0; di < m1; di++ )
750 for( int dj = 0; dj < n1; dj++ )
751 s(di, dj) = (*this)(i+di, j+dj);
755 template<typename _Tp, int m, int n> inline
756 Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const
758 CV_DbgAssert((unsigned)i < (unsigned)m);
759 return Matx<_Tp, 1, n>(&val[i*n]);
762 template<typename _Tp, int m, int n> inline
763 Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const
765 CV_DbgAssert((unsigned)j < (unsigned)n);
767 for( int i = 0; i < m; i++ )
768 v.val[i] = val[i*n + j];
772 template<typename _Tp, int m, int n> inline
773 typename Matx<_Tp, m, n>::diag_type Matx<_Tp, m, n>::diag() const
776 for( int i = 0; i < shortdim; i++ )
777 d.val[i] = val[i*n + i];
781 template<typename _Tp, int m, int n> inline
782 const _Tp& Matx<_Tp, m, n>::operator()(int i, int j) const
784 CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n );
785 return this->val[i*n + j];
788 template<typename _Tp, int m, int n> inline
789 _Tp& Matx<_Tp, m, n>::operator ()(int i, int j)
791 CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n );
795 template<typename _Tp, int m, int n> inline
796 const _Tp& Matx<_Tp, m, n>::operator ()(int i) const
798 CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
799 CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
803 template<typename _Tp, int m, int n> inline
804 _Tp& Matx<_Tp, m, n>::operator ()(int i)
806 CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
807 CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
811 template<typename _Tp, int m, int n> inline
812 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp)
814 for( int i = 0; i < channels; i++ )
815 val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]);
818 template<typename _Tp, int m, int n> inline
819 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp)
821 for( int i = 0; i < channels; i++ )
822 val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]);
825 template<typename _Tp, int m, int n> template<typename _T2> inline
826 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp)
828 for( int i = 0; i < channels; i++ )
829 val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
832 template<typename _Tp, int m, int n> inline
833 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp)
835 for( int i = 0; i < channels; i++ )
836 val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]);
839 template<typename _Tp, int m, int n> inline
840 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp)
842 for( int i = 0; i < channels; i++ )
843 val[i] = saturate_cast<_Tp>(a.val[i] / b.val[i]);
846 template<typename _Tp, int m, int n> template<int l> inline
847 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp)
849 for( int i = 0; i < m; i++ )
850 for( int j = 0; j < n; j++ )
853 for( int k = 0; k < l; k++ )
854 s += a(i, k) * b(k, j);
859 template<typename _Tp, int m, int n> inline
860 Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp)
862 for( int i = 0; i < m; i++ )
863 for( int j = 0; j < n; j++ )
864 val[i*n + j] = a(j, i);
867 template<typename _Tp, int m, int n> inline
868 Matx<_Tp, m, n> Matx<_Tp, m, n>::mul(const Matx<_Tp, m, n>& a) const
870 return Matx<_Tp, m, n>(*this, a, Matx_MulOp());
873 template<typename _Tp, int m, int n> inline
874 Matx<_Tp, m, n> Matx<_Tp, m, n>::div(const Matx<_Tp, m, n>& a) const
876 return Matx<_Tp, m, n>(*this, a, Matx_DivOp());
879 template<typename _Tp, int m, int n> inline
880 Matx<_Tp, n, m> Matx<_Tp, m, n>::t() const
882 return Matx<_Tp, n, m>(*this, Matx_TOp());
885 template<typename _Tp, int m, int n> inline
886 Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const
888 Matx<_Tp, n, 1> x = solve((const Matx<_Tp, m, 1>&)(rhs), method);
889 return (Vec<_Tp, n>&)(x);
892 template<typename _Tp, int m> static inline
893 double determinant(const Matx<_Tp, m, m>& a)
895 return cv::internal::Matx_DetOp<_Tp, m>()(a);
898 template<typename _Tp, int m, int n> static inline
899 double trace(const Matx<_Tp, m, n>& a)
902 for( int i = 0; i < std::min(m, n); i++ )
907 template<typename _Tp, int m, int n> static inline
908 double norm(const Matx<_Tp, m, n>& M)
910 return std::sqrt(normL2Sqr<_Tp, double>(M.val, m*n));
913 template<typename _Tp, int m, int n> static inline
914 double norm(const Matx<_Tp, m, n>& M, int normType)
918 return (double)normInf<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
920 return (double)normL1<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
922 return (double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
925 return std::sqrt((double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n));
931 //////////////////////////////// matx comma initializer //////////////////////////////////
933 template<typename _Tp, typename _T2, int m, int n> static inline
934 MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val)
936 MatxCommaInitializer<_Tp, m, n> commaInitializer((Matx<_Tp, m, n>*)&mtx);
937 return (commaInitializer, val);
940 template<typename _Tp, int m, int n> inline
941 MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx)
945 template<typename _Tp, int m, int n> template<typename _T2> inline
946 MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value)
948 CV_DbgAssert( idx < m*n );
949 dst->val[idx++] = saturate_cast<_Tp>(value);
953 template<typename _Tp, int m, int n> inline
954 Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const
956 CV_DbgAssert( idx == n*m );
962 /////////////////////////////////// Vec Implementation ///////////////////////////////////
964 template<typename _Tp, int cn> inline
965 Vec<_Tp, cn>::Vec() {}
967 template<typename _Tp, int cn> inline
968 Vec<_Tp, cn>::Vec(_Tp v0)
969 : Matx<_Tp, cn, 1>(v0) {}
971 template<typename _Tp, int cn> inline
972 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1)
973 : Matx<_Tp, cn, 1>(v0, v1) {}
975 template<typename _Tp, int cn> inline
976 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
977 : Matx<_Tp, cn, 1>(v0, v1, v2) {}
979 template<typename _Tp, int cn> inline
980 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
981 : Matx<_Tp, cn, 1>(v0, v1, v2, v3) {}
983 template<typename _Tp, int cn> inline
984 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
985 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) {}
987 template<typename _Tp, int cn> inline
988 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
989 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) {}
991 template<typename _Tp, int cn> inline
992 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
993 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) {}
995 template<typename _Tp, int cn> inline
996 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
997 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) {}
999 template<typename _Tp, int cn> inline
1000 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
1001 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) {}
1003 template<typename _Tp, int cn> inline
1004 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)
1005 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {}
1007 template<typename _Tp, int cn> inline
1008 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)
1009 : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) {}
1011 template<typename _Tp, int cn> inline
1012 Vec<_Tp, cn>::Vec(const _Tp* values)
1013 : Matx<_Tp, cn, 1>(values) {}
1015 template<typename _Tp, int cn> inline
1016 Vec<_Tp, cn>::Vec(std::initializer_list<_Tp> list)
1017 : Matx<_Tp, cn, 1>(list) {}
1019 template<typename _Tp, int cn> inline
1020 Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m)
1021 : Matx<_Tp, cn, 1>(m.val) {}
1023 template<typename _Tp, int cn> inline
1024 Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp op)
1025 : Matx<_Tp, cn, 1>(a, b, op) {}
1027 template<typename _Tp, int cn> inline
1028 Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp op)
1029 : Matx<_Tp, cn, 1>(a, b, op) {}
1031 template<typename _Tp, int cn> template<typename _T2> inline
1032 Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp op)
1033 : Matx<_Tp, cn, 1>(a, alpha, op) {}
1035 template<typename _Tp, int cn> inline
1036 Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha)
1039 for( int i = 0; i < cn; i++ ) v.val[i] = alpha;
1043 template<typename _Tp, int cn> inline
1044 Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const
1047 for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(this->val[i]*v.val[i]);
1052 Vec<float, 2> Vec<float, 2>::conj() const
1054 return cv::internal::conjugate(*this);
1058 Vec<double, 2> Vec<double, 2>::conj() const
1060 return cv::internal::conjugate(*this);
1064 Vec<float, 4> Vec<float, 4>::conj() const
1066 return cv::internal::conjugate(*this);
1070 Vec<double, 4> Vec<double, 4>::conj() const
1072 return cv::internal::conjugate(*this);
1075 template<typename _Tp, int cn> inline
1076 Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>&) const
1078 CV_StaticAssert(cn == 3, "for arbitrary-size vector there is no cross-product defined");
1079 return Vec<_Tp, cn>();
1083 Vec<float, 3> Vec<float, 3>::cross(const Vec<float, 3>& v) const
1085 return Vec<float,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
1086 this->val[2]*v.val[0] - this->val[0]*v.val[2],
1087 this->val[0]*v.val[1] - this->val[1]*v.val[0]);
1091 Vec<double, 3> Vec<double, 3>::cross(const Vec<double, 3>& v) const
1093 return Vec<double,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
1094 this->val[2]*v.val[0] - this->val[0]*v.val[2],
1095 this->val[0]*v.val[1] - this->val[1]*v.val[0]);
1098 template<typename _Tp, int cn> template<typename T2> inline
1099 Vec<_Tp, cn>::operator Vec<T2, cn>() const
1102 for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast<T2>(this->val[i]);
1106 template<typename _Tp, int cn> inline
1107 const _Tp& Vec<_Tp, cn>::operator [](int i) const
1109 CV_DbgAssert( (unsigned)i < (unsigned)cn );
1110 return this->val[i];
1113 template<typename _Tp, int cn> inline
1114 _Tp& Vec<_Tp, cn>::operator [](int i)
1116 CV_DbgAssert( (unsigned)i < (unsigned)cn );
1117 return this->val[i];
1120 template<typename _Tp, int cn> inline
1121 const _Tp& Vec<_Tp, cn>::operator ()(int i) const
1123 CV_DbgAssert( (unsigned)i < (unsigned)cn );
1124 return this->val[i];
1127 template<typename _Tp, int cn> inline
1128 _Tp& Vec<_Tp, cn>::operator ()(int i)
1130 CV_DbgAssert( (unsigned)i < (unsigned)cn );
1131 return this->val[i];
1134 template<typename _Tp, int cn> inline
1135 Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v)
1137 double nv = norm(v);
1138 return v * (nv ? 1./nv : 0.);
1143 //////////////////////////////// vec comma initializer //////////////////////////////////
1146 template<typename _Tp, typename _T2, int cn> static inline
1147 VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val)
1149 VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec);
1150 return (commaInitializer, val);
1153 template<typename _Tp, int cn> inline
1154 VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec)
1155 : MatxCommaInitializer<_Tp, cn, 1>(_vec)
1158 template<typename _Tp, int cn> template<typename _T2> inline
1159 VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value)
1161 CV_DbgAssert( this->idx < cn );
1162 this->dst->val[this->idx++] = saturate_cast<_Tp>(value);
1166 template<typename _Tp, int cn> inline
1167 Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const
1169 CV_DbgAssert( this->idx == cn );
1175 ///////////////////////////// Matx out-of-class operators ////////////////////////////////
1177 //! @relates cv::Matx
1180 template<typename _Tp1, typename _Tp2, int m, int n> static inline
1181 Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
1183 for( int i = 0; i < m*n; i++ )
1184 a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
1188 template<typename _Tp1, typename _Tp2, int m, int n> static inline
1189 Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
1191 for( int i = 0; i < m*n; i++ )
1192 a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
1196 template<typename _Tp, int m, int n> static inline
1197 Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1199 return Matx<_Tp, m, n>(a, b, Matx_AddOp());
1202 template<typename _Tp, int m, int n> static inline
1203 Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1205 return Matx<_Tp, m, n>(a, b, Matx_SubOp());
1208 template<typename _Tp, int m, int n> static inline
1209 Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha)
1211 for( int i = 0; i < m*n; i++ )
1212 a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
1216 template<typename _Tp, int m, int n> static inline
1217 Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha)
1219 for( int i = 0; i < m*n; i++ )
1220 a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
1224 template<typename _Tp, int m, int n> static inline
1225 Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha)
1227 for( int i = 0; i < m*n; i++ )
1228 a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
1232 template<typename _Tp, int m, int n> static inline
1233 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int alpha)
1235 return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1238 template<typename _Tp, int m, int n> static inline
1239 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, float alpha)
1241 return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1244 template<typename _Tp, int m, int n> static inline
1245 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, double alpha)
1247 return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1250 template<typename _Tp, int m, int n> static inline
1251 Matx<_Tp, m, n> operator * (int alpha, const Matx<_Tp, m, n>& a)
1253 return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1256 template<typename _Tp, int m, int n> static inline
1257 Matx<_Tp, m, n> operator * (float alpha, const Matx<_Tp, m, n>& a)
1259 return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1262 template<typename _Tp, int m, int n> static inline
1263 Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a)
1265 return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1268 template<typename _Tp, int m, int n> static inline
1269 Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a)
1271 return Matx<_Tp, m, n>(a, -1, Matx_ScaleOp());
1274 template<typename _Tp, int m, int n, int l> static inline
1275 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b)
1277 return Matx<_Tp, m, n>(a, b, Matx_MatMulOp());
1280 template<typename _Tp, int m, int n> static inline
1281 Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b)
1283 Matx<_Tp, m, 1> c(a, b, Matx_MatMulOp());
1284 return (const Vec<_Tp, m>&)(c);
1287 template<typename _Tp, int m, int n> static inline
1288 bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1290 for( int i = 0; i < m*n; i++ )
1291 if( a.val[i] != b.val[i] ) return false;
1295 template<typename _Tp, int m, int n> static inline
1296 bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1303 ////////////////////////////// Vec out-of-class operators ////////////////////////////////
1305 //! @relates cv::Vec
1308 template<typename _Tp1, typename _Tp2, int cn> static inline
1309 Vec<_Tp1, cn>& operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
1311 for( int i = 0; i < cn; i++ )
1312 a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
1316 template<typename _Tp1, typename _Tp2, int cn> static inline
1317 Vec<_Tp1, cn>& operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
1319 for( int i = 0; i < cn; i++ )
1320 a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
1324 template<typename _Tp, int cn> static inline
1325 Vec<_Tp, cn> operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
1327 return Vec<_Tp, cn>(a, b, Matx_AddOp());
1330 template<typename _Tp, int cn> static inline
1331 Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
1333 return Vec<_Tp, cn>(a, b, Matx_SubOp());
1336 template<typename _Tp, int cn> static inline
1337 Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, int alpha)
1339 for( int i = 0; i < cn; i++ )
1340 a[i] = saturate_cast<_Tp>(a[i]*alpha);
1344 template<typename _Tp, int cn> static inline
1345 Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, float alpha)
1347 for( int i = 0; i < cn; i++ )
1348 a[i] = saturate_cast<_Tp>(a[i]*alpha);
1352 template<typename _Tp, int cn> static inline
1353 Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, double alpha)
1355 for( int i = 0; i < cn; i++ )
1356 a[i] = saturate_cast<_Tp>(a[i]*alpha);
1360 template<typename _Tp, int cn> static inline
1361 Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, int alpha)
1363 double ialpha = 1./alpha;
1364 for( int i = 0; i < cn; i++ )
1365 a[i] = saturate_cast<_Tp>(a[i]*ialpha);
1369 template<typename _Tp, int cn> static inline
1370 Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, float alpha)
1372 float ialpha = 1.f/alpha;
1373 for( int i = 0; i < cn; i++ )
1374 a[i] = saturate_cast<_Tp>(a[i]*ialpha);
1378 template<typename _Tp, int cn> static inline
1379 Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, double alpha)
1381 double ialpha = 1./alpha;
1382 for( int i = 0; i < cn; i++ )
1383 a[i] = saturate_cast<_Tp>(a[i]*ialpha);
1387 template<typename _Tp, int cn> static inline
1388 Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, int alpha)
1390 return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1393 template<typename _Tp, int cn> static inline
1394 Vec<_Tp, cn> operator * (int alpha, const Vec<_Tp, cn>& a)
1396 return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1399 template<typename _Tp, int cn> static inline
1400 Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, float alpha)
1402 return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1405 template<typename _Tp, int cn> static inline
1406 Vec<_Tp, cn> operator * (float alpha, const Vec<_Tp, cn>& a)
1408 return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1411 template<typename _Tp, int cn> static inline
1412 Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, double alpha)
1414 return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1417 template<typename _Tp, int cn> static inline
1418 Vec<_Tp, cn> operator * (double alpha, const Vec<_Tp, cn>& a)
1420 return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1423 template<typename _Tp, int cn> static inline
1424 Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, int alpha)
1426 return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
1429 template<typename _Tp, int cn> static inline
1430 Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, float alpha)
1432 return Vec<_Tp, cn>(a, 1.f/alpha, Matx_ScaleOp());
1435 template<typename _Tp, int cn> static inline
1436 Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, double alpha)
1438 return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
1441 template<typename _Tp, int cn> static inline
1442 Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a)
1445 for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
1449 template<typename _Tp> inline Vec<_Tp, 4> operator * (const Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
1451 return Vec<_Tp, 4>(saturate_cast<_Tp>(v1[0]*v2[0] - v1[1]*v2[1] - v1[2]*v2[2] - v1[3]*v2[3]),
1452 saturate_cast<_Tp>(v1[0]*v2[1] + v1[1]*v2[0] + v1[2]*v2[3] - v1[3]*v2[2]),
1453 saturate_cast<_Tp>(v1[0]*v2[2] - v1[1]*v2[3] + v1[2]*v2[0] + v1[3]*v2[1]),
1454 saturate_cast<_Tp>(v1[0]*v2[3] + v1[1]*v2[2] - v1[2]*v2[1] + v1[3]*v2[0]));
1457 template<typename _Tp> inline Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
1467 #endif // OPENCV_CORE_MATX_HPP