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 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #ifndef __OPENCV_CORE_MATRIX_OPERATIONS_HPP__
44 #define __OPENCV_CORE_MATRIX_OPERATIONS_HPP__
49 #endif // SKIP_INCLUDES
56 //////////////////////////////// Mat ////////////////////////////////
58 inline void Mat::initEmpty()
61 dims = rows = cols = 0;
62 data = datastart = dataend = datalimit = 0;
67 inline Mat::Mat() : size(&rows)
72 inline Mat::Mat(int _rows, int _cols, int _type) : size(&rows)
75 create(_rows, _cols, _type);
78 inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s) : size(&rows)
81 create(_rows, _cols, _type);
85 inline Mat::Mat(Size _sz, int _type) : size(&rows)
88 create( _sz.height, _sz.width, _type );
91 inline Mat::Mat(Size _sz, int _type, const Scalar& _s) : size(&rows)
94 create(_sz.height, _sz.width, _type);
98 inline Mat::Mat(int _dims, const int* _sz, int _type) : size(&rows)
101 create(_dims, _sz, _type);
104 inline Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) : size(&rows)
107 create(_dims, _sz, _type);
111 inline Mat::Mat(const Mat& m)
112 : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
113 refcount(m.refcount), datastart(m.datastart), dataend(m.dataend),
114 datalimit(m.datalimit), allocator(m.allocator), size(&rows)
117 CV_XADD(refcount, 1);
120 step[0] = m.step[0]; step[1] = m.step[1];
129 inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)
130 : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_rows), cols(_cols),
131 data((uchar*)_data), refcount(0), datastart((uchar*)_data), dataend(0),
132 datalimit(0), allocator(0), size(&rows)
134 size_t esz = CV_ELEM_SIZE(_type), minstep = cols*esz;
135 if( _step == AUTO_STEP )
138 flags |= CONTINUOUS_FLAG;
142 if( rows == 1 ) _step = minstep;
143 CV_DbgAssert( _step >= minstep );
144 flags |= _step == minstep ? CONTINUOUS_FLAG : 0;
146 step[0] = _step; step[1] = esz;
147 datalimit = datastart + _step*rows;
148 dataend = datalimit - _step + minstep;
151 inline Mat::Mat(Size _sz, int _type, void* _data, size_t _step)
152 : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_sz.height), cols(_sz.width),
153 data((uchar*)_data), refcount(0), datastart((uchar*)_data), dataend(0),
154 datalimit(0), allocator(0), size(&rows)
156 size_t esz = CV_ELEM_SIZE(_type), minstep = cols*esz;
157 if( _step == AUTO_STEP )
160 flags |= CONTINUOUS_FLAG;
164 if( rows == 1 ) _step = minstep;
165 CV_DbgAssert( _step >= minstep );
166 flags |= _step == minstep ? CONTINUOUS_FLAG : 0;
168 step[0] = _step; step[1] = esz;
169 datalimit = datastart + _step*rows;
170 dataend = datalimit - _step + minstep;
174 template<typename _Tp> inline Mat::Mat(const vector<_Tp>& vec, bool copyData)
175 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
176 dims(2), rows((int)vec.size()), cols(1), data(0), refcount(0),
177 datastart(0), dataend(0), allocator(0), size(&rows)
183 step[0] = step[1] = sizeof(_Tp);
184 data = datastart = (uchar*)&vec[0];
185 datalimit = dataend = datastart + rows*step[0];
188 Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this);
192 template<typename _Tp, int n> inline Mat::Mat(const Vec<_Tp, n>& vec, bool copyData)
193 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
194 dims(2), rows(n), cols(1), data(0), refcount(0),
195 datastart(0), dataend(0), allocator(0), size(&rows)
199 step[0] = step[1] = sizeof(_Tp);
200 data = datastart = (uchar*)vec.val;
201 datalimit = dataend = datastart + rows*step[0];
204 Mat(n, 1, DataType<_Tp>::type, (void*)vec.val).copyTo(*this);
208 template<typename _Tp, int m, int n> inline Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData)
209 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
210 dims(2), rows(m), cols(n), data(0), refcount(0),
211 datastart(0), dataend(0), allocator(0), size(&rows)
215 step[0] = cols*sizeof(_Tp);
216 step[1] = sizeof(_Tp);
217 data = datastart = (uchar*)M.val;
218 datalimit = dataend = datastart + rows*step[0];
221 Mat(m, n, DataType<_Tp>::type, (uchar*)M.val).copyTo(*this);
225 template<typename _Tp> inline Mat::Mat(const Point_<_Tp>& pt, bool copyData)
226 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
227 dims(2), rows(2), cols(1), data(0), refcount(0),
228 datastart(0), dataend(0), allocator(0), size(&rows)
232 step[0] = step[1] = sizeof(_Tp);
233 data = datastart = (uchar*)&pt.x;
234 datalimit = dataend = datastart + rows*step[0];
238 create(2, 1, DataType<_Tp>::type);
239 ((_Tp*)data)[0] = pt.x;
240 ((_Tp*)data)[1] = pt.y;
245 template<typename _Tp> inline Mat::Mat(const Point3_<_Tp>& pt, bool copyData)
246 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
247 dims(2), rows(3), cols(1), data(0), refcount(0),
248 datastart(0), dataend(0), allocator(0), size(&rows)
252 step[0] = step[1] = sizeof(_Tp);
253 data = datastart = (uchar*)&pt.x;
254 datalimit = dataend = datastart + rows*step[0];
258 create(3, 1, DataType<_Tp>::type);
259 ((_Tp*)data)[0] = pt.x;
260 ((_Tp*)data)[1] = pt.y;
261 ((_Tp*)data)[2] = pt.z;
266 template<typename _Tp> inline Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer)
267 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
268 dims(0), rows(0), cols(0), data(0), refcount(0),
269 datastart(0), dataend(0), allocator(0), size(&rows)
271 *this = *commaInitializer;
277 if( step.p != step.buf )
281 inline Mat& Mat::operator = (const Mat& m)
286 CV_XADD(m.refcount, 1);
289 if( dims <= 2 && m.dims <= 2 )
300 datastart = m.datastart;
302 datalimit = m.datalimit;
303 refcount = m.refcount;
304 allocator = m.allocator;
309 inline Mat Mat::row(int y) const { return Mat(*this, Range(y, y+1), Range::all()); }
310 inline Mat Mat::col(int x) const { return Mat(*this, Range::all(), Range(x, x+1)); }
311 inline Mat Mat::rowRange(int startrow, int endrow) const
312 { return Mat(*this, Range(startrow, endrow), Range::all()); }
313 inline Mat Mat::rowRange(const Range& r) const
314 { return Mat(*this, r, Range::all()); }
315 inline Mat Mat::colRange(int startcol, int endcol) const
316 { return Mat(*this, Range::all(), Range(startcol, endcol)); }
317 inline Mat Mat::colRange(const Range& r) const
318 { return Mat(*this, Range::all(), r); }
320 inline Mat Mat::diag(const Mat& d)
322 CV_Assert( d.cols == 1 || d.rows == 1 );
323 int len = d.rows + d.cols - 1;
324 Mat m(len, len, d.type(), Scalar(0)), md = m.diag();
332 inline Mat Mat::clone() const
339 inline void Mat::assignTo( Mat& m, int _type ) const
347 inline void Mat::create(int _rows, int _cols, int _type)
350 if( dims <= 2 && rows == _rows && cols == _cols && type() == _type && data )
352 int sz[] = {_rows, _cols};
353 create(2, sz, _type);
356 inline void Mat::create(Size _sz, int _type)
358 create(_sz.height, _sz.width, _type);
361 inline void Mat::addref()
362 { if( refcount ) CV_XADD(refcount, 1); }
364 inline void Mat::release()
366 if( refcount && CV_XADD(refcount, -1) == 1 )
368 data = datastart = dataend = datalimit = 0;
369 for(int i = 0; i < dims; i++)
374 inline Mat Mat::operator()( Range _rowRange, Range _colRange ) const
376 return Mat(*this, _rowRange, _colRange);
379 inline Mat Mat::operator()( const Rect& roi ) const
380 { return Mat(*this, roi); }
382 inline Mat Mat::operator()(const Range* ranges) const
384 return Mat(*this, ranges);
387 inline Mat::operator CvMat() const
389 CV_DbgAssert(dims <= 2);
390 CvMat m = cvMat(rows, dims == 1 ? 1 : cols, type(), data);
391 m.step = (int)step[0];
392 m.type = (m.type & ~CONTINUOUS_FLAG) | (flags & CONTINUOUS_FLAG);
396 inline bool Mat::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; }
397 inline bool Mat::isSubmatrix() const { return (flags & SUBMATRIX_FLAG) != 0; }
398 inline size_t Mat::elemSize() const { return dims > 0 ? step.p[dims-1] : 0; }
399 inline size_t Mat::elemSize1() const { return CV_ELEM_SIZE1(flags); }
400 inline int Mat::type() const { return CV_MAT_TYPE(flags); }
401 inline int Mat::depth() const { return CV_MAT_DEPTH(flags); }
402 inline int Mat::channels() const { return CV_MAT_CN(flags); }
403 inline size_t Mat::step1(int i) const { return step.p[i]/elemSize1(); }
404 inline bool Mat::empty() const { return data == 0 || total() == 0; }
405 inline size_t Mat::total() const
408 return (size_t)rows*cols;
410 for( int i = 0; i < dims; i++ )
415 inline uchar* Mat::ptr(int y)
417 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
418 return data + step.p[0]*y;
421 inline const uchar* Mat::ptr(int y) const
423 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
424 return data + step.p[0]*y;
427 template<typename _Tp> inline _Tp* Mat::ptr(int y)
429 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
430 return (_Tp*)(data + step.p[0]*y);
433 template<typename _Tp> inline const _Tp* Mat::ptr(int y) const
435 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
436 return (const _Tp*)(data + step.p[0]*y);
440 inline uchar* Mat::ptr(int i0, int i1)
442 CV_DbgAssert( dims >= 2 && data &&
443 (unsigned)i0 < (unsigned)size.p[0] &&
444 (unsigned)i1 < (unsigned)size.p[1] );
445 return data + i0*step.p[0] + i1*step.p[1];
448 inline const uchar* Mat::ptr(int i0, int i1) const
450 CV_DbgAssert( dims >= 2 && data &&
451 (unsigned)i0 < (unsigned)size.p[0] &&
452 (unsigned)i1 < (unsigned)size.p[1] );
453 return data + i0*step.p[0] + i1*step.p[1];
456 template<typename _Tp> inline _Tp* Mat::ptr(int i0, int i1)
458 CV_DbgAssert( dims >= 2 && data &&
459 (unsigned)i0 < (unsigned)size.p[0] &&
460 (unsigned)i1 < (unsigned)size.p[1] );
461 return (_Tp*)(data + i0*step.p[0] + i1*step.p[1]);
464 template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1) const
466 CV_DbgAssert( dims >= 2 && data &&
467 (unsigned)i0 < (unsigned)size.p[0] &&
468 (unsigned)i1 < (unsigned)size.p[1] );
469 return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1]);
472 inline uchar* Mat::ptr(int i0, int i1, int i2)
474 CV_DbgAssert( dims >= 3 && data &&
475 (unsigned)i0 < (unsigned)size.p[0] &&
476 (unsigned)i1 < (unsigned)size.p[1] &&
477 (unsigned)i2 < (unsigned)size.p[2] );
478 return data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2];
481 inline const uchar* Mat::ptr(int i0, int i1, int i2) const
483 CV_DbgAssert( dims >= 3 && data &&
484 (unsigned)i0 < (unsigned)size.p[0] &&
485 (unsigned)i1 < (unsigned)size.p[1] &&
486 (unsigned)i2 < (unsigned)size.p[2] );
487 return data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2];
490 template<typename _Tp> inline _Tp* Mat::ptr(int i0, int i1, int i2)
492 CV_DbgAssert( dims >= 3 && data &&
493 (unsigned)i0 < (unsigned)size.p[0] &&
494 (unsigned)i1 < (unsigned)size.p[1] &&
495 (unsigned)i2 < (unsigned)size.p[2] );
496 return (_Tp*)(data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]);
499 template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1, int i2) const
501 CV_DbgAssert( dims >= 3 && data &&
502 (unsigned)i0 < (unsigned)size.p[0] &&
503 (unsigned)i1 < (unsigned)size.p[1] &&
504 (unsigned)i2 < (unsigned)size.p[2] );
505 return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]);
508 inline uchar* Mat::ptr(const int* idx)
512 CV_DbgAssert( d >= 1 && p );
513 for( i = 0; i < d; i++ )
515 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
516 p += idx[i]*step.p[i];
521 inline const uchar* Mat::ptr(const int* idx) const
525 CV_DbgAssert( d >= 1 && p );
526 for( i = 0; i < d; i++ )
528 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
529 p += idx[i]*step.p[i];
534 template<typename _Tp> inline _Tp& Mat::at(int i0, int i1)
536 CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
537 (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
538 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
539 return ((_Tp*)(data + step.p[0]*i0))[i1];
542 template<typename _Tp> inline const _Tp& Mat::at(int i0, int i1) const
544 CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
545 (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
546 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
547 return ((const _Tp*)(data + step.p[0]*i0))[i1];
550 template<typename _Tp> inline _Tp& Mat::at(Point pt)
552 CV_DbgAssert( dims <= 2 && data && (unsigned)pt.y < (unsigned)size.p[0] &&
553 (unsigned)(pt.x*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
554 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
555 return ((_Tp*)(data + step.p[0]*pt.y))[pt.x];
558 template<typename _Tp> inline const _Tp& Mat::at(Point pt) const
560 CV_DbgAssert( dims <= 2 && data && (unsigned)pt.y < (unsigned)size.p[0] &&
561 (unsigned)(pt.x*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
562 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
563 return ((const _Tp*)(data + step.p[0]*pt.y))[pt.x];
566 template<typename _Tp> inline _Tp& Mat::at(int i0)
568 CV_DbgAssert( dims <= 2 && data &&
569 (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) &&
570 elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
571 if( isContinuous() || size.p[0] == 1 )
572 return ((_Tp*)data)[i0];
574 return *(_Tp*)(data + step.p[0]*i0);
575 int i = i0/cols, j = i0 - i*cols;
576 return ((_Tp*)(data + step.p[0]*i))[j];
579 template<typename _Tp> inline const _Tp& Mat::at(int i0) const
581 CV_DbgAssert( dims <= 2 && data &&
582 (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) &&
583 elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
584 if( isContinuous() || size.p[0] == 1 )
585 return ((const _Tp*)data)[i0];
587 return *(const _Tp*)(data + step.p[0]*i0);
588 int i = i0/cols, j = i0 - i*cols;
589 return ((const _Tp*)(data + step.p[0]*i))[j];
592 template<typename _Tp> inline _Tp& Mat::at(int i0, int i1, int i2)
594 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
595 return *(_Tp*)ptr(i0, i1, i2);
597 template<typename _Tp> inline const _Tp& Mat::at(int i0, int i1, int i2) const
599 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
600 return *(const _Tp*)ptr(i0, i1, i2);
602 template<typename _Tp> inline _Tp& Mat::at(const int* idx)
604 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
605 return *(_Tp*)ptr(idx);
607 template<typename _Tp> inline const _Tp& Mat::at(const int* idx) const
609 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
610 return *(const _Tp*)ptr(idx);
612 template<typename _Tp, int n> _Tp& Mat::at(const Vec<int, n>& idx)
614 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
615 return *(_Tp*)ptr(idx.val);
617 template<typename _Tp, int n> inline const _Tp& Mat::at(const Vec<int, n>& idx) const
619 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
620 return *(const _Tp*)ptr(idx.val);
624 template<typename _Tp> inline MatConstIterator_<_Tp> Mat::begin() const
626 CV_DbgAssert( elemSize() == sizeof(_Tp) );
627 return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
630 template<typename _Tp> inline MatConstIterator_<_Tp> Mat::end() const
632 CV_DbgAssert( elemSize() == sizeof(_Tp) );
633 MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
638 template<typename _Tp> inline MatIterator_<_Tp> Mat::begin()
640 CV_DbgAssert( elemSize() == sizeof(_Tp) );
641 return MatIterator_<_Tp>((Mat_<_Tp>*)this);
644 template<typename _Tp> inline MatIterator_<_Tp> Mat::end()
646 CV_DbgAssert( elemSize() == sizeof(_Tp) );
647 MatIterator_<_Tp> it((Mat_<_Tp>*)this);
652 template<typename _Tp> inline Mat::operator vector<_Tp>() const
659 template<typename _Tp, int n> inline Mat::operator Vec<_Tp, n>() const
661 CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) &&
662 rows + cols - 1 == n && channels() == 1 );
664 if( isContinuous() && type() == DataType<_Tp>::type )
665 return Vec<_Tp, n>((_Tp*)data);
666 Vec<_Tp, n> v; Mat tmp(rows, cols, DataType<_Tp>::type, v.val);
667 convertTo(tmp, tmp.type());
671 template<typename _Tp, int m, int n> inline Mat::operator Matx<_Tp, m, n>() const
673 CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 );
675 if( isContinuous() && type() == DataType<_Tp>::type )
676 return Matx<_Tp, m, n>((_Tp*)data);
677 Matx<_Tp, m, n> mtx; Mat tmp(rows, cols, DataType<_Tp>::type, mtx.val);
678 convertTo(tmp, tmp.type());
683 template<typename _Tp> inline void Mat::push_back(const _Tp& elem)
687 *this = Mat(1, 1, DataType<_Tp>::type, (void*)&elem).clone();
690 CV_Assert(DataType<_Tp>::type == type() && cols == 1
691 /* && dims == 2 (cols == 1 implies dims == 2) */);
692 uchar* tmp = dataend + step[0];
693 if( !isSubmatrix() && isContinuous() && tmp <= datalimit )
695 *(_Tp*)(data + (size.p[0]++)*step.p[0]) = elem;
702 template<typename _Tp> inline void Mat::push_back(const Mat_<_Tp>& m)
704 push_back((const Mat&)m);
707 inline Mat::MSize::MSize(int* _p) : p(_p) {}
708 inline Size Mat::MSize::operator()() const
710 CV_DbgAssert(p[-1] <= 2);
711 return Size(p[1], p[0]);
713 inline const int& Mat::MSize::operator[](int i) const { return p[i]; }
714 inline int& Mat::MSize::operator[](int i) { return p[i]; }
715 inline Mat::MSize::operator const int*() const { return p; }
717 inline bool Mat::MSize::operator == (const MSize& sz) const
719 int d = p[-1], dsz = sz.p[-1];
723 return p[0] == sz.p[0] && p[1] == sz.p[1];
725 for( int i = 0; i < d; i++ )
726 if( p[i] != sz.p[i] )
731 inline bool Mat::MSize::operator != (const MSize& sz) const
733 return !(*this == sz);
736 inline Mat::MStep::MStep() { p = buf; p[0] = p[1] = 0; }
737 inline Mat::MStep::MStep(size_t s) { p = buf; p[0] = s; p[1] = 0; }
738 inline const size_t& Mat::MStep::operator[](int i) const { return p[i]; }
739 inline size_t& Mat::MStep::operator[](int i) { return p[i]; }
740 inline Mat::MStep::operator size_t() const
742 CV_DbgAssert( p == buf );
745 inline Mat::MStep& Mat::MStep::operator = (size_t s)
747 CV_DbgAssert( p == buf );
752 static inline Mat cvarrToMatND(const CvArr* arr, bool copyData=false, int coiMode=0)
754 return cvarrToMat(arr, copyData, true, coiMode);
757 ///////////////////////////////////////////// SVD //////////////////////////////////////////////////////
760 inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
761 inline void SVD::solveZ( InputArray m, OutputArray _dst )
763 Mat mtx = m.getMat();
764 SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
765 _dst.create(svd.vt.cols, 1, svd.vt.type());
766 Mat dst = _dst.getMat();
767 svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
770 template<typename _Tp, int m, int n, int nm> inline void
771 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
773 assert( nm == MIN(m, n));
774 Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
775 SVD::compute(_a, _w, _u, _vt);
776 CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
779 template<typename _Tp, int m, int n, int nm> inline void
780 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
782 assert( nm == MIN(m, n));
783 Mat _a(a, false), _w(w, false);
784 SVD::compute(_a, _w);
785 CV_Assert(_w.data == (uchar*)&w.val[0]);
788 template<typename _Tp, int m, int n, int nm, int nb> inline void
789 SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
790 const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
791 Matx<_Tp, n, nb>& dst )
793 assert( nm == MIN(m, n));
794 Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
795 SVD::backSubst(_w, _u, _vt, _rhs, _dst);
796 CV_Assert(_dst.data == (uchar*)&dst.val[0]);
799 ///////////////////////////////// Mat_<_Tp> ////////////////////////////////////
801 template<typename _Tp> inline Mat_<_Tp>::Mat_()
802 : Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; }
804 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols)
805 : Mat(_rows, _cols, DataType<_Tp>::type) {}
807 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
808 : Mat(_rows, _cols, DataType<_Tp>::type) { *this = value; }
810 template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _sz)
811 : Mat(_sz.height, _sz.width, DataType<_Tp>::type) {}
813 template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
814 : Mat(_sz.height, _sz.width, DataType<_Tp>::type) { *this = value; }
816 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _dims, const int* _sz)
817 : Mat(_dims, _sz, DataType<_Tp>::type) {}
819 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s)
820 : Mat(_dims, _sz, DataType<_Tp>::type, Scalar(_s)) {}
822 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges)
825 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat& m)
826 : Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; *this = m; }
828 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m)
831 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps)
832 : Mat(_rows, _cols, DataType<_Tp>::type, _data, steps) {}
834 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Range& _rowRange, const Range& _colRange)
835 : Mat(m, _rowRange, _colRange) {}
837 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
840 template<typename _Tp> template<int n> inline
841 Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData)
842 : Mat(n/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&vec)
844 CV_Assert(n%DataType<_Tp>::channels == 0);
849 template<typename _Tp> template<int m, int n> inline
850 Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type,m,n>& M, bool copyData)
851 : Mat(m, n/DataType<_Tp>::channels, DataType<_Tp>::type, (void*)&M)
853 CV_Assert(n % DataType<_Tp>::channels == 0);
858 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
859 : Mat(2/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt)
861 CV_Assert(2 % DataType<_Tp>::channels == 0);
866 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
867 : Mat(3/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt)
869 CV_Assert(3 % DataType<_Tp>::channels == 0);
874 template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer)
875 : Mat(commaInitializer) {}
877 template<typename _Tp> inline Mat_<_Tp>::Mat_(const vector<_Tp>& vec, bool copyData)
878 : Mat(vec, copyData) {}
880 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
882 if( DataType<_Tp>::type == m.type() )
887 if( DataType<_Tp>::depth == m.depth() )
889 return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0));
891 CV_DbgAssert(DataType<_Tp>::channels == m.channels());
892 m.convertTo(*this, type());
896 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
902 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
904 typedef typename DataType<_Tp>::vec_type VT;
905 Mat::operator=(Scalar((const VT&)s));
909 template<typename _Tp> inline void Mat_<_Tp>::create(int _rows, int _cols)
911 Mat::create(_rows, _cols, DataType<_Tp>::type);
914 template<typename _Tp> inline void Mat_<_Tp>::create(Size _sz)
916 Mat::create(_sz, DataType<_Tp>::type);
919 template<typename _Tp> inline void Mat_<_Tp>::create(int _dims, const int* _sz)
921 Mat::create(_dims, _sz, DataType<_Tp>::type);
925 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
926 { return Mat_<_Tp>(Mat::cross(m)); }
928 template<typename _Tp> template<typename T2> inline Mat_<_Tp>::operator Mat_<T2>() const
929 { return Mat_<T2>(*this); }
931 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::row(int y) const
932 { return Mat_(*this, Range(y, y+1), Range::all()); }
933 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::col(int x) const
934 { return Mat_(*this, Range::all(), Range(x, x+1)); }
935 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::diag(int d) const
936 { return Mat_(Mat::diag(d)); }
937 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::clone() const
938 { return Mat_(Mat::clone()); }
940 template<typename _Tp> inline size_t Mat_<_Tp>::elemSize() const
942 CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
946 template<typename _Tp> inline size_t Mat_<_Tp>::elemSize1() const
948 CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp)/DataType<_Tp>::channels );
949 return sizeof(_Tp)/DataType<_Tp>::channels;
951 template<typename _Tp> inline int Mat_<_Tp>::type() const
953 CV_DbgAssert( Mat::type() == DataType<_Tp>::type );
954 return DataType<_Tp>::type;
956 template<typename _Tp> inline int Mat_<_Tp>::depth() const
958 CV_DbgAssert( Mat::depth() == DataType<_Tp>::depth );
959 return DataType<_Tp>::depth;
961 template<typename _Tp> inline int Mat_<_Tp>::channels() const
963 CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
964 return DataType<_Tp>::channels;
966 template<typename _Tp> inline size_t Mat_<_Tp>::stepT(int i) const { return step.p[i]/elemSize(); }
967 template<typename _Tp> inline size_t Mat_<_Tp>::step1(int i) const { return step.p[i]/elemSize1(); }
969 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
970 { return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright)); }
972 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range& _rowRange, const Range& _colRange ) const
973 { return Mat_<_Tp>(*this, _rowRange, _colRange); }
975 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
976 { return Mat_<_Tp>(*this, roi); }
978 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const
979 { return Mat_<_Tp>(*this, ranges); }
981 template<typename _Tp> inline _Tp* Mat_<_Tp>::operator [](int y)
982 { return (_Tp*)ptr(y); }
983 template<typename _Tp> inline const _Tp* Mat_<_Tp>::operator [](int y) const
984 { return (const _Tp*)ptr(y); }
986 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0, int i1)
988 CV_DbgAssert( dims <= 2 && data &&
989 (unsigned)i0 < (unsigned)size.p[0] &&
990 (unsigned)i1 < (unsigned)size.p[1] &&
991 type() == DataType<_Tp>::type );
992 return ((_Tp*)(data + step.p[0]*i0))[i1];
995 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const
997 CV_DbgAssert( dims <= 2 && data &&
998 (unsigned)i0 < (unsigned)size.p[0] &&
999 (unsigned)i1 < (unsigned)size.p[1] &&
1000 type() == DataType<_Tp>::type );
1001 return ((const _Tp*)(data + step.p[0]*i0))[i1];
1004 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(Point pt)
1006 CV_DbgAssert( dims <= 2 && data &&
1007 (unsigned)pt.y < (unsigned)size.p[0] &&
1008 (unsigned)pt.x < (unsigned)size.p[1] &&
1009 type() == DataType<_Tp>::type );
1010 return ((_Tp*)(data + step.p[0]*pt.y))[pt.x];
1013 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(Point pt) const
1015 CV_DbgAssert( dims <= 2 && data &&
1016 (unsigned)pt.y < (unsigned)size.p[0] &&
1017 (unsigned)pt.x < (unsigned)size.p[1] &&
1018 type() == DataType<_Tp>::type );
1019 return ((const _Tp*)(data + step.p[0]*pt.y))[pt.x];
1022 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(const int* idx)
1024 return Mat::at<_Tp>(idx);
1027 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(const int* idx) const
1029 return Mat::at<_Tp>(idx);
1032 template<typename _Tp> template<int n> inline _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx)
1034 return Mat::at<_Tp>(idx);
1037 template<typename _Tp> template<int n> inline const _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx) const
1039 return Mat::at<_Tp>(idx);
1042 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0)
1044 return this->at<_Tp>(i0);
1047 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0) const
1049 return this->at<_Tp>(i0);
1052 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2)
1054 return this->at<_Tp>(i0, i1, i2);
1057 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const
1059 return this->at<_Tp>(i0, i1, i2);
1063 template<typename _Tp> inline Mat_<_Tp>::operator vector<_Tp>() const
1070 template<typename _Tp> template<int n> inline Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const
1072 CV_Assert(n % DataType<_Tp>::channels == 0);
1073 return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>();
1076 template<typename _Tp> template<int m, int n> inline Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const
1078 CV_Assert(n % DataType<_Tp>::channels == 0);
1080 Matx<typename DataType<_Tp>::channel_type, m, n> res = this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>();
1084 template<typename T1, typename T2, typename Op> inline void
1085 process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op )
1087 int y, x, rows = m1.rows, cols = m1.cols;
1089 CV_DbgAssert( m1.size() == m2.size() );
1091 for( y = 0; y < rows; y++ )
1093 const T1* src = m1[y];
1096 for( x = 0; x < cols; x++ )
1097 dst[x] = op(src[x]);
1101 template<typename T1, typename T2, typename T3, typename Op> inline void
1102 process( const Mat_<T1>& m1, const Mat_<T2>& m2, Mat_<T3>& m3, Op op )
1104 int y, x, rows = m1.rows, cols = m1.cols;
1106 CV_DbgAssert( m1.size() == m2.size() );
1108 for( y = 0; y < rows; y++ )
1110 const T1* src1 = m1[y];
1111 const T2* src2 = m2[y];
1114 for( x = 0; x < cols; x++ )
1115 dst[x] = op( src1[x], src2[x] );
1120 /////////////////////////////// Input/Output Arrays /////////////////////////////////
1122 template<typename _Tp> inline _InputArray::_InputArray(const vector<_Tp>& vec)
1123 : flags(FIXED_TYPE + STD_VECTOR + DataType<_Tp>::type), obj((void*)&vec) {}
1125 template<typename _Tp> inline _InputArray::_InputArray(const vector<vector<_Tp> >& vec)
1126 : flags(FIXED_TYPE + STD_VECTOR_VECTOR + DataType<_Tp>::type), obj((void*)&vec) {}
1128 template<typename _Tp> inline _InputArray::_InputArray(const vector<Mat_<_Tp> >& vec)
1129 : flags(FIXED_TYPE + STD_VECTOR_MAT + DataType<_Tp>::type), obj((void*)&vec) {}
1131 template<typename _Tp, int m, int n> inline _InputArray::_InputArray(const Matx<_Tp, m, n>& mtx)
1132 : flags(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type), obj((void*)&mtx), sz(n, m) {}
1134 template<typename _Tp> inline _InputArray::_InputArray(const _Tp* vec, int n)
1135 : flags(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type), obj((void*)vec), sz(n, 1) {}
1137 inline _InputArray::_InputArray(const Scalar& s)
1138 : flags(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F), obj((void*)&s), sz(1, 4) {}
1140 template<typename _Tp> inline _InputArray::_InputArray(const Mat_<_Tp>& m)
1141 : flags(FIXED_TYPE + MAT + DataType<_Tp>::type), obj((void*)&m) {}
1143 template<typename _Tp> inline _OutputArray::_OutputArray(vector<_Tp>& vec)
1144 : _InputArray(vec) {}
1145 template<typename _Tp> inline _OutputArray::_OutputArray(vector<vector<_Tp> >& vec)
1146 : _InputArray(vec) {}
1147 template<typename _Tp> inline _OutputArray::_OutputArray(vector<Mat_<_Tp> >& vec)
1148 : _InputArray(vec) {}
1149 template<typename _Tp> inline _OutputArray::_OutputArray(Mat_<_Tp>& m)
1151 template<typename _Tp, int m, int n> inline _OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx)
1152 : _InputArray(mtx) {}
1153 template<typename _Tp> inline _OutputArray::_OutputArray(_Tp* vec, int n)
1154 : _InputArray(vec, n) {}
1156 template<typename _Tp> inline _OutputArray::_OutputArray(const vector<_Tp>& vec)
1157 : _InputArray(vec) {flags |= FIXED_SIZE;}
1158 template<typename _Tp> inline _OutputArray::_OutputArray(const vector<vector<_Tp> >& vec)
1159 : _InputArray(vec) {flags |= FIXED_SIZE;}
1160 template<typename _Tp> inline _OutputArray::_OutputArray(const vector<Mat_<_Tp> >& vec)
1161 : _InputArray(vec) {flags |= FIXED_SIZE;}
1163 template<typename _Tp> inline _OutputArray::_OutputArray(const Mat_<_Tp>& m)
1164 : _InputArray(m) {flags |= FIXED_SIZE;}
1165 template<typename _Tp, int m, int n> inline _OutputArray::_OutputArray(const Matx<_Tp, m, n>& mtx)
1166 : _InputArray(mtx) {}
1167 template<typename _Tp> inline _OutputArray::_OutputArray(const _Tp* vec, int n)
1168 : _InputArray(vec, n) {}
1170 //////////////////////////////////// Matrix Expressions /////////////////////////////////////////
1172 class CV_EXPORTS MatOp
1176 virtual ~MatOp() {};
1178 virtual bool elementWise(const MatExpr& expr) const;
1179 virtual void assign(const MatExpr& expr, Mat& m, int type=-1) const = 0;
1180 virtual void roi(const MatExpr& expr, const Range& rowRange,
1181 const Range& colRange, MatExpr& res) const;
1182 virtual void diag(const MatExpr& expr, int d, MatExpr& res) const;
1183 virtual void augAssignAdd(const MatExpr& expr, Mat& m) const;
1184 virtual void augAssignSubtract(const MatExpr& expr, Mat& m) const;
1185 virtual void augAssignMultiply(const MatExpr& expr, Mat& m) const;
1186 virtual void augAssignDivide(const MatExpr& expr, Mat& m) const;
1187 virtual void augAssignAnd(const MatExpr& expr, Mat& m) const;
1188 virtual void augAssignOr(const MatExpr& expr, Mat& m) const;
1189 virtual void augAssignXor(const MatExpr& expr, Mat& m) const;
1191 virtual void add(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
1192 virtual void add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const;
1194 virtual void subtract(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
1195 virtual void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const;
1197 virtual void multiply(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const;
1198 virtual void multiply(const MatExpr& expr1, double s, MatExpr& res) const;
1200 virtual void divide(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const;
1201 virtual void divide(double s, const MatExpr& expr, MatExpr& res) const;
1203 virtual void abs(const MatExpr& expr, MatExpr& res) const;
1205 virtual void transpose(const MatExpr& expr, MatExpr& res) const;
1206 virtual void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
1207 virtual void invert(const MatExpr& expr, int method, MatExpr& res) const;
1209 virtual Size size(const MatExpr& expr) const;
1210 virtual int type(const MatExpr& expr) const;
1214 class CV_EXPORTS MatExpr
1217 MatExpr() : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s(Scalar()) {}
1218 MatExpr(const MatOp* _op, int _flags, const Mat& _a=Mat(), const Mat& _b=Mat(),
1219 const Mat& _c=Mat(), double _alpha=1, double _beta=1, const Scalar& _s=Scalar())
1220 : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s) {}
1221 explicit MatExpr(const Mat& m);
1222 operator Mat() const
1225 op->assign(*this, m);
1229 template<typename _Tp> operator Mat_<_Tp>() const
1232 op->assign(*this, m, DataType<_Tp>::type);
1236 MatExpr row(int y) const;
1237 MatExpr col(int x) const;
1238 MatExpr diag(int d=0) const;
1239 MatExpr operator()( const Range& rowRange, const Range& colRange ) const;
1240 MatExpr operator()( const Rect& roi ) const;
1242 Mat cross(const Mat& m) const;
1243 double dot(const Mat& m) const;
1246 MatExpr inv(int method = DECOMP_LU) const;
1247 MatExpr mul(const MatExpr& e, double scale=1) const;
1248 MatExpr mul(const Mat& m, double scale=1) const;
1262 CV_EXPORTS MatExpr operator + (const Mat& a, const Mat& b);
1263 CV_EXPORTS MatExpr operator + (const Mat& a, const Scalar& s);
1264 CV_EXPORTS MatExpr operator + (const Scalar& s, const Mat& a);
1265 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Mat& m);
1266 CV_EXPORTS MatExpr operator + (const Mat& m, const MatExpr& e);
1267 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Scalar& s);
1268 CV_EXPORTS MatExpr operator + (const Scalar& s, const MatExpr& e);
1269 CV_EXPORTS MatExpr operator + (const MatExpr& e1, const MatExpr& e2);
1271 CV_EXPORTS MatExpr operator - (const Mat& a, const Mat& b);
1272 CV_EXPORTS MatExpr operator - (const Mat& a, const Scalar& s);
1273 CV_EXPORTS MatExpr operator - (const Scalar& s, const Mat& a);
1274 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Mat& m);
1275 CV_EXPORTS MatExpr operator - (const Mat& m, const MatExpr& e);
1276 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Scalar& s);
1277 CV_EXPORTS MatExpr operator - (const Scalar& s, const MatExpr& e);
1278 CV_EXPORTS MatExpr operator - (const MatExpr& e1, const MatExpr& e2);
1280 CV_EXPORTS MatExpr operator - (const Mat& m);
1281 CV_EXPORTS MatExpr operator - (const MatExpr& e);
1283 CV_EXPORTS MatExpr operator * (const Mat& a, const Mat& b);
1284 CV_EXPORTS MatExpr operator * (const Mat& a, double s);
1285 CV_EXPORTS MatExpr operator * (double s, const Mat& a);
1286 CV_EXPORTS MatExpr operator * (const MatExpr& e, const Mat& m);
1287 CV_EXPORTS MatExpr operator * (const Mat& m, const MatExpr& e);
1288 CV_EXPORTS MatExpr operator * (const MatExpr& e, double s);
1289 CV_EXPORTS MatExpr operator * (double s, const MatExpr& e);
1290 CV_EXPORTS MatExpr operator * (const MatExpr& e1, const MatExpr& e2);
1292 CV_EXPORTS MatExpr operator / (const Mat& a, const Mat& b);
1293 CV_EXPORTS MatExpr operator / (const Mat& a, double s);
1294 CV_EXPORTS MatExpr operator / (double s, const Mat& a);
1295 CV_EXPORTS MatExpr operator / (const MatExpr& e, const Mat& m);
1296 CV_EXPORTS MatExpr operator / (const Mat& m, const MatExpr& e);
1297 CV_EXPORTS MatExpr operator / (const MatExpr& e, double s);
1298 CV_EXPORTS MatExpr operator / (double s, const MatExpr& e);
1299 CV_EXPORTS MatExpr operator / (const MatExpr& e1, const MatExpr& e2);
1301 CV_EXPORTS MatExpr operator < (const Mat& a, const Mat& b);
1302 CV_EXPORTS MatExpr operator < (const Mat& a, double s);
1303 CV_EXPORTS MatExpr operator < (double s, const Mat& a);
1305 CV_EXPORTS MatExpr operator <= (const Mat& a, const Mat& b);
1306 CV_EXPORTS MatExpr operator <= (const Mat& a, double s);
1307 CV_EXPORTS MatExpr operator <= (double s, const Mat& a);
1309 CV_EXPORTS MatExpr operator == (const Mat& a, const Mat& b);
1310 CV_EXPORTS MatExpr operator == (const Mat& a, double s);
1311 CV_EXPORTS MatExpr operator == (double s, const Mat& a);
1313 CV_EXPORTS MatExpr operator != (const Mat& a, const Mat& b);
1314 CV_EXPORTS MatExpr operator != (const Mat& a, double s);
1315 CV_EXPORTS MatExpr operator != (double s, const Mat& a);
1317 CV_EXPORTS MatExpr operator >= (const Mat& a, const Mat& b);
1318 CV_EXPORTS MatExpr operator >= (const Mat& a, double s);
1319 CV_EXPORTS MatExpr operator >= (double s, const Mat& a);
1321 CV_EXPORTS MatExpr operator > (const Mat& a, const Mat& b);
1322 CV_EXPORTS MatExpr operator > (const Mat& a, double s);
1323 CV_EXPORTS MatExpr operator > (double s, const Mat& a);
1325 CV_EXPORTS MatExpr min(const Mat& a, const Mat& b);
1326 CV_EXPORTS MatExpr min(const Mat& a, double s);
1327 CV_EXPORTS MatExpr min(double s, const Mat& a);
1329 CV_EXPORTS MatExpr max(const Mat& a, const Mat& b);
1330 CV_EXPORTS MatExpr max(const Mat& a, double s);
1331 CV_EXPORTS MatExpr max(double s, const Mat& a);
1333 template<typename _Tp> static inline MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1335 return cv::min((const Mat&)a, (const Mat&)b);
1338 template<typename _Tp> static inline MatExpr min(const Mat_<_Tp>& a, double s)
1340 return cv::min((const Mat&)a, s);
1343 template<typename _Tp> static inline MatExpr min(double s, const Mat_<_Tp>& a)
1345 return cv::min((const Mat&)a, s);
1348 template<typename _Tp> static inline MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1350 return cv::max((const Mat&)a, (const Mat&)b);
1353 template<typename _Tp> static inline MatExpr max(const Mat_<_Tp>& a, double s)
1355 return cv::max((const Mat&)a, s);
1358 template<typename _Tp> static inline MatExpr max(double s, const Mat_<_Tp>& a)
1360 return cv::max((const Mat&)a, s);
1363 template<typename _Tp> static inline void min(const Mat_<_Tp>& a, const Mat_<_Tp>& b, Mat_<_Tp>& c)
1365 cv::min((const Mat&)a, (const Mat&)b, (Mat&)c);
1368 template<typename _Tp> static inline void min(const Mat_<_Tp>& a, double s, Mat_<_Tp>& c)
1370 cv::min((const Mat&)a, s, (Mat&)c);
1373 template<typename _Tp> static inline void min(double s, const Mat_<_Tp>& a, Mat_<_Tp>& c)
1375 cv::min((const Mat&)a, s, (Mat&)c);
1378 template<typename _Tp> static inline void max(const Mat_<_Tp>& a, const Mat_<_Tp>& b, Mat_<_Tp>& c)
1380 cv::max((const Mat&)a, (const Mat&)b, (Mat&)c);
1383 template<typename _Tp> static inline void max(const Mat_<_Tp>& a, double s, Mat_<_Tp>& c)
1385 cv::max((const Mat&)a, s, (Mat&)c);
1388 template<typename _Tp> static inline void max(double s, const Mat_<_Tp>& a, Mat_<_Tp>& c)
1390 cv::max((const Mat&)a, s, (Mat&)c);
1394 CV_EXPORTS MatExpr operator & (const Mat& a, const Mat& b);
1395 CV_EXPORTS MatExpr operator & (const Mat& a, const Scalar& s);
1396 CV_EXPORTS MatExpr operator & (const Scalar& s, const Mat& a);
1398 CV_EXPORTS MatExpr operator | (const Mat& a, const Mat& b);
1399 CV_EXPORTS MatExpr operator | (const Mat& a, const Scalar& s);
1400 CV_EXPORTS MatExpr operator | (const Scalar& s, const Mat& a);
1402 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Mat& b);
1403 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Scalar& s);
1404 CV_EXPORTS MatExpr operator ^ (const Scalar& s, const Mat& a);
1406 CV_EXPORTS MatExpr operator ~(const Mat& m);
1408 CV_EXPORTS MatExpr abs(const Mat& m);
1409 CV_EXPORTS MatExpr abs(const MatExpr& e);
1411 template<typename _Tp> static inline MatExpr abs(const Mat_<_Tp>& m)
1413 return cv::abs((const Mat&)m);
1416 ////////////////////////////// Augmenting algebraic operations //////////////////////////////////
1418 inline Mat& Mat::operator = (const MatExpr& e)
1420 e.op->assign(e, *this);
1424 template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatExpr& e)
1426 e.op->assign(e, *this, DataType<_Tp>::type);
1429 template<typename _Tp> Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e)
1431 e.op->assign(e, *this, DataType<_Tp>::type);
1435 static inline Mat& operator += (const Mat& a, const Mat& b)
1441 static inline Mat& operator += (const Mat& a, const Scalar& s)
1447 template<typename _Tp> static inline
1448 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1451 return (Mat_<_Tp>&)a;
1454 template<typename _Tp> static inline
1455 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Scalar& s)
1458 return (Mat_<_Tp>&)a;
1461 static inline Mat& operator += (const Mat& a, const MatExpr& b)
1463 b.op->augAssignAdd(b, (Mat&)a);
1467 template<typename _Tp> static inline
1468 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b)
1470 b.op->augAssignAdd(b, (Mat&)a);
1471 return (Mat_<_Tp>&)a;
1474 static inline Mat& operator -= (const Mat& a, const Mat& b)
1476 subtract(a, b, (Mat&)a);
1480 static inline Mat& operator -= (const Mat& a, const Scalar& s)
1482 subtract(a, s, (Mat&)a);
1486 template<typename _Tp> static inline
1487 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1489 subtract(a, b, (Mat&)a);
1490 return (Mat_<_Tp>&)a;
1493 template<typename _Tp> static inline
1494 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Scalar& s)
1496 subtract(a, s, (Mat&)a);
1497 return (Mat_<_Tp>&)a;
1500 static inline Mat& operator -= (const Mat& a, const MatExpr& b)
1502 b.op->augAssignSubtract(b, (Mat&)a);
1506 template<typename _Tp> static inline
1507 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b)
1509 b.op->augAssignSubtract(b, (Mat&)a);
1510 return (Mat_<_Tp>&)a;
1513 static inline Mat& operator *= (const Mat& a, const Mat& b)
1515 gemm(a, b, 1, Mat(), 0, (Mat&)a, 0);
1519 static inline Mat& operator *= (const Mat& a, double s)
1521 a.convertTo((Mat&)a, -1, s);
1525 template<typename _Tp> static inline
1526 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1528 gemm(a, b, 1, Mat(), 0, (Mat&)a, 0);
1529 return (Mat_<_Tp>&)a;
1532 template<typename _Tp> static inline
1533 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, double s)
1535 a.convertTo((Mat&)a, -1, s);
1536 return (Mat_<_Tp>&)a;
1539 static inline Mat& operator *= (const Mat& a, const MatExpr& b)
1541 b.op->augAssignMultiply(b, (Mat&)a);
1545 template<typename _Tp> static inline
1546 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b)
1548 b.op->augAssignMultiply(b, (Mat&)a);
1549 return (Mat_<_Tp>&)a;
1552 static inline Mat& operator /= (const Mat& a, const Mat& b)
1554 divide(a, b, (Mat&)a);
1558 static inline Mat& operator /= (const Mat& a, double s)
1560 a.convertTo((Mat&)a, -1, 1./s);
1564 template<typename _Tp> static inline
1565 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1567 divide(a, b, (Mat&)a);
1568 return (Mat_<_Tp>&)a;
1571 template<typename _Tp> static inline
1572 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, double s)
1574 a.convertTo((Mat&)a, -1, 1./s);
1575 return (Mat_<_Tp>&)a;
1578 static inline Mat& operator /= (const Mat& a, const MatExpr& b)
1580 b.op->augAssignDivide(b, (Mat&)a);
1584 template<typename _Tp> static inline
1585 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b)
1587 b.op->augAssignDivide(b, (Mat&)a);
1588 return (Mat_<_Tp>&)a;
1591 ////////////////////////////// Logical operations ///////////////////////////////
1593 static inline Mat& operator &= (const Mat& a, const Mat& b)
1595 bitwise_and(a, b, (Mat&)a);
1599 static inline Mat& operator &= (const Mat& a, const Scalar& s)
1601 bitwise_and(a, s, (Mat&)a);
1605 template<typename _Tp> static inline Mat_<_Tp>&
1606 operator &= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1608 bitwise_and(a, b, (Mat&)a);
1609 return (Mat_<_Tp>&)a;
1612 template<typename _Tp> static inline Mat_<_Tp>&
1613 operator &= (const Mat_<_Tp>& a, const Scalar& s)
1615 bitwise_and(a, s, (Mat&)a);
1616 return (Mat_<_Tp>&)a;
1619 static inline Mat& operator |= (const Mat& a, const Mat& b)
1621 bitwise_or(a, b, (Mat&)a);
1625 static inline Mat& operator |= (const Mat& a, const Scalar& s)
1627 bitwise_or(a, s, (Mat&)a);
1631 template<typename _Tp> static inline Mat_<_Tp>&
1632 operator |= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1634 bitwise_or(a, b, (Mat&)a);
1635 return (Mat_<_Tp>&)a;
1638 template<typename _Tp> static inline Mat_<_Tp>&
1639 operator |= (const Mat_<_Tp>& a, const Scalar& s)
1641 bitwise_or(a, s, (Mat&)a);
1642 return (Mat_<_Tp>&)a;
1645 static inline Mat& operator ^= (const Mat& a, const Mat& b)
1647 bitwise_xor(a, b, (Mat&)a);
1651 static inline Mat& operator ^= (const Mat& a, const Scalar& s)
1653 bitwise_xor(a, s, (Mat&)a);
1657 template<typename _Tp> static inline Mat_<_Tp>&
1658 operator ^= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1660 bitwise_xor(a, b, (Mat&)a);
1661 return (Mat_<_Tp>&)a;
1664 template<typename _Tp> static inline Mat_<_Tp>&
1665 operator ^= (const Mat_<_Tp>& a, const Scalar& s)
1667 bitwise_xor(a, s, (Mat&)a);
1668 return (Mat_<_Tp>&)a;
1671 /////////////////////////////// Miscellaneous operations //////////////////////////////
1673 template<typename _Tp> void split(const Mat& src, vector<Mat_<_Tp> >& mv)
1674 { split(src, (vector<Mat>&)mv ); }
1676 //////////////////////////////////////////////////////////////
1678 template<typename _Tp> inline MatExpr Mat_<_Tp>::zeros(int rows, int cols)
1680 return Mat::zeros(rows, cols, DataType<_Tp>::type);
1683 template<typename _Tp> inline MatExpr Mat_<_Tp>::zeros(Size sz)
1685 return Mat::zeros(sz, DataType<_Tp>::type);
1688 template<typename _Tp> inline MatExpr Mat_<_Tp>::ones(int rows, int cols)
1690 return Mat::ones(rows, cols, DataType<_Tp>::type);
1693 template<typename _Tp> inline MatExpr Mat_<_Tp>::ones(Size sz)
1695 return Mat::ones(sz, DataType<_Tp>::type);
1698 template<typename _Tp> inline MatExpr Mat_<_Tp>::eye(int rows, int cols)
1700 return Mat::eye(rows, cols, DataType<_Tp>::type);
1703 template<typename _Tp> inline MatExpr Mat_<_Tp>::eye(Size sz)
1705 return Mat::eye(sz, DataType<_Tp>::type);
1708 //////////////////////////////// Iterators & Comma initializers //////////////////////////////////
1710 inline MatConstIterator::MatConstIterator()
1711 : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0) {}
1713 inline MatConstIterator::MatConstIterator(const Mat* _m)
1714 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
1716 if( m && m->isContinuous() )
1718 sliceStart = m->data;
1719 sliceEnd = sliceStart + m->total()*elemSize;
1721 seek((const int*)0);
1724 inline MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
1725 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
1727 CV_Assert(m && m->dims <= 2);
1728 if( m->isContinuous() )
1730 sliceStart = m->data;
1731 sliceEnd = sliceStart + m->total()*elemSize;
1733 int idx[]={_row, _col};
1737 inline MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
1738 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
1740 CV_Assert(m && m->dims <= 2);
1741 if( m->isContinuous() )
1743 sliceStart = m->data;
1744 sliceEnd = sliceStart + m->total()*elemSize;
1746 int idx[]={_pt.y, _pt.x};
1750 inline MatConstIterator::MatConstIterator(const MatConstIterator& it)
1751 : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd)
1754 inline MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it )
1756 m = it.m; elemSize = it.elemSize; ptr = it.ptr;
1757 sliceStart = it.sliceStart; sliceEnd = it.sliceEnd;
1761 inline uchar* MatConstIterator::operator *() const { return ptr; }
1763 inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs)
1765 if( !m || ofs == 0 )
1767 ptrdiff_t ofsb = ofs*elemSize;
1769 if( ptr < sliceStart || sliceEnd <= ptr )
1777 inline MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs)
1778 { return (*this += -ofs); }
1780 inline MatConstIterator& MatConstIterator::operator --()
1782 if( m && (ptr -= elemSize) < sliceStart )
1790 inline MatConstIterator MatConstIterator::operator --(int)
1792 MatConstIterator b = *this;
1797 inline MatConstIterator& MatConstIterator::operator ++()
1799 if( m && (ptr += elemSize) >= sliceEnd )
1807 inline MatConstIterator MatConstIterator::operator ++(int)
1809 MatConstIterator b = *this;
1814 template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_() {}
1816 template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m)
1817 : MatConstIterator(_m) {}
1819 template<typename _Tp> inline MatConstIterator_<_Tp>::
1820 MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col)
1821 : MatConstIterator(_m, _row, _col) {}
1823 template<typename _Tp> inline MatConstIterator_<_Tp>::
1824 MatConstIterator_(const Mat_<_Tp>* _m, Point _pt)
1825 : MatConstIterator(_m, _pt) {}
1827 template<typename _Tp> inline MatConstIterator_<_Tp>::
1828 MatConstIterator_(const MatConstIterator_& it)
1829 : MatConstIterator(it) {}
1831 template<typename _Tp> inline MatConstIterator_<_Tp>&
1832 MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
1834 MatConstIterator::operator = (it);
1838 template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator *() const { return *(_Tp*)(this->ptr); }
1840 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs)
1842 MatConstIterator::operator += (ofs);
1846 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs)
1847 { return (*this += -ofs); }
1849 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
1851 MatConstIterator::operator --();
1855 template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
1857 MatConstIterator_ b = *this;
1858 MatConstIterator::operator --();
1862 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
1864 MatConstIterator::operator ++();
1868 template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
1870 MatConstIterator_ b = *this;
1871 MatConstIterator::operator ++();
1875 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_() : MatConstIterator_<_Tp>() {}
1877 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
1878 : MatConstIterator_<_Tp>(_m) {}
1880 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
1881 : MatConstIterator_<_Tp>(_m, _row, _col) {}
1883 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, Point _pt)
1884 : MatConstIterator_<_Tp>(_m, _pt) {}
1886 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, const int* _idx)
1887 : MatConstIterator_<_Tp>(_m, _idx) {}
1889 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
1890 : MatConstIterator_<_Tp>(it) {}
1892 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
1894 MatConstIterator::operator = (it);
1898 template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator *() const { return *(_Tp*)(this->ptr); }
1900 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs)
1902 MatConstIterator::operator += (ofs);
1906 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs)
1908 MatConstIterator::operator += (-ofs);
1912 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
1914 MatConstIterator::operator --();
1918 template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
1920 MatIterator_ b = *this;
1921 MatConstIterator::operator --();
1925 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
1927 MatConstIterator::operator ++();
1931 template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
1933 MatIterator_ b = *this;
1934 MatConstIterator::operator ++();
1938 template<typename _Tp> inline Point MatConstIterator_<_Tp>::pos() const
1942 CV_DbgAssert( m->dims <= 2 );
1943 if( m->isContinuous() )
1945 ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data;
1946 int y = (int)(ofs / m->cols), x = (int)(ofs - (ptrdiff_t)y*m->cols);
1951 ptrdiff_t ofs = (uchar*)ptr - m->data;
1952 int y = (int)(ofs / m->step), x = (int)((ofs - y*m->step)/sizeof(_Tp));
1958 operator == (const MatConstIterator& a, const MatConstIterator& b)
1959 { return a.m == b.m && a.ptr == b.ptr; }
1961 template<typename _Tp> static inline bool
1962 operator != (const MatConstIterator& a, const MatConstIterator& b)
1963 { return !(a == b); }
1965 template<typename _Tp> static inline bool
1966 operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
1967 { return a.m == b.m && a.ptr == b.ptr; }
1969 template<typename _Tp> static inline bool
1970 operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
1971 { return a.m != b.m || a.ptr != b.ptr; }
1973 template<typename _Tp> static inline bool
1974 operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
1975 { return a.m == b.m && a.ptr == b.ptr; }
1977 template<typename _Tp> static inline bool
1978 operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
1979 { return a.m != b.m || a.ptr != b.ptr; }
1982 operator < (const MatConstIterator& a, const MatConstIterator& b)
1983 { return a.ptr < b.ptr; }
1986 operator > (const MatConstIterator& a, const MatConstIterator& b)
1987 { return a.ptr > b.ptr; }
1990 operator <= (const MatConstIterator& a, const MatConstIterator& b)
1991 { return a.ptr <= b.ptr; }
1994 operator >= (const MatConstIterator& a, const MatConstIterator& b)
1995 { return a.ptr >= b.ptr; }
1997 CV_EXPORTS ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a);
1999 static inline MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs)
2000 { MatConstIterator b = a; return b += ofs; }
2002 static inline MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a)
2003 { MatConstIterator b = a; return b += ofs; }
2005 static inline MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs)
2006 { MatConstIterator b = a; return b += -ofs; }
2008 template<typename _Tp> static inline MatConstIterator_<_Tp>
2009 operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
2010 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; }
2012 template<typename _Tp> static inline MatConstIterator_<_Tp>
2013 operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a)
2014 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; }
2016 template<typename _Tp> static inline MatConstIterator_<_Tp>
2017 operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
2018 { MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatConstIterator_<_Tp>&)t; }
2020 inline uchar* MatConstIterator::operator [](ptrdiff_t i) const
2021 { return *(*this + i); }
2023 template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const
2024 { return *(_Tp*)MatConstIterator::operator [](i); }
2026 template<typename _Tp> static inline MatIterator_<_Tp>
2027 operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
2028 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; }
2030 template<typename _Tp> static inline MatIterator_<_Tp>
2031 operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a)
2032 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; }
2034 template<typename _Tp> static inline MatIterator_<_Tp>
2035 operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
2036 { MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatIterator_<_Tp>&)t; }
2038 template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const
2039 { return *(*this + i); }
2041 template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
2042 { return Mat::begin<_Tp>(); }
2044 template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::end() const
2045 { return Mat::end<_Tp>(); }
2047 template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::begin()
2048 { return Mat::begin<_Tp>(); }
2050 template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::end()
2051 { return Mat::end<_Tp>(); }
2053 template<typename _Tp> inline MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m) : it(_m) {}
2055 template<typename _Tp> template<typename T2> inline MatCommaInitializer_<_Tp>&
2056 MatCommaInitializer_<_Tp>::operator , (T2 v)
2058 CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() );
2059 *this->it = _Tp(v); ++this->it;
2063 template<typename _Tp> inline Mat_<_Tp> MatCommaInitializer_<_Tp>::operator *() const
2065 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
2066 return Mat_<_Tp>(*this->it.m);
2069 template<typename _Tp> inline MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
2071 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
2072 return Mat_<_Tp>(*this->it.m);
2075 template<typename _Tp, typename T2> static inline MatCommaInitializer_<_Tp>
2076 operator << (const Mat_<_Tp>& m, T2 val)
2078 MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
2079 return (commaInitializer, val);
2082 //////////////////////////////// SparseMat ////////////////////////////////
2084 inline SparseMat::SparseMat()
2085 : flags(MAGIC_VAL), hdr(0)
2089 inline SparseMat::SparseMat(int _dims, const int* _sizes, int _type)
2090 : flags(MAGIC_VAL), hdr(0)
2092 create(_dims, _sizes, _type);
2095 inline SparseMat::SparseMat(const SparseMat& m)
2096 : flags(m.flags), hdr(m.hdr)
2101 inline SparseMat::~SparseMat()
2106 inline SparseMat& SparseMat::operator = (const SparseMat& m)
2111 CV_XADD(&m.hdr->refcount, 1);
2119 inline SparseMat& SparseMat::operator = (const Mat& m)
2120 { return (*this = SparseMat(m)); }
2122 inline SparseMat SparseMat::clone() const
2130 inline void SparseMat::assignTo( SparseMat& m, int _type ) const
2135 convertTo(m, _type);
2138 inline void SparseMat::addref()
2139 { if( hdr ) CV_XADD(&hdr->refcount, 1); }
2141 inline void SparseMat::release()
2143 if( hdr && CV_XADD(&hdr->refcount, -1) == 1 )
2148 inline size_t SparseMat::elemSize() const
2149 { return CV_ELEM_SIZE(flags); }
2151 inline size_t SparseMat::elemSize1() const
2152 { return CV_ELEM_SIZE1(flags); }
2154 inline int SparseMat::type() const
2155 { return CV_MAT_TYPE(flags); }
2157 inline int SparseMat::depth() const
2158 { return CV_MAT_DEPTH(flags); }
2160 inline int SparseMat::channels() const
2161 { return CV_MAT_CN(flags); }
2163 inline const int* SparseMat::size() const
2165 return hdr ? hdr->size : 0;
2168 inline int SparseMat::size(int i) const
2172 CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
2173 return hdr->size[i];
2178 inline int SparseMat::dims() const
2180 return hdr ? hdr->dims : 0;
2183 inline size_t SparseMat::nzcount() const
2185 return hdr ? hdr->nodeCount : 0;
2188 inline size_t SparseMat::hash(int i0) const
2193 inline size_t SparseMat::hash(int i0, int i1) const
2195 return (size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1;
2198 inline size_t SparseMat::hash(int i0, int i1, int i2) const
2200 return ((size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1)*HASH_SCALE + (unsigned)i2;
2203 inline size_t SparseMat::hash(const int* idx) const
2205 size_t h = (unsigned)idx[0];
2208 int i, d = hdr->dims;
2209 for( i = 1; i < d; i++ )
2210 h = h*HASH_SCALE + (unsigned)idx[i];
2214 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, size_t* hashval)
2215 { return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval); }
2217 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
2218 { return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); }
2220 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
2221 { return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); }
2223 template<typename _Tp> inline _Tp& SparseMat::ref(const int* idx, size_t* hashval)
2224 { return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval); }
2226 template<typename _Tp> inline _Tp SparseMat::value(int i0, size_t* hashval) const
2228 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
2229 return p ? *p : _Tp();
2232 template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, size_t* hashval) const
2234 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
2235 return p ? *p : _Tp();
2238 template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
2240 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
2241 return p ? *p : _Tp();
2244 template<typename _Tp> inline _Tp SparseMat::value(const int* idx, size_t* hashval) const
2246 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
2247 return p ? *p : _Tp();
2250 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, size_t* hashval) const
2251 { return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval); }
2253 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
2254 { return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval); }
2256 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
2257 { return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval); }
2259 template<typename _Tp> inline const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
2260 { return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval); }
2262 template<typename _Tp> inline _Tp& SparseMat::value(Node* n)
2263 { return *(_Tp*)((uchar*)n + hdr->valueOffset); }
2265 template<typename _Tp> inline const _Tp& SparseMat::value(const Node* n) const
2266 { return *(const _Tp*)((const uchar*)n + hdr->valueOffset); }
2268 inline SparseMat::Node* SparseMat::node(size_t nidx)
2269 { return (Node*)(void*)&hdr->pool[nidx]; }
2271 inline const SparseMat::Node* SparseMat::node(size_t nidx) const
2272 { return (const Node*)(void*)&hdr->pool[nidx]; }
2274 inline SparseMatIterator SparseMat::begin()
2275 { return SparseMatIterator(this); }
2277 inline SparseMatConstIterator SparseMat::begin() const
2278 { return SparseMatConstIterator(this); }
2280 inline SparseMatIterator SparseMat::end()
2281 { SparseMatIterator it(this); it.seekEnd(); return it; }
2283 inline SparseMatConstIterator SparseMat::end() const
2284 { SparseMatConstIterator it(this); it.seekEnd(); return it; }
2286 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat::begin()
2287 { return SparseMatIterator_<_Tp>(this); }
2289 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat::begin() const
2290 { return SparseMatConstIterator_<_Tp>(this); }
2292 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat::end()
2293 { SparseMatIterator_<_Tp> it(this); it.seekEnd(); return it; }
2295 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat::end() const
2296 { SparseMatConstIterator_<_Tp> it(this); it.seekEnd(); return it; }
2299 inline SparseMatConstIterator::SparseMatConstIterator()
2300 : m(0), hashidx(0), ptr(0)
2304 inline SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
2305 : m(it.m), hashidx(it.hashidx), ptr(it.ptr)
2309 static inline bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
2310 { return it1.m == it2.m && it1.ptr == it2.ptr; }
2312 static inline bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
2313 { return !(it1 == it2); }
2316 inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
2321 hashidx = it.hashidx;
2327 template<typename _Tp> inline const _Tp& SparseMatConstIterator::value() const
2328 { return *(_Tp*)ptr; }
2330 inline const SparseMat::Node* SparseMatConstIterator::node() const
2332 return ptr && m && m->hdr ?
2333 (const SparseMat::Node*)(void*)(ptr - m->hdr->valueOffset) : 0;
2336 inline SparseMatConstIterator SparseMatConstIterator::operator ++(int)
2338 SparseMatConstIterator it = *this;
2344 inline void SparseMatConstIterator::seekEnd()
2348 hashidx = m->hdr->hashtab.size();
2353 inline SparseMatIterator::SparseMatIterator()
2356 inline SparseMatIterator::SparseMatIterator(SparseMat* _m)
2357 : SparseMatConstIterator(_m)
2360 inline SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
2361 : SparseMatConstIterator(it)
2365 inline SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
2367 (SparseMatConstIterator&)*this = it;
2371 template<typename _Tp> inline _Tp& SparseMatIterator::value() const
2372 { return *(_Tp*)ptr; }
2374 inline SparseMat::Node* SparseMatIterator::node() const
2376 return (SparseMat::Node*)SparseMatConstIterator::node();
2379 inline SparseMatIterator& SparseMatIterator::operator ++()
2381 SparseMatConstIterator::operator ++();
2385 inline SparseMatIterator SparseMatIterator::operator ++(int)
2387 SparseMatIterator it = *this;
2393 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_()
2394 { flags = MAGIC_VAL | DataType<_Tp>::type; }
2396 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes)
2397 : SparseMat(_dims, _sizes, DataType<_Tp>::type)
2400 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
2402 if( m.type() == DataType<_Tp>::type )
2403 *this = (const SparseMat_<_Tp>&)m;
2405 m.convertTo(*this, DataType<_Tp>::type);
2408 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
2410 this->flags = m.flags;
2413 CV_XADD(&this->hdr->refcount, 1);
2416 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Mat& m)
2422 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const CvSparseMat* m)
2428 template<typename _Tp> inline SparseMat_<_Tp>&
2429 SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
2433 if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
2441 template<typename _Tp> inline SparseMat_<_Tp>&
2442 SparseMat_<_Tp>::operator = (const SparseMat& m)
2444 if( m.type() == DataType<_Tp>::type )
2445 return (*this = (const SparseMat_<_Tp>&)m);
2446 m.convertTo(*this, DataType<_Tp>::type);
2450 template<typename _Tp> inline SparseMat_<_Tp>&
2451 SparseMat_<_Tp>::operator = (const Mat& m)
2452 { return (*this = SparseMat(m)); }
2454 template<typename _Tp> inline SparseMat_<_Tp>
2455 SparseMat_<_Tp>::clone() const
2462 template<typename _Tp> inline void
2463 SparseMat_<_Tp>::create(int _dims, const int* _sizes)
2465 SparseMat::create(_dims, _sizes, DataType<_Tp>::type);
2468 template<typename _Tp> inline
2469 SparseMat_<_Tp>::operator CvSparseMat*() const
2471 return SparseMat::operator CvSparseMat*();
2474 template<typename _Tp> inline int SparseMat_<_Tp>::type() const
2475 { return DataType<_Tp>::type; }
2477 template<typename _Tp> inline int SparseMat_<_Tp>::depth() const
2478 { return DataType<_Tp>::depth; }
2480 template<typename _Tp> inline int SparseMat_<_Tp>::channels() const
2481 { return DataType<_Tp>::channels; }
2483 template<typename _Tp> inline _Tp&
2484 SparseMat_<_Tp>::ref(int i0, size_t* hashval)
2485 { return SparseMat::ref<_Tp>(i0, hashval); }
2487 template<typename _Tp> inline _Tp
2488 SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const
2489 { return SparseMat::value<_Tp>(i0, hashval); }
2491 template<typename _Tp> inline _Tp&
2492 SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
2493 { return SparseMat::ref<_Tp>(i0, i1, hashval); }
2495 template<typename _Tp> inline _Tp
2496 SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
2497 { return SparseMat::value<_Tp>(i0, i1, hashval); }
2499 template<typename _Tp> inline _Tp&
2500 SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
2501 { return SparseMat::ref<_Tp>(i0, i1, i2, hashval); }
2503 template<typename _Tp> inline _Tp
2504 SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
2505 { return SparseMat::value<_Tp>(i0, i1, i2, hashval); }
2507 template<typename _Tp> inline _Tp&
2508 SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
2509 { return SparseMat::ref<_Tp>(idx, hashval); }
2511 template<typename _Tp> inline _Tp
2512 SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
2513 { return SparseMat::value<_Tp>(idx, hashval); }
2515 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
2516 { return SparseMatIterator_<_Tp>(this); }
2518 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
2519 { return SparseMatConstIterator_<_Tp>(this); }
2521 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
2522 { SparseMatIterator_<_Tp> it(this); it.seekEnd(); return it; }
2524 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
2525 { SparseMatConstIterator_<_Tp> it(this); it.seekEnd(); return it; }
2527 template<typename _Tp> inline
2528 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
2531 template<typename _Tp> inline
2532 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
2533 : SparseMatConstIterator(_m)
2536 template<typename _Tp> inline
2537 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat* _m)
2538 : SparseMatConstIterator(_m)
2540 CV_Assert( _m->type() == DataType<_Tp>::type );
2543 template<typename _Tp> inline
2544 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
2545 : SparseMatConstIterator(it)
2548 template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
2549 SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
2550 { return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
2551 (*reinterpret_cast<SparseMatConstIterator*>(this) =
2552 reinterpret_cast<const SparseMatConstIterator&>(it)); }
2554 template<typename _Tp> inline const _Tp&
2555 SparseMatConstIterator_<_Tp>::operator *() const
2556 { return *(const _Tp*)this->ptr; }
2558 template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
2559 SparseMatConstIterator_<_Tp>::operator ++()
2561 SparseMatConstIterator::operator ++();
2565 template<typename _Tp> inline SparseMatConstIterator_<_Tp>
2566 SparseMatConstIterator_<_Tp>::operator ++(int)
2568 SparseMatConstIterator it = *this;
2569 SparseMatConstIterator::operator ++();
2573 template<typename _Tp> inline
2574 SparseMatIterator_<_Tp>::SparseMatIterator_()
2577 template<typename _Tp> inline
2578 SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
2579 : SparseMatConstIterator_<_Tp>(_m)
2582 template<typename _Tp> inline
2583 SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat* _m)
2584 : SparseMatConstIterator_<_Tp>(_m)
2587 template<typename _Tp> inline
2588 SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
2589 : SparseMatConstIterator_<_Tp>(it)
2592 template<typename _Tp> inline SparseMatIterator_<_Tp>&
2593 SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
2594 { return reinterpret_cast<SparseMatIterator_<_Tp>&>
2595 (*reinterpret_cast<SparseMatConstIterator*>(this) =
2596 reinterpret_cast<const SparseMatConstIterator&>(it)); }
2598 template<typename _Tp> inline _Tp&
2599 SparseMatIterator_<_Tp>::operator *() const
2600 { return *(_Tp*)this->ptr; }
2602 template<typename _Tp> inline SparseMatIterator_<_Tp>&
2603 SparseMatIterator_<_Tp>::operator ++()
2605 SparseMatConstIterator::operator ++();
2609 template<typename _Tp> inline SparseMatIterator_<_Tp>
2610 SparseMatIterator_<_Tp>::operator ++(int)
2612 SparseMatIterator it = *this;
2613 SparseMatConstIterator::operator ++();