EstervQrCode 2.0.0
Library for qr code manipulation
Loading...
Searching...
No Matches
mat.inl.hpp
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// Copyright (C) 2015, Itseez Inc., all rights reserved.
17// Third party copyrights are property of their respective owners.
18//
19// Redistribution and use in source and binary forms, with or without modification,
20// are permitted provided that the following conditions are met:
21//
22// * Redistribution's of source code must retain the above copyright notice,
23// this list of conditions and the following disclaimer.
24//
25// * Redistribution's in binary form must reproduce the above copyright notice,
26// this list of conditions and the following disclaimer in the documentation
27// and/or other materials provided with the distribution.
28//
29// * The name of the copyright holders may not be used to endorse or promote products
30// derived from this software without specific prior written permission.
31//
32// This software is provided by the copyright holders and contributors "as is" and
33// any express or implied warranties, including, but not limited to, the implied
34// warranties of merchantability and fitness for a particular purpose are disclaimed.
35// In no event shall the Intel Corporation or contributors be liable for any direct,
36// indirect, incidental, special, exemplary, or consequential damages
37// (including, but not limited to, procurement of substitute goods or services;
38// loss of use, data, or profits; or business interruption) however caused
39// and on any theory of liability, whether in contract, strict liability,
40// or tort (including negligence or otherwise) arising in any way out of
41// the use of this software, even if advised of the possibility of such damage.
42//
43//M*/
44
45#ifndef OPENCV_CORE_MATRIX_OPERATIONS_HPP
46#define OPENCV_CORE_MATRIX_OPERATIONS_HPP
47
48#ifndef __cplusplus
49# error mat.inl.hpp header must be compiled as C++
50#endif
51
52#ifdef _MSC_VER
53#pragma warning( push )
54#pragma warning( disable: 4127 5054 )
55#endif
56
57#if defined(CV_SKIP_DISABLE_CLANG_ENUM_WARNINGS)
58 // nothing
59#elif defined(CV_FORCE_DISABLE_CLANG_ENUM_WARNINGS)
60 #define CV_DISABLE_CLANG_ENUM_WARNINGS
61#elif defined(__clang__) && defined(__has_warning)
62 #if __has_warning("-Wdeprecated-enum-enum-conversion") && __has_warning("-Wdeprecated-anon-enum-enum-conversion")
63 #define CV_DISABLE_CLANG_ENUM_WARNINGS
64 #endif
65#endif
66#ifdef CV_DISABLE_CLANG_ENUM_WARNINGS
67#pragma clang diagnostic push
68#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"
69#pragma clang diagnostic ignored "-Wdeprecated-anon-enum-enum-conversion"
70#endif
71
72namespace cv
73{
75
76
78
80
81template<typename _Tp> static inline
82int rawType()
83{
84 CV_StaticAssert(sizeof(_Tp) <= CV_CN_MAX, "sizeof(_Tp) is too large");
85 const int elemSize = sizeof(_Tp);
86 return (int)CV_MAKETYPE(CV_8U, elemSize);
87}
88
90
91inline void _InputArray::init(int _flags, const void* _obj)
92{ flags = _flags; obj = (void*)_obj; }
93
94inline void _InputArray::init(int _flags, const void* _obj, Size _sz)
95{ flags = _flags; obj = (void*)_obj; sz = _sz; }
96
97inline void* _InputArray::getObj() const { return obj; }
98inline int _InputArray::getFlags() const { return flags; }
99inline Size _InputArray::getSz() const { return sz; }
100
101inline _InputArray::_InputArray() { init(0 + NONE, 0); }
102inline _InputArray::_InputArray(int _flags, void* _obj) { init(_flags, _obj); }
103inline _InputArray::_InputArray(const Mat& m) { init(MAT+ACCESS_READ, &m); }
105inline _InputArray::_InputArray(const UMat& m) { init(UMAT+ACCESS_READ, &m); }
107
108template<typename _Tp> inline
111
112template<typename _Tp, std::size_t _Nm> inline
115
116template<std::size_t _Nm> inline
118{ init(STD_ARRAY_MAT + ACCESS_READ, arr.data(), Size(1, _Nm)); }
119
120inline
123
124template<typename _Tp> inline
127
128template<typename _Tp> inline
129_InputArray::_InputArray(const std::vector<Mat_<_Tp> >& vec)
131
132template<typename _Tp, int m, int n> inline
133_InputArray::_InputArray(const Matx<_Tp, m, n>& mtx)
135
136template<typename _Tp> inline
137_InputArray::_InputArray(const _Tp* vec, int n)
139
140template<typename _Tp> inline
141_InputArray::_InputArray(const Mat_<_Tp>& m)
143
144inline _InputArray::_InputArray(const double& val)
145{ init(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F + ACCESS_READ, &val, Size(1,1)); }
146
147inline _InputArray::_InputArray(const cuda::GpuMat& d_mat)
148{ init(CUDA_GPU_MAT + ACCESS_READ, &d_mat); }
149
152
153inline _InputArray::_InputArray(const ogl::Buffer& buf)
154{ init(OPENGL_BUFFER + ACCESS_READ, &buf); }
155
156inline _InputArray::_InputArray(const cuda::HostMem& cuda_mem)
157{ init(CUDA_HOST_MEM + ACCESS_READ, &cuda_mem); }
158
159template<typename _Tp> inline
160_InputArray _InputArray::rawIn(const std::vector<_Tp>& vec)
161{
162 _InputArray v;
164 v.obj = (void*)&vec;
165 return v;
166}
167
168template<typename _Tp, std::size_t _Nm> inline
170{
171 _InputArray v;
173 v.obj = (void*)arr.data();
174 v.sz = Size(1, _Nm);
175 return v;
176}
177
179
180inline Mat _InputArray::getMat(int i) const
181{
182 if( kind() == MAT && i < 0 )
183 return *(const Mat*)obj;
184 return getMat_(i);
185}
186
187inline bool _InputArray::isMat() const { return kind() == _InputArray::MAT; }
188inline bool _InputArray::isUMat() const { return kind() == _InputArray::UMAT; }
189inline bool _InputArray::isMatVector() const { return kind() == _InputArray::STD_VECTOR_MAT; }
190inline bool _InputArray::isUMatVector() const { return kind() == _InputArray::STD_VECTOR_UMAT; }
191inline bool _InputArray::isMatx() const { return kind() == _InputArray::MATX; }
192inline bool _InputArray::isVector() const { return kind() == _InputArray::STD_VECTOR ||
194 (kind() == _InputArray::MATX && (sz.width <= 1 || sz.height <= 1)); }
195inline bool _InputArray::isGpuMat() const { return kind() == _InputArray::CUDA_GPU_MAT; }
197
199
201inline _OutputArray::_OutputArray(int _flags, void* _obj) { init(_flags + ACCESS_WRITE, _obj); }
202inline _OutputArray::_OutputArray(Mat& m) { init(MAT+ACCESS_WRITE, &m); }
204inline _OutputArray::_OutputArray(UMat& m) { init(UMAT + ACCESS_WRITE, &m); }
206
207template<typename _Tp> inline
210
211template<typename _Tp, std::size_t _Nm> inline
214
215template<std::size_t _Nm> inline
217{ init(STD_ARRAY_MAT + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
218
219template<typename _Tp> inline
222
223template<typename _Tp> inline
226
227template<typename _Tp> inline
228_OutputArray::_OutputArray(Mat_<_Tp>& m)
230
231template<typename _Tp, int m, int n> inline
232_OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx)
234
235template<typename _Tp> inline
236_OutputArray::_OutputArray(_Tp* vec, int n)
238
239template<typename _Tp> inline
242
243template<typename _Tp, std::size_t _Nm> inline
246
247template<std::size_t _Nm> inline
249{ init(FIXED_SIZE + STD_ARRAY_MAT + ACCESS_WRITE, arr.data(), Size(1, _Nm)); }
250
251template<typename _Tp> inline
254
255template<typename _Tp> inline
256_OutputArray::_OutputArray(const std::vector<Mat_<_Tp> >& vec)
258
259template<typename _Tp> inline
260_OutputArray::_OutputArray(const Mat_<_Tp>& m)
262
263template<typename _Tp, int m, int n> inline
264_OutputArray::_OutputArray(const Matx<_Tp, m, n>& mtx)
266
267template<typename _Tp> inline
268_OutputArray::_OutputArray(const _Tp* vec, int n)
270
271inline _OutputArray::_OutputArray(cuda::GpuMat& d_mat)
272{ init(CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
273
276
277inline _OutputArray::_OutputArray(ogl::Buffer& buf)
278{ init(OPENGL_BUFFER + ACCESS_WRITE, &buf); }
279
280inline _OutputArray::_OutputArray(cuda::HostMem& cuda_mem)
281{ init(CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
282
283inline _OutputArray::_OutputArray(const Mat& m)
285
288
289inline _OutputArray::_OutputArray(const UMat& m)
291
294
295inline _OutputArray::_OutputArray(const cuda::GpuMat& d_mat)
297
298
299inline _OutputArray::_OutputArray(const ogl::Buffer& buf)
301
302inline _OutputArray::_OutputArray(const cuda::HostMem& cuda_mem)
304
305template<typename _Tp> inline
307{
308 _OutputArray v;
310 v.obj = (void*)&vec;
311 return v;
312}
313
314template<typename _Tp, std::size_t _Nm> inline
316{
317 _OutputArray v;
319 v.obj = (void*)arr.data();
320 v.sz = Size(1, _Nm);
321 return v;
322}
323
325
327inline _InputOutputArray::_InputOutputArray(int _flags, void* _obj) { init(_flags+ACCESS_RW, _obj); }
332
333template<typename _Tp> inline
336
337template<typename _Tp, std::size_t _Nm> inline
340
341template<std::size_t _Nm> inline
343{ init(STD_ARRAY_MAT + ACCESS_RW, arr.data(), Size(1, _Nm)); }
344
345template<typename _Tp> inline
348
349template<typename _Tp> inline
352
353template<typename _Tp> inline
356
357template<typename _Tp, int m, int n> inline
358_InputOutputArray::_InputOutputArray(Matx<_Tp, m, n>& mtx)
360
361template<typename _Tp> inline
364
365template<typename _Tp> inline
368
369template<typename _Tp, std::size_t _Nm> inline
372
373template<std::size_t _Nm> inline
375{ init(FIXED_SIZE + STD_ARRAY_MAT + ACCESS_RW, arr.data(), Size(1, _Nm)); }
376
377template<typename _Tp> inline
380
381template<typename _Tp> inline
384
385template<typename _Tp> inline
386_InputOutputArray::_InputOutputArray(const Mat_<_Tp>& m)
388
389template<typename _Tp, int m, int n> inline
390_InputOutputArray::_InputOutputArray(const Matx<_Tp, m, n>& mtx)
392
393template<typename _Tp> inline
394_InputOutputArray::_InputOutputArray(const _Tp* vec, int n)
396
397inline _InputOutputArray::_InputOutputArray(cuda::GpuMat& d_mat)
398{ init(CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
399
400inline _InputOutputArray::_InputOutputArray(ogl::Buffer& buf)
401{ init(OPENGL_BUFFER + ACCESS_RW, &buf); }
402
403inline _InputOutputArray::_InputOutputArray(cuda::HostMem& cuda_mem)
404{ init(CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
405
406inline _InputOutputArray::_InputOutputArray(const Mat& m)
407{ init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_RW, &m); }
408
411
412inline _InputOutputArray::_InputOutputArray(const UMat& m)
414
417
418inline _InputOutputArray::_InputOutputArray(const cuda::GpuMat& d_mat)
420
423
426
427inline _InputOutputArray::_InputOutputArray(const ogl::Buffer& buf)
429
430inline _InputOutputArray::_InputOutputArray(const cuda::HostMem& cuda_mem)
431{ init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
432
433template<typename _Tp> inline
434_InputOutputArray _InputOutputArray::rawInOut(std::vector<_Tp>& vec)
435{
438 v.obj = (void*)&vec;
439 return v;
440}
441
442template<typename _Tp, std::size_t _Nm> inline
444{
447 v.obj = (void*)arr.data();
448 v.sz = Size(1, _Nm);
449 return v;
450}
451
452
453template<typename _Tp> static inline _InputArray rawIn(_Tp& v) { return _InputArray::rawIn(v); }
454template<typename _Tp> static inline _OutputArray rawOut(_Tp& v) { return _OutputArray::rawOut(v); }
455template<typename _Tp> static inline _InputOutputArray rawInOut(_Tp& v) { return _InputOutputArray::rawInOut(v); }
456
458
460
461template<typename _Tp> inline
462Mat::Mat(const std::vector<_Tp>& vec, bool copyData)
463 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
464 cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
465{
466 if(vec.empty())
467 return;
468 if( !copyData )
469 {
470 step[0] = step[1] = sizeof(_Tp);
471 datastart = data = (uchar*)&vec[0];
472 datalimit = dataend = datastart + rows * step[0];
473 }
474 else
475 Mat((int)vec.size(), 1, traits::Type<_Tp>::value, (uchar*)&vec[0]).copyTo(*this);
476}
477
478template<typename _Tp, typename> inline
479Mat::Mat(const std::initializer_list<_Tp> list)
480 : Mat()
481{
482 CV_Assert(list.size() != 0);
483 Mat((int)list.size(), 1, traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
484}
485
486template<typename _Tp> inline
488 : Mat()
489{
490 size_t size_total = 1;
491 for(auto s : sizes)
492 size_total *= s;
493 CV_Assert(list.size() != 0);
494 CV_Assert(size_total == list.size());
495 Mat((int)sizes.size(), (int*)sizes.begin(), traits::Type<_Tp>::value, (uchar*)list.begin()).copyTo(*this);
496}
497
498template<typename _Tp, std::size_t _Nm> inline
499Mat::Mat(const std::array<_Tp, _Nm>& arr, bool copyData)
500 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows((int)arr.size()),
501 cols(1), data(0), datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
502{
503 if(arr.empty())
504 return;
505 if( !copyData )
506 {
507 step[0] = step[1] = sizeof(_Tp);
508 datastart = data = (uchar*)arr.data();
509 datalimit = dataend = datastart + rows * step[0];
510 }
511 else
512 Mat((int)arr.size(), 1, traits::Type<_Tp>::value, (uchar*)arr.data()).copyTo(*this);
513}
514
515template<typename _Tp, int n> inline
516Mat::Mat(const Vec<_Tp, n>& vec, bool copyData)
517 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(n), cols(1), data(0),
518 datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
519{
520 if( !copyData )
521 {
522 step[0] = step[1] = sizeof(_Tp);
523 datastart = data = (uchar*)vec.val;
524 datalimit = dataend = datastart + rows * step[0];
525 }
526 else
527 Mat(n, 1, traits::Type<_Tp>::value, (void*)vec.val).copyTo(*this);
528}
529
530
531template<typename _Tp, int m, int n> inline
532Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData)
533 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(m), cols(n), data(0),
534 datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
535{
536 if( !copyData )
537 {
538 step[0] = cols * sizeof(_Tp);
539 step[1] = sizeof(_Tp);
540 datastart = data = (uchar*)M.val;
541 datalimit = dataend = datastart + rows * step[0];
542 }
543 else
544 Mat(m, n, traits::Type<_Tp>::value, (uchar*)M.val).copyTo(*this);
545}
546
547template<typename _Tp> inline
548Mat::Mat(const Point_<_Tp>& pt, bool copyData)
549 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(2), cols(1), data(0),
550 datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
551{
552 if( !copyData )
553 {
554 step[0] = step[1] = sizeof(_Tp);
555 datastart = data = (uchar*)&pt.x;
556 datalimit = dataend = datastart + rows * step[0];
557 }
558 else
559 {
560 create(2, 1, traits::Type<_Tp>::value);
561 ((_Tp*)data)[0] = pt.x;
562 ((_Tp*)data)[1] = pt.y;
563 }
564}
565
566template<typename _Tp> inline
567Mat::Mat(const Point3_<_Tp>& pt, bool copyData)
568 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows(3), cols(1), data(0),
569 datastart(0), dataend(0), datalimit(0), allocator(0), u(0), size(&rows), step(0)
570{
571 if( !copyData )
572 {
573 step[0] = step[1] = sizeof(_Tp);
574 datastart = data = (uchar*)&pt.x;
575 datalimit = dataend = datastart + rows * step[0];
576 }
577 else
578 {
579 create(3, 1, traits::Type<_Tp>::value);
580 ((_Tp*)data)[0] = pt.x;
581 ((_Tp*)data)[1] = pt.y;
582 ((_Tp*)data)[2] = pt.z;
583 }
584}
585
586template<typename _Tp> inline
587Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer)
588 : flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(0), rows(0), cols(0), data(0),
589 datastart(0), dataend(0), allocator(0), u(0), size(&rows)
590{
591 *this = commaInitializer.operator Mat_<_Tp>();
592}
593
594inline
595Mat Mat::row(int y) const
596{
597 return Mat(*this, Range(y, y + 1), Range::all());
598}
599
600inline
601Mat Mat::col(int x) const
602{
603 return Mat(*this, Range::all(), Range(x, x + 1));
604}
605
606inline
607Mat Mat::rowRange(int startrow, int endrow) const
608{
609 return Mat(*this, Range(startrow, endrow), Range::all());
610}
611
612inline
613Mat Mat::rowRange(const Range& r) const
614{
615 return Mat(*this, r, Range::all());
616}
617
618inline
619Mat Mat::colRange(int startcol, int endcol) const
620{
621 return Mat(*this, Range::all(), Range(startcol, endcol));
622}
623
624inline
625Mat Mat::colRange(const Range& r) const
626{
627 return Mat(*this, Range::all(), r);
628}
629
630inline
631Mat Mat::operator()( Range _rowRange, Range _colRange ) const
632{
633 return Mat(*this, _rowRange, _colRange);
634}
635
636inline
637Mat Mat::operator()( const Rect& roi ) const
638{
639 return Mat(*this, roi);
640}
641
642inline
643Mat Mat::operator()(const Range* ranges) const
644{
645 return Mat(*this, ranges);
646}
647
648inline
649Mat Mat::operator()(const std::vector<Range>& ranges) const
650{
651 return Mat(*this, ranges);
652}
653
654inline
655bool Mat::isContinuous() const
656{
657 return (flags & CONTINUOUS_FLAG) != 0;
658}
659
660inline
661bool Mat::isSubmatrix() const
662{
663 return (flags & SUBMATRIX_FLAG) != 0;
664}
665
666inline
667size_t Mat::elemSize() const
668{
669 size_t res = dims > 0 ? step.p[dims - 1] : 0;
670 CV_DbgAssert(res != 0);
671 return res;
672}
673
674inline
675size_t Mat::elemSize1() const
676{
677 return CV_ELEM_SIZE1(flags);
678}
679
680inline
681int Mat::type() const
682{
683 return CV_MAT_TYPE(flags);
684}
685
686inline
687int Mat::depth() const
688{
689 return CV_MAT_DEPTH(flags);
690}
691
692inline
693int Mat::channels() const
694{
695 return CV_MAT_CN(flags);
696}
697
698inline
699uchar* Mat::ptr(int y)
700{
701 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
702 return data + step.p[0] * y;
703}
704
705inline
706const uchar* Mat::ptr(int y) const
707{
708 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
709 return data + step.p[0] * y;
710}
711
712template<typename _Tp> inline
713_Tp* Mat::ptr(int y)
714{
715 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
716 return (_Tp*)(data + step.p[0] * y);
717}
718
719template<typename _Tp> inline
720const _Tp* Mat::ptr(int y) const
721{
722 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
723 return (const _Tp*)(data + step.p[0] * y);
724}
725
726inline
727uchar* Mat::ptr(int i0, int i1)
728{
729 CV_DbgAssert(dims >= 2);
731 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
732 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
733 return data + i0 * step.p[0] + i1 * step.p[1];
734}
735
736inline
737const uchar* Mat::ptr(int i0, int i1) const
738{
739 CV_DbgAssert(dims >= 2);
741 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
742 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
743 return data + i0 * step.p[0] + i1 * step.p[1];
744}
745
746template<typename _Tp> inline
747_Tp* Mat::ptr(int i0, int i1)
748{
749 CV_DbgAssert(dims >= 2);
751 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
752 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
753 return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
754}
755
756template<typename _Tp> inline
757const _Tp* Mat::ptr(int i0, int i1) const
758{
759 CV_DbgAssert(dims >= 2);
761 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
762 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
763 return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
764}
765
766inline
767uchar* Mat::ptr(int i0, int i1, int i2)
768{
769 CV_DbgAssert(dims >= 3);
771 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
772 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
773 CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
774 return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
775}
776
777inline
778const uchar* Mat::ptr(int i0, int i1, int i2) const
779{
780 CV_DbgAssert(dims >= 3);
782 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
783 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
784 CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
785 return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
786}
787
788template<typename _Tp> inline
789_Tp* Mat::ptr(int i0, int i1, int i2)
790{
791 CV_DbgAssert(dims >= 3);
793 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
794 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
795 CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
796 return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
797}
798
799template<typename _Tp> inline
800const _Tp* Mat::ptr(int i0, int i1, int i2) const
801{
802 CV_DbgAssert(dims >= 3);
804 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
805 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
806 CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
807 return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
808}
809
810inline
811uchar* Mat::ptr(const int* idx)
812{
813 int i, d = dims;
814 uchar* p = data;
815 CV_DbgAssert( d >= 1 && p );
816 for( i = 0; i < d; i++ )
817 {
818 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
819 p += idx[i] * step.p[i];
820 }
821 return p;
822}
823
824inline
825const uchar* Mat::ptr(const int* idx) const
826{
827 int i, d = dims;
828 uchar* p = data;
829 CV_DbgAssert( d >= 1 && p );
830 for( i = 0; i < d; i++ )
831 {
832 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
833 p += idx[i] * step.p[i];
834 }
835 return p;
836}
837
838template<typename _Tp> inline
839_Tp* Mat::ptr(const int* idx)
840{
841 int i, d = dims;
842 uchar* p = data;
843 CV_DbgAssert( d >= 1 && p );
844 for( i = 0; i < d; i++ )
845 {
846 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
847 p += idx[i] * step.p[i];
848 }
849 return (_Tp*)p;
850}
851
852template<typename _Tp> inline
853const _Tp* Mat::ptr(const int* idx) const
854{
855 int i, d = dims;
856 uchar* p = data;
857 CV_DbgAssert( d >= 1 && p );
858 for( i = 0; i < d; i++ )
859 {
860 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
861 p += idx[i] * step.p[i];
862 }
863 return (const _Tp*)p;
864}
865
866template<int n> inline
867uchar* Mat::ptr(const Vec<int, n>& idx)
868{
869 return Mat::ptr(idx.val);
870}
871
872template<int n> inline
873const uchar* Mat::ptr(const Vec<int, n>& idx) const
874{
875 return Mat::ptr(idx.val);
876}
877
878template<typename _Tp, int n> inline
879_Tp* Mat::ptr(const Vec<int, n>& idx)
880{
881 CV_DbgAssert( elemSize() == sizeof(_Tp) );
882 return Mat::ptr<_Tp>(idx.val);
883}
884
885template<typename _Tp, int n> inline
886const _Tp* Mat::ptr(const Vec<int, n>& idx) const
887{
888 CV_DbgAssert( elemSize() == sizeof(_Tp) );
889 return Mat::ptr<_Tp>(idx.val);
890}
891
892
893template<typename _Tp> inline
894_Tp& Mat::at(int i0, int i1)
895{
896 CV_DbgAssert(dims <= 2);
898 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
899 CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
900 CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
901 return ((_Tp*)(data + step.p[0] * i0))[i1];
902}
903
904template<typename _Tp> inline
905const _Tp& Mat::at(int i0, int i1) const
906{
907 CV_DbgAssert(dims <= 2);
909 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
910 CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
911 CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
912 return ((const _Tp*)(data + step.p[0] * i0))[i1];
913}
914
915template<typename _Tp> inline
916_Tp& Mat::at(Point pt)
917{
918 CV_DbgAssert(dims <= 2);
920 CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
921 CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
922 CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
923 return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
924}
925
926template<typename _Tp> inline
927const _Tp& Mat::at(Point pt) const
928{
929 CV_DbgAssert(dims <= 2);
931 CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
932 CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
933 CV_DbgAssert(CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1());
934 return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
935}
936
937template<typename _Tp> inline
938_Tp& Mat::at(int i0)
939{
940 CV_DbgAssert(dims <= 2);
942 CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
943 CV_DbgAssert(elemSize() == sizeof(_Tp));
944 if( isContinuous() || size.p[0] == 1 )
945 return ((_Tp*)data)[i0];
946 if( size.p[1] == 1 )
947 return *(_Tp*)(data + step.p[0] * i0);
948 int i = i0 / cols, j = i0 - i * cols;
949 return ((_Tp*)(data + step.p[0] * i))[j];
950}
951
952template<typename _Tp> inline
953const _Tp& Mat::at(int i0) const
954{
955 CV_DbgAssert(dims <= 2);
957 CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
958 CV_DbgAssert(elemSize() == sizeof(_Tp));
959 if( isContinuous() || size.p[0] == 1 )
960 return ((const _Tp*)data)[i0];
961 if( size.p[1] == 1 )
962 return *(const _Tp*)(data + step.p[0] * i0);
963 int i = i0 / cols, j = i0 - i * cols;
964 return ((const _Tp*)(data + step.p[0] * i))[j];
965}
966
967template<typename _Tp> inline
968_Tp& Mat::at(int i0, int i1, int i2)
969{
970 CV_DbgAssert( elemSize() == sizeof(_Tp) );
971 return *(_Tp*)ptr(i0, i1, i2);
972}
973
974template<typename _Tp> inline
975const _Tp& Mat::at(int i0, int i1, int i2) const
976{
977 CV_DbgAssert( elemSize() == sizeof(_Tp) );
978 return *(const _Tp*)ptr(i0, i1, i2);
979}
980
981template<typename _Tp> inline
982_Tp& Mat::at(const int* idx)
983{
984 CV_DbgAssert( elemSize() == sizeof(_Tp) );
985 return *(_Tp*)ptr(idx);
986}
987
988template<typename _Tp> inline
989const _Tp& Mat::at(const int* idx) const
990{
991 CV_DbgAssert( elemSize() == sizeof(_Tp) );
992 return *(const _Tp*)ptr(idx);
993}
994
995template<typename _Tp, int n> inline
996_Tp& Mat::at(const Vec<int, n>& idx)
997{
998 CV_DbgAssert( elemSize() == sizeof(_Tp) );
999 return *(_Tp*)ptr(idx.val);
1000}
1001
1002template<typename _Tp, int n> inline
1003const _Tp& Mat::at(const Vec<int, n>& idx) const
1004{
1005 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1006 return *(const _Tp*)ptr(idx.val);
1007}
1008
1009template<typename _Tp> inline
1010MatConstIterator_<_Tp> Mat::begin() const
1011{
1012 if (empty())
1013 return MatConstIterator_<_Tp>();
1014 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1015 return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
1016}
1017
1018template<typename _Tp> inline
1020{
1021 if (empty())
1023 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1024 MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
1025 it += total();
1027}
1028
1029template<typename _Tp> inline
1030MatConstIterator_<_Tp> Mat::end() const
1031{
1032 if (empty())
1033 return MatConstIterator_<_Tp>();
1034 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1035 MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
1036 it += total();
1037 return it;
1038}
1039
1040template<typename _Tp> inline
1042{
1043 if (empty())
1045 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1046 return std::reverse_iterator<MatConstIterator_<_Tp>>((const Mat_<_Tp>*)this);
1047}
1048
1049template<typename _Tp> inline
1050MatIterator_<_Tp> Mat::begin()
1051{
1052 if (empty())
1053 return MatIterator_<_Tp>();
1054 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1055 return MatIterator_<_Tp>((Mat_<_Tp>*)this);
1056}
1057
1058template<typename _Tp> inline
1060{
1061 if (empty())
1063 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1064 MatIterator_<_Tp> it((Mat_<_Tp>*)this);
1065 it += total();
1067}
1068
1069template<typename _Tp> inline
1070MatIterator_<_Tp> Mat::end()
1071{
1072 if (empty())
1073 return MatIterator_<_Tp>();
1074 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1075 MatIterator_<_Tp> it((Mat_<_Tp>*)this);
1076 it += total();
1077 return it;
1078}
1079
1080template<typename _Tp> inline
1082{
1083 if (empty())
1085 CV_DbgAssert( elemSize() == sizeof(_Tp) );
1086 return std::reverse_iterator<MatIterator_<_Tp>>(MatIterator_<_Tp>((Mat_<_Tp>*)this));
1087}
1088
1089template<typename _Tp, typename Functor> inline
1090void Mat::forEach(const Functor& operation) {
1091 this->forEach_impl<_Tp>(operation);
1092}
1093
1094template<typename _Tp, typename Functor> inline
1095void Mat::forEach(const Functor& operation) const {
1096 // call as not const
1097 (const_cast<Mat*>(this))->forEach<_Tp>(operation);
1098}
1099
1100template<typename _Tp> inline
1101Mat::operator std::vector<_Tp>() const
1102{
1104 copyTo(v);
1105 return v;
1106}
1107
1108template<typename _Tp, std::size_t _Nm> inline
1109Mat::operator std::array<_Tp, _Nm>() const
1110{
1112 copyTo(v);
1113 return v;
1114}
1115
1116template<typename _Tp, int n> inline
1117Mat::operator Vec<_Tp, n>() const
1118{
1119 CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) &&
1120 rows + cols - 1 == n && channels() == 1 );
1121
1122 if( isContinuous() && type() == traits::Type<_Tp>::value )
1123 return Vec<_Tp, n>((_Tp*)data);
1124 Vec<_Tp, n> v;
1125 Mat tmp(rows, cols, traits::Type<_Tp>::value, v.val);
1126 convertTo(tmp, tmp.type());
1127 return v;
1128}
1129
1130template<typename _Tp, int m, int n> inline
1131Mat::operator Matx<_Tp, m, n>() const
1132{
1133 CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 );
1134
1135 if( isContinuous() && type() == traits::Type<_Tp>::value )
1136 return Matx<_Tp, m, n>((_Tp*)data);
1137 Matx<_Tp, m, n> mtx;
1138 Mat tmp(rows, cols, traits::Type<_Tp>::value, mtx.val);
1139 convertTo(tmp, tmp.type());
1140 return mtx;
1141}
1142
1143template<typename _Tp> inline
1144void Mat::push_back(const _Tp& elem)
1145{
1146 if( !data )
1147 {
1148 *this = Mat(1, 1, traits::Type<_Tp>::value, (void*)&elem).clone();
1149 return;
1150 }
1151 CV_Assert(traits::Type<_Tp>::value == type() && cols == 1
1152 /* && dims == 2 (cols == 1 implies dims == 2) */);
1153 const uchar* tmp = dataend + step[0];
1154 if( !isSubmatrix() && isContinuous() && tmp <= datalimit )
1155 {
1156 *(_Tp*)(data + (size.p[0]++) * step.p[0]) = elem;
1157 dataend = tmp;
1158 }
1159 else
1160 push_back_(&elem);
1161}
1162
1163template<typename _Tp> inline
1164void Mat::push_back(const Mat_<_Tp>& m)
1165{
1166 push_back((const Mat&)m);
1167}
1168
1169template<> inline
1170void Mat::push_back(const MatExpr& expr)
1171{
1172 push_back(static_cast<Mat>(expr));
1173}
1174
1175
1176template<typename _Tp> inline
1177void Mat::push_back(const std::vector<_Tp>& v)
1178{
1179 push_back(Mat(v));
1180}
1181
1182
1184
1185inline
1186MatSize::MatSize(int* _p) CV_NOEXCEPT
1187 : p(_p) {}
1188
1189inline
1190int MatSize::dims() const CV_NOEXCEPT
1191{
1192 return (p - 1)[0];
1193}
1194
1195inline
1196Size MatSize::operator()() const
1197{
1198 CV_DbgAssert(dims() <= 2);
1199 return Size(p[1], p[0]);
1200}
1201
1202inline
1203const int& MatSize::operator[](int i) const
1204{
1205 CV_DbgAssert(i < dims());
1206#ifdef __OPENCV_BUILD
1207 CV_DbgAssert(i >= 0);
1208#endif
1209 return p[i];
1210}
1211
1212inline
1213int& MatSize::operator[](int i)
1214{
1215 CV_DbgAssert(i < dims());
1216#ifdef __OPENCV_BUILD
1217 CV_DbgAssert(i >= 0);
1218#endif
1219 return p[i];
1220}
1221
1222inline
1223MatSize::operator const int*() const CV_NOEXCEPT
1224{
1225 return p;
1226}
1227
1228inline
1229bool MatSize::operator != (const MatSize& sz) const CV_NOEXCEPT
1230{
1231 return !(*this == sz);
1232}
1233
1234
1235
1237
1238inline
1239MatStep::MatStep() CV_NOEXCEPT
1240{
1241 p = buf; p[0] = p[1] = 0;
1242}
1243
1244inline
1245MatStep::MatStep(size_t s) CV_NOEXCEPT
1246{
1247 p = buf; p[0] = s; p[1] = 0;
1248}
1249
1250inline
1251const size_t& MatStep::operator[](int i) const CV_NOEXCEPT
1252{
1253 return p[i];
1254}
1255
1256inline
1257size_t& MatStep::operator[](int i) CV_NOEXCEPT
1258{
1259 return p[i];
1260}
1261
1262inline MatStep::operator size_t() const
1263{
1264 CV_DbgAssert( p == buf );
1265 return buf[0];
1266}
1267
1268inline MatStep& MatStep::operator = (size_t s)
1269{
1270 CV_DbgAssert( p == buf );
1271 buf[0] = s;
1272 return *this;
1273}
1274
1275
1276
1278
1279template<typename _Tp> inline
1280Mat_<_Tp>::Mat_() CV_NOEXCEPT
1281 : Mat()
1282{
1283 flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
1284}
1285
1286template<typename _Tp> inline
1287Mat_<_Tp>::Mat_(int _rows, int _cols)
1288 : Mat(_rows, _cols, traits::Type<_Tp>::value)
1289{
1290}
1291
1292template<typename _Tp> inline
1293Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
1294 : Mat(_rows, _cols, traits::Type<_Tp>::value)
1295{
1296 *this = value;
1297}
1298
1299template<typename _Tp> inline
1300Mat_<_Tp>::Mat_(Size _sz)
1301 : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
1302{}
1303
1304template<typename _Tp> inline
1305Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
1306 : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
1307{
1308 *this = value;
1309}
1310
1311template<typename _Tp> inline
1312Mat_<_Tp>::Mat_(int _dims, const int* _sz)
1313 : Mat(_dims, _sz, traits::Type<_Tp>::value)
1314{}
1315
1316template<typename _Tp> inline
1317Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s)
1318 : Mat(_dims, _sz, traits::Type<_Tp>::value, Scalar(_s))
1319{}
1320
1321template<typename _Tp> inline
1322Mat_<_Tp>::Mat_(int _dims, const int* _sz, _Tp* _data, const size_t* _steps)
1323 : Mat(_dims, _sz, traits::Type<_Tp>::value, _data, _steps)
1324{}
1325
1326template<typename _Tp> inline
1327Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges)
1328 : Mat(m, ranges)
1329{}
1330
1331template<typename _Tp> inline
1332Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const std::vector<Range>& ranges)
1333 : Mat(m, ranges)
1334{}
1335
1336template<typename _Tp> inline
1337Mat_<_Tp>::Mat_(const Mat& m)
1338 : Mat()
1339{
1340 flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
1341 *this = m;
1342}
1343
1344template<typename _Tp> inline
1345Mat_<_Tp>::Mat_(const Mat_& m)
1346 : Mat(m)
1347{}
1348
1349template<typename _Tp> inline
1350Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps)
1351 : Mat(_rows, _cols, traits::Type<_Tp>::value, _data, steps)
1352{}
1353
1354template<typename _Tp> inline
1355Mat_<_Tp>::Mat_(const Mat_& m, const Range& _rowRange, const Range& _colRange)
1356 : Mat(m, _rowRange, _colRange)
1357{}
1358
1359template<typename _Tp> inline
1360Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
1361 : Mat(m, roi)
1362{}
1363
1364template<typename _Tp> template<int n> inline
1365Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData)
1366 : Mat(n / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&vec)
1367{
1368 CV_Assert(n%DataType<_Tp>::channels == 0);
1369 if( copyData )
1370 *this = clone();
1371}
1372
1373template<typename _Tp> template<int m, int n> inline
1374Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& M, bool copyData)
1375 : Mat(m, n / DataType<_Tp>::channels, traits::Type<_Tp>::value, (void*)&M)
1376{
1377 CV_Assert(n % DataType<_Tp>::channels == 0);
1378 if( copyData )
1379 *this = clone();
1380}
1381
1382template<typename _Tp> inline
1383Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
1384 : Mat(2 / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&pt)
1385{
1386 CV_Assert(2 % DataType<_Tp>::channels == 0);
1387 if( copyData )
1388 *this = clone();
1389}
1390
1391template<typename _Tp> inline
1392Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
1393 : Mat(3 / DataType<_Tp>::channels, 1, traits::Type<_Tp>::value, (void*)&pt)
1394{
1395 CV_Assert(3 % DataType<_Tp>::channels == 0);
1396 if( copyData )
1397 *this = clone();
1398}
1399
1400template<typename _Tp> inline
1401Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer)
1402 : Mat(commaInitializer)
1403{}
1404
1405template<typename _Tp> inline
1406Mat_<_Tp>::Mat_(const std::vector<_Tp>& vec, bool copyData)
1407 : Mat(vec, copyData)
1408{}
1409
1410template<typename _Tp> inline
1411Mat_<_Tp>::Mat_(std::initializer_list<_Tp> list)
1412 : Mat(list)
1413{}
1414
1415template<typename _Tp> inline
1417 : Mat(sizes, list)
1418{}
1419
1420template<typename _Tp> template<std::size_t _Nm> inline
1421Mat_<_Tp>::Mat_(const std::array<_Tp, _Nm>& arr, bool copyData)
1422 : Mat(arr, copyData)
1423{}
1424
1425template<typename _Tp> inline
1426Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
1427{
1428 if (m.empty())
1429 {
1430 release();
1431 return *this;
1432 }
1433 if( traits::Type<_Tp>::value == m.type() )
1434 {
1435 Mat::operator = (m);
1436 return *this;
1437 }
1438 if( traits::Depth<_Tp>::value == m.depth() )
1439 {
1440 return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0));
1441 }
1442 CV_Assert(DataType<_Tp>::channels == m.channels() || m.empty());
1443 m.convertTo(*this, type());
1444 return *this;
1445}
1446
1447template<typename _Tp> inline
1448Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
1449{
1450 Mat::operator=(m);
1451 return *this;
1452}
1453
1454template<typename _Tp> inline
1455Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
1456{
1457 typedef typename DataType<_Tp>::vec_type VT;
1458 Mat::operator=(Scalar((const VT&)s));
1459 return *this;
1460}
1461
1462template<typename _Tp> inline
1463void Mat_<_Tp>::create(int _rows, int _cols)
1464{
1465 Mat::create(_rows, _cols, traits::Type<_Tp>::value);
1466}
1467
1468template<typename _Tp> inline
1469void Mat_<_Tp>::create(Size _sz)
1470{
1471 Mat::create(_sz, traits::Type<_Tp>::value);
1472}
1473
1474template<typename _Tp> inline
1475void Mat_<_Tp>::create(int _dims, const int* _sz)
1476{
1477 Mat::create(_dims, _sz, traits::Type<_Tp>::value);
1478}
1479
1480template<typename _Tp> inline
1481void Mat_<_Tp>::release()
1482{
1483 Mat::release();
1484 flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
1485}
1486
1487template<typename _Tp> inline
1488Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
1489{
1490 return Mat_<_Tp>(Mat::cross(m));
1491}
1492
1493template<typename _Tp> template<typename T2> inline
1494Mat_<_Tp>::operator Mat_<T2>() const
1495{
1496 return Mat_<T2>(static_cast<const Mat&>(*this));
1497}
1498
1499template<typename _Tp> inline
1500Mat_<_Tp> Mat_<_Tp>::row(int y) const
1501{
1502 return Mat_(*this, Range(y, y+1), Range::all());
1503}
1504
1505template<typename _Tp> inline
1506Mat_<_Tp> Mat_<_Tp>::col(int x) const
1507{
1508 return Mat_(*this, Range::all(), Range(x, x+1));
1509}
1510
1511template<typename _Tp> inline
1512Mat_<_Tp> Mat_<_Tp>::diag(int d) const
1513{
1514 return Mat_(Mat::diag(d));
1515}
1516
1517template<typename _Tp> inline
1518Mat_<_Tp> Mat_<_Tp>::clone() const
1519{
1520 return Mat_(Mat::clone());
1521}
1522
1523template<typename _Tp> inline
1524size_t Mat_<_Tp>::elemSize() const
1525{
1526 CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
1527 return sizeof(_Tp);
1528}
1529
1530template<typename _Tp> inline
1531size_t Mat_<_Tp>::elemSize1() const
1532{
1533 CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp) / DataType<_Tp>::channels );
1534 return sizeof(_Tp) / DataType<_Tp>::channels;
1535}
1536
1537template<typename _Tp> inline
1538int Mat_<_Tp>::type() const
1539{
1540 CV_DbgAssert( Mat::type() == traits::Type<_Tp>::value );
1541 return traits::Type<_Tp>::value;
1542}
1543
1544template<typename _Tp> inline
1545int Mat_<_Tp>::depth() const
1546{
1547 CV_DbgAssert( Mat::depth() == traits::Depth<_Tp>::value );
1548 return traits::Depth<_Tp>::value;
1549}
1550
1551template<typename _Tp> inline
1552int Mat_<_Tp>::channels() const
1553{
1554 CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
1555 return DataType<_Tp>::channels;
1556}
1557
1558template<typename _Tp> inline
1559size_t Mat_<_Tp>::stepT(int i) const
1560{
1561 return step.p[i] / elemSize();
1562}
1563
1564template<typename _Tp> inline
1565size_t Mat_<_Tp>::step1(int i) const
1566{
1567 return step.p[i] / elemSize1();
1568}
1569
1570template<typename _Tp> inline
1571Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
1572{
1573 return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright));
1574}
1575
1576template<typename _Tp> inline
1577Mat_<_Tp> Mat_<_Tp>::operator()( const Range& _rowRange, const Range& _colRange ) const
1578{
1579 return Mat_<_Tp>(*this, _rowRange, _colRange);
1580}
1581
1582template<typename _Tp> inline
1583Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
1584{
1585 return Mat_<_Tp>(*this, roi);
1586}
1587
1588template<typename _Tp> inline
1589Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const
1590{
1591 return Mat_<_Tp>(*this, ranges);
1592}
1593
1594template<typename _Tp> inline
1595Mat_<_Tp> Mat_<_Tp>::operator()(const std::vector<Range>& ranges) const
1596{
1597 return Mat_<_Tp>(*this, ranges);
1598}
1599
1600template<typename _Tp> inline
1601_Tp* Mat_<_Tp>::operator [](int y)
1602{
1603 CV_DbgAssert( 0 <= y && y < size.p[0] );
1604 return (_Tp*)(data + y*step.p[0]);
1605}
1606
1607template<typename _Tp> inline
1608const _Tp* Mat_<_Tp>::operator [](int y) const
1609{
1610 CV_DbgAssert( 0 <= y && y < size.p[0] );
1611 return (const _Tp*)(data + y*step.p[0]);
1612}
1613
1614template<typename _Tp> inline
1615_Tp& Mat_<_Tp>::operator ()(int i0, int i1)
1616{
1617 CV_DbgAssert(dims <= 2);
1619 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
1620 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
1621 CV_DbgAssert(type() == traits::Type<_Tp>::value);
1622 return ((_Tp*)(data + step.p[0] * i0))[i1];
1623}
1624
1625template<typename _Tp> inline
1626const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const
1627{
1628 CV_DbgAssert(dims <= 2);
1630 CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
1631 CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
1632 CV_DbgAssert(type() == traits::Type<_Tp>::value);
1633 return ((const _Tp*)(data + step.p[0] * i0))[i1];
1634}
1635
1636template<typename _Tp> inline
1637_Tp& Mat_<_Tp>::operator ()(Point pt)
1638{
1639 CV_DbgAssert(dims <= 2);
1641 CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
1642 CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
1643 CV_DbgAssert(type() == traits::Type<_Tp>::value);
1644 return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
1645}
1646
1647template<typename _Tp> inline
1648const _Tp& Mat_<_Tp>::operator ()(Point pt) const
1649{
1650 CV_DbgAssert(dims <= 2);
1652 CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
1653 CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
1654 CV_DbgAssert(type() == traits::Type<_Tp>::value);
1655 return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
1656}
1657
1658template<typename _Tp> inline
1659_Tp& Mat_<_Tp>::operator ()(const int* idx)
1660{
1661 return Mat::at<_Tp>(idx);
1662}
1663
1664template<typename _Tp> inline
1665const _Tp& Mat_<_Tp>::operator ()(const int* idx) const
1666{
1667 return Mat::at<_Tp>(idx);
1668}
1669
1670template<typename _Tp> template<int n> inline
1671_Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx)
1672{
1673 return Mat::at<_Tp>(idx);
1674}
1675
1676template<typename _Tp> template<int n> inline
1677const _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx) const
1678{
1679 return Mat::at<_Tp>(idx);
1680}
1681
1682template<typename _Tp> inline
1683_Tp& Mat_<_Tp>::operator ()(int i0)
1684{
1685 return this->at<_Tp>(i0);
1686}
1687
1688template<typename _Tp> inline
1689const _Tp& Mat_<_Tp>::operator ()(int i0) const
1690{
1691 return this->at<_Tp>(i0);
1692}
1693
1694template<typename _Tp> inline
1695_Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2)
1696{
1697 return this->at<_Tp>(i0, i1, i2);
1698}
1699
1700template<typename _Tp> inline
1701const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const
1702{
1703 return this->at<_Tp>(i0, i1, i2);
1704}
1705
1706template<typename _Tp> inline
1707Mat_<_Tp>::operator std::vector<_Tp>() const
1708{
1710 copyTo(v);
1711 return v;
1712}
1713
1714template<typename _Tp> template<std::size_t _Nm> inline
1715Mat_<_Tp>::operator std::array<_Tp, _Nm>() const
1716{
1718 copyTo(a);
1719 return a;
1720}
1721
1722template<typename _Tp> template<int n> inline
1723Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const
1724{
1725 CV_Assert(n % DataType<_Tp>::channels == 0);
1726
1727#if defined _MSC_VER
1728 const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
1729 return pMat->operator Vec<typename DataType<_Tp>::channel_type, n>();
1730#else
1731 return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>();
1732#endif
1733}
1734
1735template<typename _Tp> template<int m, int n> inline
1736Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const
1737{
1738 CV_Assert(n % DataType<_Tp>::channels == 0);
1739
1740#if defined _MSC_VER
1741 const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
1742 Matx<typename DataType<_Tp>::channel_type, m, n> res = pMat->operator Matx<typename DataType<_Tp>::channel_type, m, n>();
1743 return res;
1744#else
1745 Matx<typename DataType<_Tp>::channel_type, m, n> res = this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>();
1746 return res;
1747#endif
1748}
1749
1750template<typename _Tp> inline
1751MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
1752{
1753 return Mat::begin<_Tp>();
1754}
1755
1756template<typename _Tp> inline
1757std::reverse_iterator<MatConstIterator_<_Tp>> Mat_<_Tp>::rbegin() const
1758{
1759 return Mat::rbegin<_Tp>();
1760}
1761
1762template<typename _Tp> inline
1763MatConstIterator_<_Tp> Mat_<_Tp>::end() const
1764{
1765 return Mat::end<_Tp>();
1766}
1767
1768template<typename _Tp> inline
1769std::reverse_iterator<MatConstIterator_<_Tp>> Mat_<_Tp>::rend() const
1770{
1771 return Mat::rend<_Tp>();
1772}
1773
1774template<typename _Tp> inline
1775MatIterator_<_Tp> Mat_<_Tp>::begin()
1776{
1777 return Mat::begin<_Tp>();
1778}
1779
1780template<typename _Tp> inline
1781std::reverse_iterator<MatIterator_<_Tp>> Mat_<_Tp>::rbegin()
1782{
1783 return Mat::rbegin<_Tp>();
1784}
1785
1786template<typename _Tp> inline
1787MatIterator_<_Tp> Mat_<_Tp>::end()
1788{
1789 return Mat::end<_Tp>();
1790}
1791
1792template<typename _Tp> inline
1794{
1795 return Mat::rend<_Tp>();
1796}
1797
1798template<typename _Tp> template<typename Functor> inline
1799void Mat_<_Tp>::forEach(const Functor& operation) {
1800 Mat::forEach<_Tp, Functor>(operation);
1801}
1802
1803template<typename _Tp> template<typename Functor> inline
1804void Mat_<_Tp>::forEach(const Functor& operation) const {
1805 Mat::forEach<_Tp, Functor>(operation);
1806}
1807
1808template<typename _Tp> inline
1809Mat_<_Tp>::Mat_(Mat_&& m)
1810 : Mat(std::move(m))
1811{
1812}
1813
1814template<typename _Tp> inline
1815Mat_<_Tp>& Mat_<_Tp>::operator = (Mat_&& m)
1816{
1817 Mat::operator = (std::move(m));
1818 return *this;
1819}
1820
1821template<typename _Tp> inline
1822Mat_<_Tp>::Mat_(Mat&& m)
1823 : Mat()
1824{
1825 flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
1826 *this = std::move(m);
1827}
1828
1829template<typename _Tp> inline
1830Mat_<_Tp>& Mat_<_Tp>::operator = (Mat&& m)
1831{
1832 if (m.empty())
1833 {
1834 release();
1835 return *this;
1836 }
1837 if( traits::Type<_Tp>::value == m.type() )
1838 {
1839 Mat::operator = ((Mat&&)m);
1840 return *this;
1841 }
1842 if( traits::Depth<_Tp>::value == m.depth() )
1843 {
1844 Mat::operator = ((Mat&&)m.reshape(DataType<_Tp>::channels, m.dims, 0));
1845 return *this;
1846 }
1847 CV_DbgAssert(DataType<_Tp>::channels == m.channels());
1848 m.convertTo(*this, type());
1849 return *this;
1850}
1851
1852template<typename _Tp> inline
1853Mat_<_Tp>::Mat_(MatExpr&& e)
1854 : Mat()
1855{
1856 flags = (flags & ~CV_MAT_TYPE_MASK) + traits::Type<_Tp>::value;
1857 *this = Mat(e);
1858}
1859
1860
1862
1863inline
1864SparseMat SparseMat::clone() const
1865{
1866 SparseMat temp;
1867 this->copyTo(temp);
1868 return temp;
1869}
1870
1871inline
1872size_t SparseMat::elemSize() const
1873{
1874 return CV_ELEM_SIZE(flags);
1875}
1876
1877inline
1878size_t SparseMat::elemSize1() const
1879{
1880 return CV_ELEM_SIZE1(flags);
1881}
1882
1883inline
1884int SparseMat::type() const
1885{
1886 return CV_MAT_TYPE(flags);
1887}
1888
1889inline
1890int SparseMat::depth() const
1891{
1892 return CV_MAT_DEPTH(flags);
1893}
1894
1895inline
1896int SparseMat::channels() const
1897{
1898 return CV_MAT_CN(flags);
1899}
1900
1901inline
1902const int* SparseMat::size() const
1903{
1904 return hdr ? hdr->size : 0;
1905}
1906
1907inline
1908int SparseMat::size(int i) const
1909{
1910 if( hdr )
1911 {
1912 CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
1913 return hdr->size[i];
1914 }
1915 return 0;
1916}
1917
1918inline
1919int SparseMat::dims() const
1920{
1921 return hdr ? hdr->dims : 0;
1922}
1923
1924inline
1925size_t SparseMat::nzcount() const
1926{
1927 return hdr ? hdr->nodeCount : 0;
1928}
1929
1930template<typename _Tp> inline
1931_Tp& SparseMat::ref(int i0, size_t* hashval)
1932{
1933 return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval);
1934}
1935
1936template<typename _Tp> inline
1937_Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
1938{
1939 return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval);
1940}
1941
1942template<typename _Tp> inline
1943_Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
1944{
1945 return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval);
1946}
1947
1948template<typename _Tp> inline
1949_Tp& SparseMat::ref(const int* idx, size_t* hashval)
1950{
1951 return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval);
1952}
1953
1954template<typename _Tp> inline
1955_Tp SparseMat::value(int i0, size_t* hashval) const
1956{
1957 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
1958 return p ? *p : _Tp();
1959}
1960
1961template<typename _Tp> inline
1962_Tp SparseMat::value(int i0, int i1, size_t* hashval) const
1963{
1964 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
1965 return p ? *p : _Tp();
1966}
1967
1968template<typename _Tp> inline
1969_Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
1970{
1971 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
1972 return p ? *p : _Tp();
1973}
1974
1975template<typename _Tp> inline
1976_Tp SparseMat::value(const int* idx, size_t* hashval) const
1977{
1978 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
1979 return p ? *p : _Tp();
1980}
1981
1982template<typename _Tp> inline
1983const _Tp* SparseMat::find(int i0, size_t* hashval) const
1984{
1985 return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
1986}
1987
1988template<typename _Tp> inline
1989const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
1990{
1991 return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
1992}
1993
1994template<typename _Tp> inline
1995const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
1996{
1997 return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
1998}
1999
2000template<typename _Tp> inline
2001const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
2002{
2003 return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
2004}
2005
2006template<typename _Tp> inline
2007_Tp& SparseMat::value(Node* n)
2008{
2009 return *(_Tp*)((uchar*)n + hdr->valueOffset);
2010}
2011
2012template<typename _Tp> inline
2013const _Tp& SparseMat::value(const Node* n) const
2014{
2015 return *(const _Tp*)((const uchar*)n + hdr->valueOffset);
2016}
2017
2018inline
2019SparseMat::Node* SparseMat::node(size_t nidx)
2020{
2021 return (Node*)(void*)&hdr->pool[nidx];
2022}
2023
2024inline
2025const SparseMat::Node* SparseMat::node(size_t nidx) const
2026{
2027 return (const Node*)(const void*)&hdr->pool[nidx];
2028}
2029
2030inline
2031SparseMatIterator SparseMat::begin()
2032{
2033 return SparseMatIterator(this);
2034}
2035
2036inline
2037SparseMatConstIterator SparseMat::begin() const
2038{
2039 return SparseMatConstIterator(this);
2040}
2041
2042inline
2043SparseMatIterator SparseMat::end()
2044{
2045 SparseMatIterator it(this);
2046 it.seekEnd();
2047 return it;
2048}
2049
2050inline
2051SparseMatConstIterator SparseMat::end() const
2052{
2053 SparseMatConstIterator it(this);
2054 it.seekEnd();
2055 return it;
2056}
2057
2058template<typename _Tp> inline
2059SparseMatIterator_<_Tp> SparseMat::begin()
2060{
2061 return SparseMatIterator_<_Tp>(this);
2062}
2063
2064template<typename _Tp> inline
2065SparseMatConstIterator_<_Tp> SparseMat::begin() const
2066{
2067 return SparseMatConstIterator_<_Tp>(this);
2068}
2069
2070template<typename _Tp> inline
2071SparseMatIterator_<_Tp> SparseMat::end()
2072{
2073 SparseMatIterator_<_Tp> it(this);
2074 it.seekEnd();
2075 return it;
2076}
2077
2078template<typename _Tp> inline
2079SparseMatConstIterator_<_Tp> SparseMat::end() const
2080{
2081 SparseMatConstIterator_<_Tp> it(this);
2082 it.seekEnd();
2083 return it;
2084}
2085
2086
2087
2089
2090template<typename _Tp> inline
2091SparseMat_<_Tp>::SparseMat_()
2092{
2093 flags = MAGIC_VAL + traits::Type<_Tp>::value;
2094}
2095
2096template<typename _Tp> inline
2097SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes)
2098 : SparseMat(_dims, _sizes, traits::Type<_Tp>::value)
2099{}
2100
2101template<typename _Tp> inline
2102SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
2103{
2104 if( m.type() == traits::Type<_Tp>::value )
2105 *this = (const SparseMat_<_Tp>&)m;
2106 else
2107 m.convertTo(*this, traits::Type<_Tp>::value);
2108}
2109
2110template<typename _Tp> inline
2111SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
2112{
2113 this->flags = m.flags;
2114 this->hdr = m.hdr;
2115 if( this->hdr )
2116 CV_XADD(&this->hdr->refcount, 1);
2117}
2118
2119template<typename _Tp> inline
2120SparseMat_<_Tp>::SparseMat_(const Mat& m)
2121{
2122 SparseMat sm(m);
2123 *this = sm;
2124}
2125
2126template<typename _Tp> inline
2127SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
2128{
2129 if( this != &m )
2130 {
2131 if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
2132 release();
2133 flags = m.flags;
2134 hdr = m.hdr;
2135 }
2136 return *this;
2137}
2138
2139template<typename _Tp> inline
2140SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat& m)
2141{
2142 if( m.type() == traits::Type<_Tp>::value )
2143 return (*this = (const SparseMat_<_Tp>&)m);
2144 m.convertTo(*this, traits::Type<_Tp>::value);
2145 return *this;
2146}
2147
2148template<typename _Tp> inline
2149SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const Mat& m)
2150{
2151 return (*this = SparseMat(m));
2152}
2153
2154template<typename _Tp> inline
2155SparseMat_<_Tp> SparseMat_<_Tp>::clone() const
2156{
2157 SparseMat_<_Tp> m;
2158 this->copyTo(m);
2159 return m;
2160}
2161
2162template<typename _Tp> inline
2163void SparseMat_<_Tp>::create(int _dims, const int* _sizes)
2164{
2165 SparseMat::create(_dims, _sizes, traits::Type<_Tp>::value);
2166}
2167
2168template<typename _Tp> inline
2169int SparseMat_<_Tp>::type() const
2170{
2171 return traits::Type<_Tp>::value;
2172}
2173
2174template<typename _Tp> inline
2175int SparseMat_<_Tp>::depth() const
2176{
2177 return traits::Depth<_Tp>::value;
2178}
2179
2180template<typename _Tp> inline
2181int SparseMat_<_Tp>::channels() const
2182{
2183 return DataType<_Tp>::channels;
2184}
2185
2186template<typename _Tp> inline
2187_Tp& SparseMat_<_Tp>::ref(int i0, size_t* hashval)
2188{
2189 return SparseMat::ref<_Tp>(i0, hashval);
2190}
2191
2192template<typename _Tp> inline
2193_Tp SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const
2194{
2195 return SparseMat::value<_Tp>(i0, hashval);
2196}
2197
2198template<typename _Tp> inline
2199_Tp& SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
2200{
2201 return SparseMat::ref<_Tp>(i0, i1, hashval);
2202}
2203
2204template<typename _Tp> inline
2205_Tp SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
2206{
2207 return SparseMat::value<_Tp>(i0, i1, hashval);
2208}
2209
2210template<typename _Tp> inline
2211_Tp& SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
2212{
2213 return SparseMat::ref<_Tp>(i0, i1, i2, hashval);
2214}
2215
2216template<typename _Tp> inline
2217_Tp SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
2218{
2219 return SparseMat::value<_Tp>(i0, i1, i2, hashval);
2220}
2221
2222template<typename _Tp> inline
2223_Tp& SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
2224{
2225 return SparseMat::ref<_Tp>(idx, hashval);
2226}
2227
2228template<typename _Tp> inline
2229_Tp SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
2230{
2231 return SparseMat::value<_Tp>(idx, hashval);
2232}
2233
2234template<typename _Tp> inline
2235SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
2236{
2237 return SparseMatIterator_<_Tp>(this);
2238}
2239
2240template<typename _Tp> inline
2241SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
2242{
2243 return SparseMatConstIterator_<_Tp>(this);
2244}
2245
2246template<typename _Tp> inline
2247SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
2248{
2249 SparseMatIterator_<_Tp> it(this);
2250 it.seekEnd();
2251 return it;
2252}
2253
2254template<typename _Tp> inline
2255SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
2256{
2257 SparseMatConstIterator_<_Tp> it(this);
2258 it.seekEnd();
2259 return it;
2260}
2261
2262
2263
2265
2266inline
2267MatConstIterator::MatConstIterator()
2268 : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0)
2269{}
2270
2271inline
2272MatConstIterator::MatConstIterator(const Mat* _m)
2273 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
2274{
2275 if( m && m->isContinuous() )
2276 {
2277 CV_Assert(!m->empty());
2278 sliceStart = m->ptr();
2279 sliceEnd = sliceStart + m->total()*elemSize;
2280 }
2281 seek((const int*)0);
2282}
2283
2284inline
2285MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
2286 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
2287{
2288 CV_Assert(m && m->dims <= 2);
2289 if( m->isContinuous() )
2290 {
2291 CV_Assert(!m->empty());
2292 sliceStart = m->ptr();
2293 sliceEnd = sliceStart + m->total()*elemSize;
2294 }
2295 int idx[] = {_row, _col};
2296 seek(idx);
2297}
2298
2299inline
2300MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
2301 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
2302{
2303 CV_Assert(m && m->dims <= 2);
2304 if( m->isContinuous() )
2305 {
2306 CV_Assert(!m->empty());
2307 sliceStart = m->ptr();
2308 sliceEnd = sliceStart + m->total()*elemSize;
2309 }
2310 int idx[] = {_pt.y, _pt.x};
2311 seek(idx);
2312}
2313
2314inline
2315MatConstIterator::MatConstIterator(const MatConstIterator& it)
2316 : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd)
2317{}
2318
2319inline
2320MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it )
2321{
2322 m = it.m; elemSize = it.elemSize; ptr = it.ptr;
2323 sliceStart = it.sliceStart; sliceEnd = it.sliceEnd;
2324 return *this;
2325}
2326
2327inline
2328const uchar* MatConstIterator::operator *() const
2329{
2330 return ptr;
2331}
2332
2333inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs)
2334{
2335 if( !m || ofs == 0 )
2336 return *this;
2337 ptrdiff_t ofsb = ofs*elemSize;
2338 ptr += ofsb;
2339 if( ptr < sliceStart || sliceEnd <= ptr )
2340 {
2341 ptr -= ofsb;
2342 seek(ofs, true);
2343 }
2344 return *this;
2345}
2346
2347inline
2348MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs)
2349{
2350 return (*this += -ofs);
2351}
2352
2353inline
2354MatConstIterator& MatConstIterator::operator --()
2355{
2356 if( m && (ptr -= elemSize) < sliceStart )
2357 {
2358 ptr += elemSize;
2359 seek(-1, true);
2360 }
2361 return *this;
2362}
2363
2364inline
2365MatConstIterator MatConstIterator::operator --(int)
2366{
2367 MatConstIterator b = *this;
2368 *this += -1;
2369 return b;
2370}
2371
2372inline
2373MatConstIterator& MatConstIterator::operator ++()
2374{
2375 if( m && (ptr += elemSize) >= sliceEnd )
2376 {
2377 ptr -= elemSize;
2378 seek(1, true);
2379 }
2380 return *this;
2381}
2382
2383inline MatConstIterator MatConstIterator::operator ++(int)
2384{
2385 MatConstIterator b = *this;
2386 *this += 1;
2387 return b;
2388}
2389
2390
2391static inline
2392bool operator == (const MatConstIterator& a, const MatConstIterator& b)
2393{
2394 return a.m == b.m && a.ptr == b.ptr;
2395}
2396
2397static inline
2398bool operator != (const MatConstIterator& a, const MatConstIterator& b)
2399{
2400 return !(a == b);
2401}
2402
2403static inline
2404bool operator < (const MatConstIterator& a, const MatConstIterator& b)
2405{
2406 return a.ptr < b.ptr;
2407}
2408
2409static inline
2410bool operator > (const MatConstIterator& a, const MatConstIterator& b)
2411{
2412 return a.ptr > b.ptr;
2413}
2414
2415static inline
2416bool operator <= (const MatConstIterator& a, const MatConstIterator& b)
2417{
2418 return a.ptr <= b.ptr;
2419}
2420
2421static inline
2422bool operator >= (const MatConstIterator& a, const MatConstIterator& b)
2423{
2424 return a.ptr >= b.ptr;
2425}
2426
2427static inline
2428ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a)
2429{
2430 if( a.m != b.m )
2431 return ((size_t)(-1) >> 1);
2432 if( a.sliceEnd == b.sliceEnd )
2433 return (b.ptr - a.ptr)/static_cast<ptrdiff_t>(b.elemSize);
2434
2435 return b.lpos() - a.lpos();
2436}
2437
2438static inline
2439MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs)
2440{
2441 MatConstIterator b = a;
2442 return b += ofs;
2443}
2444
2445static inline
2446MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a)
2447{
2448 MatConstIterator b = a;
2449 return b += ofs;
2450}
2451
2452static inline
2453MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs)
2454{
2455 MatConstIterator b = a;
2456 return b += -ofs;
2457}
2458
2459
2460inline
2461const uchar* MatConstIterator::operator [](ptrdiff_t i) const
2462{
2463 return *(*this + i);
2464}
2465
2466
2467
2469
2470template<typename _Tp> inline
2471MatConstIterator_<_Tp>::MatConstIterator_()
2472{}
2473
2474template<typename _Tp> inline
2475MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m)
2476 : MatConstIterator(_m)
2477{}
2478
2479template<typename _Tp> inline
2480MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col)
2481 : MatConstIterator(_m, _row, _col)
2482{}
2483
2484template<typename _Tp> inline
2485MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, Point _pt)
2486 : MatConstIterator(_m, _pt)
2487{}
2488
2489template<typename _Tp> inline
2490MatConstIterator_<_Tp>::MatConstIterator_(const MatConstIterator_& it)
2491 : MatConstIterator(it)
2492{}
2493
2494template<typename _Tp> inline
2495MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
2496{
2497 MatConstIterator::operator = (it);
2498 return *this;
2499}
2500
2501template<typename _Tp> inline
2502const _Tp& MatConstIterator_<_Tp>::operator *() const
2503{
2504 return *(_Tp*)(this->ptr);
2505}
2506
2507template<typename _Tp> inline
2508MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs)
2509{
2510 MatConstIterator::operator += (ofs);
2511 return *this;
2512}
2513
2514template<typename _Tp> inline
2515MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs)
2516{
2517 return (*this += -ofs);
2518}
2519
2520template<typename _Tp> inline
2521MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
2522{
2523 MatConstIterator::operator --();
2524 return *this;
2525}
2526
2527template<typename _Tp> inline
2528MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
2529{
2530 MatConstIterator_ b = *this;
2531 MatConstIterator::operator --();
2532 return b;
2533}
2534
2535template<typename _Tp> inline
2536MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
2537{
2538 MatConstIterator::operator ++();
2539 return *this;
2540}
2541
2542template<typename _Tp> inline
2543MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
2544{
2545 MatConstIterator_ b = *this;
2546 MatConstIterator::operator ++();
2547 return b;
2548}
2549
2550
2551template<typename _Tp> inline
2552Point MatConstIterator_<_Tp>::pos() const
2553{
2554 if( !m )
2555 return Point();
2556 CV_DbgAssert( m->dims <= 2 );
2557 if( m->isContinuous() )
2558 {
2559 ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data;
2560 int y = (int)(ofs / m->cols);
2561 int x = (int)(ofs - (ptrdiff_t)y * m->cols);
2562 return Point(x, y);
2563 }
2564 else
2565 {
2566 ptrdiff_t ofs = (uchar*)ptr - m->data;
2567 int y = (int)(ofs / m->step);
2568 int x = (int)((ofs - y * m->step)/sizeof(_Tp));
2569 return Point(x, y);
2570 }
2571}
2572
2573
2574template<typename _Tp> static inline
2575bool operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
2576{
2577 return a.m == b.m && a.ptr == b.ptr;
2578}
2579
2580template<typename _Tp> static inline
2581bool operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
2582{
2583 return a.m != b.m || a.ptr != b.ptr;
2584}
2585
2586template<typename _Tp> static inline
2587MatConstIterator_<_Tp> operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
2588{
2589 MatConstIterator t = (const MatConstIterator&)a + ofs;
2590 return (MatConstIterator_<_Tp>&)t;
2591}
2592
2593template<typename _Tp> static inline
2594MatConstIterator_<_Tp> operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a)
2595{
2596 MatConstIterator t = (const MatConstIterator&)a + ofs;
2597 return (MatConstIterator_<_Tp>&)t;
2598}
2599
2600template<typename _Tp> static inline
2601MatConstIterator_<_Tp> operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
2602{
2603 MatConstIterator t = (const MatConstIterator&)a - ofs;
2604 return (MatConstIterator_<_Tp>&)t;
2605}
2606
2607template<typename _Tp> inline
2608const _Tp& MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const
2609{
2610 return *(_Tp*)MatConstIterator::operator [](i);
2611}
2612
2613
2614
2616
2617template<typename _Tp> inline
2618MatIterator_<_Tp>::MatIterator_()
2619 : MatConstIterator_<_Tp>()
2620{}
2621
2622template<typename _Tp> inline
2623MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
2624 : MatConstIterator_<_Tp>(_m)
2625{}
2626
2627template<typename _Tp> inline
2628MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
2629 : MatConstIterator_<_Tp>(_m, _row, _col)
2630{}
2631
2632template<typename _Tp> inline
2633MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, Point _pt)
2634 : MatConstIterator_<_Tp>(_m, _pt)
2635{}
2636
2637template<typename _Tp> inline
2638MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, const int* _idx)
2639 : MatConstIterator_<_Tp>(_m, _idx)
2640{}
2641
2642template<typename _Tp> inline
2643MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
2644 : MatConstIterator_<_Tp>(it)
2645{}
2646
2647template<typename _Tp> inline
2648MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
2649{
2650 MatConstIterator::operator = (it);
2651 return *this;
2652}
2653
2654template<typename _Tp> inline
2655_Tp& MatIterator_<_Tp>::operator *() const
2656{
2657 return *(_Tp*)(this->ptr);
2658}
2659
2660template<typename _Tp> inline
2661MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs)
2662{
2663 MatConstIterator::operator += (ofs);
2664 return *this;
2665}
2666
2667template<typename _Tp> inline
2668MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs)
2669{
2670 MatConstIterator::operator += (-ofs);
2671 return *this;
2672}
2673
2674template<typename _Tp> inline
2675MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
2676{
2677 MatConstIterator::operator --();
2678 return *this;
2679}
2680
2681template<typename _Tp> inline
2682MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
2683{
2684 MatIterator_ b = *this;
2685 MatConstIterator::operator --();
2686 return b;
2687}
2688
2689template<typename _Tp> inline
2690MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
2691{
2692 MatConstIterator::operator ++();
2693 return *this;
2694}
2695
2696template<typename _Tp> inline
2697MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
2698{
2699 MatIterator_ b = *this;
2700 MatConstIterator::operator ++();
2701 return b;
2702}
2703
2704template<typename _Tp> inline
2705_Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const
2706{
2707 return *(*this + i);
2708}
2709
2710
2711template<typename _Tp> static inline
2712bool operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
2713{
2714 return a.m == b.m && a.ptr == b.ptr;
2715}
2716
2717template<typename _Tp> static inline
2718bool operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
2719{
2720 return a.m != b.m || a.ptr != b.ptr;
2721}
2722
2723template<typename _Tp> static inline
2724MatIterator_<_Tp> operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
2725{
2726 MatConstIterator t = (const MatConstIterator&)a + ofs;
2727 return (MatIterator_<_Tp>&)t;
2728}
2729
2730template<typename _Tp> static inline
2731MatIterator_<_Tp> operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a)
2732{
2733 MatConstIterator t = (const MatConstIterator&)a + ofs;
2734 return (MatIterator_<_Tp>&)t;
2735}
2736
2737template<typename _Tp> static inline
2738MatIterator_<_Tp> operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
2739{
2740 MatConstIterator t = (const MatConstIterator&)a - ofs;
2741 return (MatIterator_<_Tp>&)t;
2742}
2743
2744
2745
2747
2748inline
2749SparseMatConstIterator::SparseMatConstIterator()
2750 : m(0), hashidx(0), ptr(0)
2751{}
2752
2753inline
2754SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
2755 : m(it.m), hashidx(it.hashidx), ptr(it.ptr)
2756{}
2757
2758inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
2759{
2760 if( this != &it )
2761 {
2762 m = it.m;
2763 hashidx = it.hashidx;
2764 ptr = it.ptr;
2765 }
2766 return *this;
2767}
2768
2769template<typename _Tp> inline
2770const _Tp& SparseMatConstIterator::value() const
2771{
2772 return *(const _Tp*)ptr;
2773}
2774
2775inline
2776const SparseMat::Node* SparseMatConstIterator::node() const
2777{
2778 return (ptr && m && m->hdr) ? (const SparseMat::Node*)(const void*)(ptr - m->hdr->valueOffset) : 0;
2779}
2780
2781inline
2782SparseMatConstIterator SparseMatConstIterator::operator ++(int)
2783{
2784 SparseMatConstIterator it = *this;
2785 ++*this;
2786 return it;
2787}
2788
2789inline
2790void SparseMatConstIterator::seekEnd()
2791{
2792 if( m && m->hdr )
2793 {
2794 hashidx = m->hdr->hashtab.size();
2795 ptr = 0;
2796 }
2797}
2798
2799
2800static inline
2801bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
2802{
2803 return it1.m == it2.m && it1.ptr == it2.ptr;
2804}
2805
2806static inline
2807bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
2808{
2809 return !(it1 == it2);
2810}
2811
2812
2813
2815
2816inline
2817SparseMatIterator::SparseMatIterator()
2818{}
2819
2820inline
2821SparseMatIterator::SparseMatIterator(SparseMat* _m)
2822 : SparseMatConstIterator(_m)
2823{}
2824
2825inline
2826SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
2827 : SparseMatConstIterator(it)
2828{}
2829
2830inline
2831SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
2832{
2833 (SparseMatConstIterator&)*this = it;
2834 return *this;
2835}
2836
2837template<typename _Tp> inline
2838_Tp& SparseMatIterator::value() const
2839{
2840 return *(_Tp*)ptr;
2841}
2842
2843inline
2844SparseMat::Node* SparseMatIterator::node() const
2845{
2846 return (SparseMat::Node*)SparseMatConstIterator::node();
2847}
2848
2849inline
2850SparseMatIterator& SparseMatIterator::operator ++()
2851{
2852 SparseMatConstIterator::operator ++();
2853 return *this;
2854}
2855
2856inline
2857SparseMatIterator SparseMatIterator::operator ++(int)
2858{
2859 SparseMatIterator it = *this;
2860 ++*this;
2861 return it;
2862}
2863
2864
2865
2867
2868template<typename _Tp> inline
2869SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
2870{}
2871
2872template<typename _Tp> inline
2873SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
2874 : SparseMatConstIterator(_m)
2875{}
2876
2877template<typename _Tp> inline
2878SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat* _m)
2879 : SparseMatConstIterator(_m)
2880{
2881 CV_Assert( _m->type() == traits::Type<_Tp>::value );
2882}
2883
2884template<typename _Tp> inline
2885SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
2886 : SparseMatConstIterator(it)
2887{}
2888
2889template<typename _Tp> inline
2890SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
2891{
2892 return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
2893 (*reinterpret_cast<SparseMatConstIterator*>(this) =
2894 reinterpret_cast<const SparseMatConstIterator&>(it));
2895}
2896
2897template<typename _Tp> inline
2898const _Tp& SparseMatConstIterator_<_Tp>::operator *() const
2899{
2900 return *(const _Tp*)this->ptr;
2901}
2902
2903template<typename _Tp> inline
2904SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator ++()
2905{
2906 SparseMatConstIterator::operator ++();
2907 return *this;
2908}
2909
2910template<typename _Tp> inline
2911SparseMatConstIterator_<_Tp> SparseMatConstIterator_<_Tp>::operator ++(int)
2912{
2913 SparseMatConstIterator_<_Tp> it = *this;
2914 SparseMatConstIterator::operator ++();
2915 return it;
2916}
2917
2918
2919
2921
2922template<typename _Tp> inline
2923SparseMatIterator_<_Tp>::SparseMatIterator_()
2924{}
2925
2926template<typename _Tp> inline
2927SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
2928 : SparseMatConstIterator_<_Tp>(_m)
2929{}
2930
2931template<typename _Tp> inline
2932SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat* _m)
2933 : SparseMatConstIterator_<_Tp>(_m)
2934{}
2935
2936template<typename _Tp> inline
2937SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
2938 : SparseMatConstIterator_<_Tp>(it)
2939{}
2940
2941template<typename _Tp> inline
2942SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
2943{
2944 return reinterpret_cast<SparseMatIterator_<_Tp>&>
2945 (*reinterpret_cast<SparseMatConstIterator*>(this) =
2946 reinterpret_cast<const SparseMatConstIterator&>(it));
2947}
2948
2949template<typename _Tp> inline
2950_Tp& SparseMatIterator_<_Tp>::operator *() const
2951{
2952 return *(_Tp*)this->ptr;
2953}
2954
2955template<typename _Tp> inline
2956SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator ++()
2957{
2958 SparseMatConstIterator::operator ++();
2959 return *this;
2960}
2961
2962template<typename _Tp> inline
2963SparseMatIterator_<_Tp> SparseMatIterator_<_Tp>::operator ++(int)
2964{
2965 SparseMatIterator_<_Tp> it = *this;
2966 SparseMatConstIterator::operator ++();
2967 return it;
2968}
2969
2970
2971
2973
2974template<typename _Tp> inline
2975MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m)
2976 : it(_m)
2977{}
2978
2979template<typename _Tp> template<typename T2> inline
2980MatCommaInitializer_<_Tp>& MatCommaInitializer_<_Tp>::operator , (T2 v)
2981{
2982 CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() );
2983 *this->it = _Tp(v);
2984 ++this->it;
2985 return *this;
2986}
2987
2988template<typename _Tp> inline
2989MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
2990{
2991 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
2992 return Mat_<_Tp>(*this->it.m);
2993}
2994
2995
2996template<typename _Tp, typename T2> static inline
2997MatCommaInitializer_<_Tp> operator << (const Mat_<_Tp>& m, T2 val)
2998{
2999 MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
3000 return (commaInitializer, val);
3001}
3002
3003
3004
3006
3007inline
3008Mat& Mat::operator = (const MatExpr& e)
3009{
3010 e.op->assign(e, *this);
3011 return *this;
3012}
3013
3014template<typename _Tp> inline
3015Mat_<_Tp>::Mat_(const MatExpr& e)
3016{
3017 e.op->assign(e, *this, traits::Type<_Tp>::value);
3018}
3019
3020template<typename _Tp> inline
3021Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e)
3022{
3023 e.op->assign(e, *this, traits::Type<_Tp>::value);
3024 return *this;
3025}
3026
3027template<typename _Tp> inline
3028MatExpr Mat_<_Tp>::zeros(int rows, int cols)
3029{
3030 return Mat::zeros(rows, cols, traits::Type<_Tp>::value);
3031}
3032
3033template<typename _Tp> inline
3034MatExpr Mat_<_Tp>::zeros(Size sz)
3035{
3036 return Mat::zeros(sz, traits::Type<_Tp>::value);
3037}
3038
3039template<typename _Tp> inline
3040MatExpr Mat_<_Tp>::ones(int rows, int cols)
3041{
3042 return Mat::ones(rows, cols, traits::Type<_Tp>::value);
3043}
3044
3045template<typename _Tp> inline
3046MatExpr Mat_<_Tp>::ones(Size sz)
3047{
3048 return Mat::ones(sz, traits::Type<_Tp>::value);
3049}
3050
3051template<typename _Tp> inline
3052MatExpr Mat_<_Tp>::eye(int rows, int cols)
3053{
3054 return Mat::eye(rows, cols, traits::Type<_Tp>::value);
3055}
3056
3057template<typename _Tp> inline
3058MatExpr Mat_<_Tp>::eye(Size sz)
3059{
3060 return Mat::eye(sz, traits::Type<_Tp>::value);
3061}
3062
3063inline
3064MatExpr::MatExpr()
3065 : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s()
3066{}
3067
3068inline
3069MatExpr::MatExpr(const MatOp* _op, int _flags, const Mat& _a, const Mat& _b,
3070 const Mat& _c, double _alpha, double _beta, const Scalar& _s)
3071 : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s)
3072{}
3073
3074inline
3075MatExpr::operator Mat() const
3076{
3077 Mat m;
3078 op->assign(*this, m);
3079 return m;
3080}
3081
3082template<typename _Tp> inline
3083MatExpr::operator Mat_<_Tp>() const
3084{
3085 Mat_<_Tp> m;
3086 op->assign(*this, m, traits::Type<_Tp>::value);
3087 return m;
3088}
3089
3090
3091template<typename _Tp> static inline
3092MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
3093{
3094 return cv::min((const Mat&)a, (const Mat&)b);
3095}
3096
3097template<typename _Tp> static inline
3098MatExpr min(const Mat_<_Tp>& a, double s)
3099{
3100 return cv::min((const Mat&)a, s);
3101}
3102
3103template<typename _Tp> static inline
3104MatExpr min(double s, const Mat_<_Tp>& a)
3105{
3106 return cv::min((const Mat&)a, s);
3107}
3108
3109template<typename _Tp> static inline
3110MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
3111{
3112 return cv::max((const Mat&)a, (const Mat&)b);
3113}
3114
3115template<typename _Tp> static inline
3116MatExpr max(const Mat_<_Tp>& a, double s)
3117{
3118 return cv::max((const Mat&)a, s);
3119}
3120
3121template<typename _Tp> static inline
3122MatExpr max(double s, const Mat_<_Tp>& a)
3123{
3124 return cv::max((const Mat&)a, s);
3125}
3126
3127template<typename _Tp> static inline
3128MatExpr abs(const Mat_<_Tp>& m)
3129{
3130 return cv::abs((const Mat&)m);
3131}
3132
3133
3134static inline
3135Mat& operator += (Mat& a, const MatExpr& b)
3136{
3137 b.op->augAssignAdd(b, a);
3138 return a;
3139}
3140
3141static inline
3142const Mat& operator += (const Mat& a, const MatExpr& b)
3143{
3144 b.op->augAssignAdd(b, (Mat&)a);
3145 return a;
3146}
3147
3148template<typename _Tp> static inline
3149Mat_<_Tp>& operator += (Mat_<_Tp>& a, const MatExpr& b)
3150{
3151 b.op->augAssignAdd(b, a);
3152 return a;
3153}
3154
3155template<typename _Tp> static inline
3156const Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b)
3157{
3158 b.op->augAssignAdd(b, (Mat&)a);
3159 return a;
3160}
3161
3162static inline
3163Mat& operator -= (Mat& a, const MatExpr& b)
3164{
3165 b.op->augAssignSubtract(b, a);
3166 return a;
3167}
3168
3169static inline
3170const Mat& operator -= (const Mat& a, const MatExpr& b)
3171{
3172 b.op->augAssignSubtract(b, (Mat&)a);
3173 return a;
3174}
3175
3176template<typename _Tp> static inline
3177Mat_<_Tp>& operator -= (Mat_<_Tp>& a, const MatExpr& b)
3178{
3179 b.op->augAssignSubtract(b, a);
3180 return a;
3181}
3182
3183template<typename _Tp> static inline
3184const Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b)
3185{
3186 b.op->augAssignSubtract(b, (Mat&)a);
3187 return a;
3188}
3189
3190static inline
3191Mat& operator *= (Mat& a, const MatExpr& b)
3192{
3193 b.op->augAssignMultiply(b, a);
3194 return a;
3195}
3196
3197static inline
3198const Mat& operator *= (const Mat& a, const MatExpr& b)
3199{
3200 b.op->augAssignMultiply(b, (Mat&)a);
3201 return a;
3202}
3203
3204template<typename _Tp> static inline
3205Mat_<_Tp>& operator *= (Mat_<_Tp>& a, const MatExpr& b)
3206{
3207 b.op->augAssignMultiply(b, a);
3208 return a;
3209}
3210
3211template<typename _Tp> static inline
3212const Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b)
3213{
3214 b.op->augAssignMultiply(b, (Mat&)a);
3215 return a;
3216}
3217
3218static inline
3219Mat& operator /= (Mat& a, const MatExpr& b)
3220{
3221 b.op->augAssignDivide(b, a);
3222 return a;
3223}
3224
3225static inline
3226const Mat& operator /= (const Mat& a, const MatExpr& b)
3227{
3228 b.op->augAssignDivide(b, (Mat&)a);
3229 return a;
3230}
3231
3232template<typename _Tp> static inline
3233Mat_<_Tp>& operator /= (Mat_<_Tp>& a, const MatExpr& b)
3234{
3235 b.op->augAssignDivide(b, a);
3236 return a;
3237}
3238
3239template<typename _Tp> static inline
3240const Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b)
3241{
3242 b.op->augAssignDivide(b, (Mat&)a);
3243 return a;
3244}
3245
3246
3248
3249template<typename _Tp> inline
3250UMat::UMat(const std::vector<_Tp>& vec, bool copyData)
3251: flags(MAGIC_VAL + traits::Type<_Tp>::value + CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
3252cols(1), allocator(0), usageFlags(USAGE_DEFAULT), u(0), offset(0), size(&rows)
3253{
3254 if(vec.empty())
3255 return;
3256 if( !copyData )
3257 {
3258 // !!!TODO!!!
3259 CV_Error(Error::StsNotImplemented, "");
3260 }
3261 else
3262 Mat((int)vec.size(), 1, traits::Type<_Tp>::value, (uchar*)&vec[0]).copyTo(*this);
3263}
3264
3265inline
3266UMat UMat::row(int y) const
3267{
3268 return UMat(*this, Range(y, y + 1), Range::all());
3269}
3270
3271inline
3272UMat UMat::col(int x) const
3273{
3274 return UMat(*this, Range::all(), Range(x, x + 1));
3275}
3276
3277inline
3278UMat UMat::rowRange(int startrow, int endrow) const
3279{
3280 return UMat(*this, Range(startrow, endrow), Range::all());
3281}
3282
3283inline
3284UMat UMat::rowRange(const Range& r) const
3285{
3286 return UMat(*this, r, Range::all());
3287}
3288
3289inline
3290UMat UMat::colRange(int startcol, int endcol) const
3291{
3292 return UMat(*this, Range::all(), Range(startcol, endcol));
3293}
3294
3295inline
3296UMat UMat::colRange(const Range& r) const
3297{
3298 return UMat(*this, Range::all(), r);
3299}
3300
3301inline
3302UMat UMat::operator()( Range _rowRange, Range _colRange ) const
3303{
3304 return UMat(*this, _rowRange, _colRange);
3305}
3306
3307inline
3308UMat UMat::operator()( const Rect& roi ) const
3309{
3310 return UMat(*this, roi);
3311}
3312
3313inline
3314UMat UMat::operator()(const Range* ranges) const
3315{
3316 return UMat(*this, ranges);
3317}
3318
3319inline
3320UMat UMat::operator()(const std::vector<Range>& ranges) const
3321{
3322 return UMat(*this, ranges);
3323}
3324
3325inline
3326bool UMat::isContinuous() const
3327{
3328 return (flags & CONTINUOUS_FLAG) != 0;
3329}
3330
3331inline
3332bool UMat::isSubmatrix() const
3333{
3334 return (flags & SUBMATRIX_FLAG) != 0;
3335}
3336
3337inline
3338size_t UMat::elemSize() const
3339{
3340 size_t res = dims > 0 ? step.p[dims - 1] : 0;
3341 CV_DbgAssert(res != 0);
3342 return res;
3343}
3344
3345inline
3346size_t UMat::elemSize1() const
3347{
3348 return CV_ELEM_SIZE1(flags);
3349}
3350
3351inline
3352int UMat::type() const
3353{
3354 return CV_MAT_TYPE(flags);
3355}
3356
3357inline
3358int UMat::depth() const
3359{
3360 return CV_MAT_DEPTH(flags);
3361}
3362
3363inline
3364int UMat::channels() const
3365{
3366 return CV_MAT_CN(flags);
3367}
3368
3369inline
3370size_t UMat::step1(int i) const
3371{
3372 return step.p[i] / elemSize1();
3373}
3374
3375
3376inline bool UMatData::hostCopyObsolete() const { return (flags & HOST_COPY_OBSOLETE) != 0; }
3377inline bool UMatData::deviceCopyObsolete() const { return (flags & DEVICE_COPY_OBSOLETE) != 0; }
3378inline bool UMatData::deviceMemMapped() const { return (flags & DEVICE_MEM_MAPPED) != 0; }
3379inline bool UMatData::copyOnMap() const { return (flags & COPY_ON_MAP) != 0; }
3380inline bool UMatData::tempUMat() const { return (flags & TEMP_UMAT) != 0; }
3381inline bool UMatData::tempCopiedUMat() const { return (flags & TEMP_COPIED_UMAT) == TEMP_COPIED_UMAT; }
3382
3383inline void UMatData::markDeviceMemMapped(bool flag)
3384{
3385 if(flag)
3386 flags |= DEVICE_MEM_MAPPED;
3387 else
3388 flags &= ~DEVICE_MEM_MAPPED;
3389}
3390
3391inline void UMatData::markHostCopyObsolete(bool flag)
3392{
3393 if(flag)
3394 flags |= HOST_COPY_OBSOLETE;
3395 else
3396 flags &= ~HOST_COPY_OBSOLETE;
3397}
3398inline void UMatData::markDeviceCopyObsolete(bool flag)
3399{
3400 if(flag)
3401 flags |= DEVICE_COPY_OBSOLETE;
3402 else
3403 flags &= ~DEVICE_COPY_OBSOLETE;
3404}
3405
3407
3408static inline
3409void swap(MatExpr& a, MatExpr& b) { a.swap(b); }
3410
3411} //cv
3412
3413#ifdef _MSC_VER
3414#pragma warning( pop )
3415#endif
3416
3417#ifdef CV_DISABLE_CLANG_ENUM_WARNINGS
3418#undef CV_DISABLE_CLANG_ENUM_WARNINGS
3419#pragma clang diagnostic pop
3420#endif
3421
3422#endif
Matrix expression representation This is a list of implemented matrix operations that can be combined...
Definition mat.hpp:3586
void swap(MatExpr &b)
Mat() CV_NOEXCEPT
_Tp height
the height
Definition types.hpp:363
_Tp width
the width
Definition types.hpp:362
int getFlags() const
void init(int _flags, const void *_obj)
@ CUDA_GPU_MAT
Definition mat.hpp:179
@ STD_VECTOR_VECTOR
Definition mat.hpp:172
@ UMAT
Definition mat.hpp:180
@ FIXED_SIZE
Definition mat.hpp:165
@ STD_VECTOR_MAT
Definition mat.hpp:173
@ FIXED_TYPE
Definition mat.hpp:164
@ MATX
Definition mat.hpp:170
@ STD_ARRAY_MAT
Definition mat.hpp:187
@ CUDA_HOST_MEM
Definition mat.hpp:178
@ MAT
Definition mat.hpp:169
@ NONE
Definition mat.hpp:168
@ STD_VECTOR_UMAT
Definition mat.hpp:181
@ STD_VECTOR_CUDA_GPU_MAT
Definition mat.hpp:183
@ STD_VECTOR
Definition mat.hpp:171
@ STD_BOOL_VECTOR
Definition mat.hpp:182
@ OPENGL_BUFFER
Definition mat.hpp:177
bool isUMatVector() const
bool isGpuMatVector() const
Size getSz() const
static _InputArray rawIn(const std::vector< _Tp > &vec)
bool isGpuMat() const
bool isMat() const
Size sz
Definition mat.hpp:262
bool isUMat() const
void * getObj() const
Mat getMat(int idx=-1) const
void * obj
Definition mat.hpp:261
_InputArray::KindFlag kind() const
Mat getMat_(int idx=-1) const
bool isMatVector() const
bool isVector() const
bool isMatx() const
int flags
Definition mat.hpp:260
static _InputOutputArray rawInOut(std::vector< _Tp > &vec)
static _OutputArray rawOut(std::vector< _Tp > &vec)
T empty(T... args)
InputArrayOfArrays Size InputOutputArray InputOutputArray OutputArrayOfArrays OutputArrayOfArrays OutputArray OutputArray OutputArray int flags
Definition calib3d.hpp:1617
void CV_EXPORTS_W copyTo(InputArray src, OutputArray dst, InputArray mask)
This is an overloaded member function, provided for convenience (python) Copies the matrix to another...
CV_EXPORTS_W void max(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element maximum of two arrays or an array and a scalar.
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
Calculates per-element minimum of two arrays or an array and a scalar.
static _InputOutputArray rawInOut(_Tp &v)
Point2i Point
Definition types.hpp:209
Size2i Size
Definition types.hpp:370
static _InputArray rawIn(_Tp &v)
CV__DEBUG_NS_BEGIN class CV_EXPORTS _OutputArray
Definition mat.hpp:72
Scalar_< double > Scalar
Definition types.hpp:702
static _OutputArray rawOut(_Tp &v)
@ USAGE_DEFAULT
Definition mat.hpp:465
@ ACCESS_READ
Definition mat.hpp:65
@ ACCESS_RW
Definition mat.hpp:66
@ ACCESS_WRITE
Definition mat.hpp:65
int rows
Definition core_c.h:257
int CvScalar value
Definition core_c.h:720
int int void int total
Definition core_c.h:1689
const CvArr const CvArr const CvArr CvArr int flags
Definition core_c.h:1342
int int channels
Definition core_c.h:100
const void * elem
Definition core_c.h:1715
int cols
Definition core_c.h:221
double const CvArr double beta
Definition core_c.h:1094
const int * idx
Definition core_c.h:668
CvSize size
Definition core_c.h:112
int int type
Definition core_c.h:221
void int step
Definition core_c.h:905
CvArr * arr
Definition core_c.h:1247
void * data
Definition core_c.h:427
const CvArr CvArr * x
Definition core_c.h:1195
double alpha
Definition core_c.h:1093
int dims
Definition core_c.h:464
const CvArr * y
Definition core_c.h:1187
const int * sizes
Definition core_c.h:440
#define CV_64F
Definition interface.h:79
#define CV_8U
Definition interface.h:73
#define CV_MAT_DEPTH(flags)
Definition interface.h:83
#define CV_CN_MAX
Definition interface.h:69
unsigned char uchar
Definition interface.h:51
#define CV_MAKETYPE(depth, cn)
Definition interface.h:85
softfloat abs(softfloat a)
Absolute value.
Definition softfloat.hpp:444
#define CV_MAT_CONT_FLAG
Definition cvdef.h:499
#define CV_ELEM_SIZE1(type)
Definition cvdef.h:508
#define CV_Error(code, msg)
Call the error handler.
Definition base.hpp:320
#define CV_MAT_CN(flags)
Definition cvdef.h:495
#define CV_NOEXCEPT
Definition cvdef.h:800
#define CV_MAT_TYPE(flags)
Definition cvdef.h:497
#define CV__DEBUG_NS_BEGIN
Definition cvdef.h:69
#define CV__DEBUG_NS_END
Definition cvdef.h:70
#define CV_Assert(expr)
Checks a condition at runtime and throws exception if it fails.
Definition base.hpp:342
#define CV_DbgAssert(expr)
Definition base.hpp:375
#define CV_ELEM_SIZE(type)
Definition cvdef.h:510
CvPoint2D32f pt[4]
Definition imgproc_c.h:571
CvArr CvPoint2D32f double M
Definition imgproc_c.h:270
CvRect r
Definition imgproc_c.h:984
float ** ranges
Definition imgproc_c.h:636
CvArr CvPoint offset
Definition imgproc_c.h:88
CvArr CvArr IplConvKernel int operation
Definition imgproc_c.h:330
CvArr CvArr * temp
Definition imgproc_c.h:329
T max(T... args)
T min(T... args)
T move(T... args)
"black box" representation of the file storage associated with a file on disk.
Definition calib3d.hpp:441
static uchar abs(uchar a)
Definition cvstd.hpp:66
T size(T... args)
float x
Definition types_c.h:978
float y
Definition types_c.h:979
@ value
Definition traits.hpp:386