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;
373 inline Mat Mat::operator()( Range _rowRange, Range _colRange ) const
375 return Mat(*this, _rowRange, _colRange);
378 inline Mat Mat::operator()( const Rect& roi ) const
379 { return Mat(*this, roi); }
381 inline Mat Mat::operator()(const Range* ranges) const
383 return Mat(*this, ranges);
386 inline Mat::operator CvMat() const
388 CV_DbgAssert(dims <= 2);
389 CvMat m = cvMat(rows, dims == 1 ? 1 : cols, type(), data);
390 m.step = (int)step[0];
391 m.type = (m.type & ~CONTINUOUS_FLAG) | (flags & CONTINUOUS_FLAG);
395 inline bool Mat::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; }
396 inline bool Mat::isSubmatrix() const { return (flags & SUBMATRIX_FLAG) != 0; }
397 inline size_t Mat::elemSize() const { return dims > 0 ? step.p[dims-1] : 0; }
398 inline size_t Mat::elemSize1() const { return CV_ELEM_SIZE1(flags); }
399 inline int Mat::type() const { return CV_MAT_TYPE(flags); }
400 inline int Mat::depth() const { return CV_MAT_DEPTH(flags); }
401 inline int Mat::channels() const { return CV_MAT_CN(flags); }
402 inline size_t Mat::step1(int i) const { return step.p[i]/elemSize1(); }
403 inline bool Mat::empty() const { return data == 0 || total() == 0; }
404 inline size_t Mat::total() const
407 return (size_t)rows*cols;
409 for( int i = 0; i < dims; i++ )
414 inline uchar* Mat::ptr(int y)
416 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
417 return data + step.p[0]*y;
420 inline const uchar* Mat::ptr(int y) const
422 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
423 return data + step.p[0]*y;
426 template<typename _Tp> inline _Tp* Mat::ptr(int y)
428 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
429 return (_Tp*)(data + step.p[0]*y);
432 template<typename _Tp> inline const _Tp* Mat::ptr(int y) const
434 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
435 return (const _Tp*)(data + step.p[0]*y);
439 inline uchar* Mat::ptr(int i0, int i1)
441 CV_DbgAssert( dims >= 2 && data &&
442 (unsigned)i0 < (unsigned)size.p[0] &&
443 (unsigned)i1 < (unsigned)size.p[1] );
444 return data + i0*step.p[0] + i1*step.p[1];
447 inline const uchar* Mat::ptr(int i0, int i1) const
449 CV_DbgAssert( dims >= 2 && data &&
450 (unsigned)i0 < (unsigned)size.p[0] &&
451 (unsigned)i1 < (unsigned)size.p[1] );
452 return data + i0*step.p[0] + i1*step.p[1];
455 template<typename _Tp> inline _Tp* Mat::ptr(int i0, int i1)
457 CV_DbgAssert( dims >= 2 && data &&
458 (unsigned)i0 < (unsigned)size.p[0] &&
459 (unsigned)i1 < (unsigned)size.p[1] );
460 return (_Tp*)(data + i0*step.p[0] + i1*step.p[1]);
463 template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1) const
465 CV_DbgAssert( dims >= 2 && data &&
466 (unsigned)i0 < (unsigned)size.p[0] &&
467 (unsigned)i1 < (unsigned)size.p[1] );
468 return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1]);
471 inline uchar* Mat::ptr(int i0, int i1, int i2)
473 CV_DbgAssert( dims >= 3 && data &&
474 (unsigned)i0 < (unsigned)size.p[0] &&
475 (unsigned)i1 < (unsigned)size.p[1] &&
476 (unsigned)i2 < (unsigned)size.p[2] );
477 return data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2];
480 inline const uchar* Mat::ptr(int i0, int i1, int i2) const
482 CV_DbgAssert( dims >= 3 && data &&
483 (unsigned)i0 < (unsigned)size.p[0] &&
484 (unsigned)i1 < (unsigned)size.p[1] &&
485 (unsigned)i2 < (unsigned)size.p[2] );
486 return data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2];
489 template<typename _Tp> inline _Tp* Mat::ptr(int i0, int i1, int i2)
491 CV_DbgAssert( dims >= 3 && data &&
492 (unsigned)i0 < (unsigned)size.p[0] &&
493 (unsigned)i1 < (unsigned)size.p[1] &&
494 (unsigned)i2 < (unsigned)size.p[2] );
495 return (_Tp*)(data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]);
498 template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1, int i2) const
500 CV_DbgAssert( dims >= 3 && data &&
501 (unsigned)i0 < (unsigned)size.p[0] &&
502 (unsigned)i1 < (unsigned)size.p[1] &&
503 (unsigned)i2 < (unsigned)size.p[2] );
504 return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]);
507 inline uchar* Mat::ptr(const int* idx)
511 CV_DbgAssert( d >= 1 && p );
512 for( i = 0; i < d; i++ )
514 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
515 p += idx[i]*step.p[i];
520 inline const uchar* Mat::ptr(const int* idx) const
524 CV_DbgAssert( d >= 1 && p );
525 for( i = 0; i < d; i++ )
527 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
528 p += idx[i]*step.p[i];
533 template<typename _Tp> inline _Tp& Mat::at(int i0, int i1)
535 CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
536 (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
537 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
538 return ((_Tp*)(data + step.p[0]*i0))[i1];
541 template<typename _Tp> inline const _Tp& Mat::at(int i0, int i1) const
543 CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
544 (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
545 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
546 return ((const _Tp*)(data + step.p[0]*i0))[i1];
549 template<typename _Tp> inline _Tp& Mat::at(Point pt)
551 CV_DbgAssert( dims <= 2 && data && (unsigned)pt.y < (unsigned)size.p[0] &&
552 (unsigned)(pt.x*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
553 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
554 return ((_Tp*)(data + step.p[0]*pt.y))[pt.x];
557 template<typename _Tp> inline const _Tp& Mat::at(Point pt) const
559 CV_DbgAssert( dims <= 2 && data && (unsigned)pt.y < (unsigned)size.p[0] &&
560 (unsigned)(pt.x*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
561 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
562 return ((const _Tp*)(data + step.p[0]*pt.y))[pt.x];
565 template<typename _Tp> inline _Tp& Mat::at(int i0)
567 CV_DbgAssert( dims <= 2 && data &&
568 (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) &&
569 elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
570 if( isContinuous() || size.p[0] == 1 )
571 return ((_Tp*)data)[i0];
573 return *(_Tp*)(data + step.p[0]*i0);
574 int i = i0/cols, j = i0 - i*cols;
575 return ((_Tp*)(data + step.p[0]*i))[j];
578 template<typename _Tp> inline const _Tp& Mat::at(int i0) const
580 CV_DbgAssert( dims <= 2 && data &&
581 (unsigned)i0 < (unsigned)(size.p[0]*size.p[1]) &&
582 elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
583 if( isContinuous() || size.p[0] == 1 )
584 return ((const _Tp*)data)[i0];
586 return *(const _Tp*)(data + step.p[0]*i0);
587 int i = i0/cols, j = i0 - i*cols;
588 return ((const _Tp*)(data + step.p[0]*i))[j];
591 template<typename _Tp> inline _Tp& Mat::at(int i0, int i1, int i2)
593 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
594 return *(_Tp*)ptr(i0, i1, i2);
596 template<typename _Tp> inline const _Tp& Mat::at(int i0, int i1, int i2) const
598 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
599 return *(const _Tp*)ptr(i0, i1, i2);
601 template<typename _Tp> inline _Tp& Mat::at(const int* idx)
603 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
604 return *(_Tp*)ptr(idx);
606 template<typename _Tp> inline const _Tp& Mat::at(const int* idx) const
608 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
609 return *(const _Tp*)ptr(idx);
611 template<typename _Tp, int n> _Tp& Mat::at(const Vec<int, n>& idx)
613 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
614 return *(_Tp*)ptr(idx.val);
616 template<typename _Tp, int n> inline const _Tp& Mat::at(const Vec<int, n>& idx) const
618 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
619 return *(const _Tp*)ptr(idx.val);
623 template<typename _Tp> inline MatConstIterator_<_Tp> Mat::begin() const
625 CV_DbgAssert( elemSize() == sizeof(_Tp) );
626 return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
629 template<typename _Tp> inline MatConstIterator_<_Tp> Mat::end() const
631 CV_DbgAssert( elemSize() == sizeof(_Tp) );
632 MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
637 template<typename _Tp> inline MatIterator_<_Tp> Mat::begin()
639 CV_DbgAssert( elemSize() == sizeof(_Tp) );
640 return MatIterator_<_Tp>((Mat_<_Tp>*)this);
643 template<typename _Tp> inline MatIterator_<_Tp> Mat::end()
645 CV_DbgAssert( elemSize() == sizeof(_Tp) );
646 MatIterator_<_Tp> it((Mat_<_Tp>*)this);
651 template<typename _Tp> inline Mat::operator vector<_Tp>() const
658 template<typename _Tp, int n> inline Mat::operator Vec<_Tp, n>() const
660 CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) &&
661 rows + cols - 1 == n && channels() == 1 );
663 if( isContinuous() && type() == DataType<_Tp>::type )
664 return Vec<_Tp, n>((_Tp*)data);
665 Vec<_Tp, n> v; Mat tmp(rows, cols, DataType<_Tp>::type, v.val);
666 convertTo(tmp, tmp.type());
670 template<typename _Tp, int m, int n> inline Mat::operator Matx<_Tp, m, n>() const
672 CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 );
674 if( isContinuous() && type() == DataType<_Tp>::type )
675 return Matx<_Tp, m, n>((_Tp*)data);
676 Matx<_Tp, m, n> mtx; Mat tmp(rows, cols, DataType<_Tp>::type, mtx.val);
677 convertTo(tmp, tmp.type());
682 template<typename _Tp> inline void Mat::push_back(const _Tp& elem)
686 *this = Mat(1, 1, DataType<_Tp>::type, (void*)&elem).clone();
689 CV_Assert(DataType<_Tp>::type == type() && cols == 1
690 /* && dims == 2 (cols == 1 implies dims == 2) */);
691 uchar* tmp = dataend + step[0];
692 if( !isSubmatrix() && isContinuous() && tmp <= datalimit )
694 *(_Tp*)(data + (size.p[0]++)*step.p[0]) = elem;
701 template<typename _Tp> inline void Mat::push_back(const Mat_<_Tp>& m)
703 push_back((const Mat&)m);
706 inline Mat::MSize::MSize(int* _p) : p(_p) {}
707 inline Size Mat::MSize::operator()() const
709 CV_DbgAssert(p[-1] <= 2);
710 return Size(p[1], p[0]);
712 inline const int& Mat::MSize::operator[](int i) const { return p[i]; }
713 inline int& Mat::MSize::operator[](int i) { return p[i]; }
714 inline Mat::MSize::operator const int*() const { return p; }
716 inline bool Mat::MSize::operator == (const MSize& sz) const
718 int d = p[-1], dsz = sz.p[-1];
722 return p[0] == sz.p[0] && p[1] == sz.p[1];
724 for( int i = 0; i < d; i++ )
725 if( p[i] != sz.p[i] )
730 inline bool Mat::MSize::operator != (const MSize& sz) const
732 return !(*this == sz);
735 inline Mat::MStep::MStep() { p = buf; p[0] = p[1] = 0; }
736 inline Mat::MStep::MStep(size_t s) { p = buf; p[0] = s; p[1] = 0; }
737 inline const size_t& Mat::MStep::operator[](int i) const { return p[i]; }
738 inline size_t& Mat::MStep::operator[](int i) { return p[i]; }
739 inline Mat::MStep::operator size_t() const
741 CV_DbgAssert( p == buf );
744 inline Mat::MStep& Mat::MStep::operator = (size_t s)
746 CV_DbgAssert( p == buf );
751 static inline Mat cvarrToMatND(const CvArr* arr, bool copyData=false, int coiMode=0)
753 return cvarrToMat(arr, copyData, true, coiMode);
756 ///////////////////////////////////////////// SVD //////////////////////////////////////////////////////
759 inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); }
760 inline void SVD::solveZ( InputArray m, OutputArray _dst )
762 Mat mtx = m.getMat();
763 SVD svd(mtx, (mtx.rows >= mtx.cols ? 0 : SVD::FULL_UV));
764 _dst.create(svd.vt.cols, 1, svd.vt.type());
765 Mat dst = _dst.getMat();
766 svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
769 template<typename _Tp, int m, int n, int nm> inline void
770 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt )
772 assert( nm == MIN(m, n));
773 Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false);
774 SVD::compute(_a, _w, _u, _vt);
775 CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]);
778 template<typename _Tp, int m, int n, int nm> inline void
779 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w )
781 assert( nm == MIN(m, n));
782 Mat _a(a, false), _w(w, false);
783 SVD::compute(_a, _w);
784 CV_Assert(_w.data == (uchar*)&w.val[0]);
787 template<typename _Tp, int m, int n, int nm, int nb> inline void
788 SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u,
789 const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs,
790 Matx<_Tp, n, nb>& dst )
792 assert( nm == MIN(m, n));
793 Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false);
794 SVD::backSubst(_w, _u, _vt, _rhs, _dst);
795 CV_Assert(_dst.data == (uchar*)&dst.val[0]);
798 ///////////////////////////////// Mat_<_Tp> ////////////////////////////////////
800 template<typename _Tp> inline Mat_<_Tp>::Mat_()
801 : Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; }
803 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols)
804 : Mat(_rows, _cols, DataType<_Tp>::type) {}
806 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
807 : Mat(_rows, _cols, DataType<_Tp>::type) { *this = value; }
809 template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _sz)
810 : Mat(_sz.height, _sz.width, DataType<_Tp>::type) {}
812 template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
813 : Mat(_sz.height, _sz.width, DataType<_Tp>::type) { *this = value; }
815 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _dims, const int* _sz)
816 : Mat(_dims, _sz, DataType<_Tp>::type) {}
818 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s)
819 : Mat(_dims, _sz, DataType<_Tp>::type, Scalar(_s)) {}
821 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges)
824 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat& m)
825 : Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; *this = m; }
827 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m)
830 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps)
831 : Mat(_rows, _cols, DataType<_Tp>::type, _data, steps) {}
833 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Range& _rowRange, const Range& _colRange)
834 : Mat(m, _rowRange, _colRange) {}
836 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
839 template<typename _Tp> template<int n> inline
840 Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData)
841 : Mat(n/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&vec)
843 CV_Assert(n%DataType<_Tp>::channels == 0);
848 template<typename _Tp> template<int m, int n> inline
849 Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type,m,n>& M, bool copyData)
850 : Mat(m, n/DataType<_Tp>::channels, DataType<_Tp>::type, (void*)&M)
852 CV_Assert(n % DataType<_Tp>::channels == 0);
857 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
858 : Mat(2/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt)
860 CV_Assert(2 % DataType<_Tp>::channels == 0);
865 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
866 : Mat(3/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt)
868 CV_Assert(3 % DataType<_Tp>::channels == 0);
873 template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer)
874 : Mat(commaInitializer) {}
876 template<typename _Tp> inline Mat_<_Tp>::Mat_(const vector<_Tp>& vec, bool copyData)
877 : Mat(vec, copyData) {}
879 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
881 if( DataType<_Tp>::type == m.type() )
886 if( DataType<_Tp>::depth == m.depth() )
888 return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0));
890 CV_DbgAssert(DataType<_Tp>::channels == m.channels());
891 m.convertTo(*this, type());
895 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
901 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
903 typedef typename DataType<_Tp>::vec_type VT;
904 Mat::operator=(Scalar((const VT&)s));
908 template<typename _Tp> inline void Mat_<_Tp>::create(int _rows, int _cols)
910 Mat::create(_rows, _cols, DataType<_Tp>::type);
913 template<typename _Tp> inline void Mat_<_Tp>::create(Size _sz)
915 Mat::create(_sz, DataType<_Tp>::type);
918 template<typename _Tp> inline void Mat_<_Tp>::create(int _dims, const int* _sz)
920 Mat::create(_dims, _sz, DataType<_Tp>::type);
924 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
925 { return Mat_<_Tp>(Mat::cross(m)); }
927 template<typename _Tp> template<typename T2> inline Mat_<_Tp>::operator Mat_<T2>() const
928 { return Mat_<T2>(*this); }
930 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::row(int y) const
931 { return Mat_(*this, Range(y, y+1), Range::all()); }
932 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::col(int x) const
933 { return Mat_(*this, Range::all(), Range(x, x+1)); }
934 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::diag(int d) const
935 { return Mat_(Mat::diag(d)); }
936 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::clone() const
937 { return Mat_(Mat::clone()); }
939 template<typename _Tp> inline size_t Mat_<_Tp>::elemSize() const
941 CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
945 template<typename _Tp> inline size_t Mat_<_Tp>::elemSize1() const
947 CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp)/DataType<_Tp>::channels );
948 return sizeof(_Tp)/DataType<_Tp>::channels;
950 template<typename _Tp> inline int Mat_<_Tp>::type() const
952 CV_DbgAssert( Mat::type() == DataType<_Tp>::type );
953 return DataType<_Tp>::type;
955 template<typename _Tp> inline int Mat_<_Tp>::depth() const
957 CV_DbgAssert( Mat::depth() == DataType<_Tp>::depth );
958 return DataType<_Tp>::depth;
960 template<typename _Tp> inline int Mat_<_Tp>::channels() const
962 CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
963 return DataType<_Tp>::channels;
965 template<typename _Tp> inline size_t Mat_<_Tp>::stepT(int i) const { return step.p[i]/elemSize(); }
966 template<typename _Tp> inline size_t Mat_<_Tp>::step1(int i) const { return step.p[i]/elemSize1(); }
968 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
969 { return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright)); }
971 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range& _rowRange, const Range& _colRange ) const
972 { return Mat_<_Tp>(*this, _rowRange, _colRange); }
974 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
975 { return Mat_<_Tp>(*this, roi); }
977 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const
978 { return Mat_<_Tp>(*this, ranges); }
980 template<typename _Tp> inline _Tp* Mat_<_Tp>::operator [](int y)
981 { return (_Tp*)ptr(y); }
982 template<typename _Tp> inline const _Tp* Mat_<_Tp>::operator [](int y) const
983 { return (const _Tp*)ptr(y); }
985 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0, int i1)
987 CV_DbgAssert( dims <= 2 && data &&
988 (unsigned)i0 < (unsigned)size.p[0] &&
989 (unsigned)i1 < (unsigned)size.p[1] &&
990 type() == DataType<_Tp>::type );
991 return ((_Tp*)(data + step.p[0]*i0))[i1];
994 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const
996 CV_DbgAssert( dims <= 2 && data &&
997 (unsigned)i0 < (unsigned)size.p[0] &&
998 (unsigned)i1 < (unsigned)size.p[1] &&
999 type() == DataType<_Tp>::type );
1000 return ((const _Tp*)(data + step.p[0]*i0))[i1];
1003 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(Point pt)
1005 CV_DbgAssert( dims <= 2 && data &&
1006 (unsigned)pt.y < (unsigned)size.p[0] &&
1007 (unsigned)pt.x < (unsigned)size.p[1] &&
1008 type() == DataType<_Tp>::type );
1009 return ((_Tp*)(data + step.p[0]*pt.y))[pt.x];
1012 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(Point pt) const
1014 CV_DbgAssert( dims <= 2 && data &&
1015 (unsigned)pt.y < (unsigned)size.p[0] &&
1016 (unsigned)pt.x < (unsigned)size.p[1] &&
1017 type() == DataType<_Tp>::type );
1018 return ((const _Tp*)(data + step.p[0]*pt.y))[pt.x];
1021 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(const int* idx)
1023 return Mat::at<_Tp>(idx);
1026 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(const int* idx) const
1028 return Mat::at<_Tp>(idx);
1031 template<typename _Tp> template<int n> inline _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx)
1033 return Mat::at<_Tp>(idx);
1036 template<typename _Tp> template<int n> inline const _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx) const
1038 return Mat::at<_Tp>(idx);
1041 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0)
1043 return this->at<_Tp>(i0);
1046 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0) const
1048 return this->at<_Tp>(i0);
1051 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2)
1053 return this->at<_Tp>(i0, i1, i2);
1056 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const
1058 return this->at<_Tp>(i0, i1, i2);
1062 template<typename _Tp> inline Mat_<_Tp>::operator vector<_Tp>() const
1069 template<typename _Tp> template<int n> inline Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const
1071 CV_Assert(n % DataType<_Tp>::channels == 0);
1072 return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>();
1075 template<typename _Tp> template<int m, int n> inline Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const
1077 CV_Assert(n % DataType<_Tp>::channels == 0);
1079 Matx<typename DataType<_Tp>::channel_type, m, n> res = this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>();
1083 template<typename T1, typename T2, typename Op> inline void
1084 process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op )
1086 int y, x, rows = m1.rows, cols = m1.cols;
1088 CV_DbgAssert( m1.size() == m2.size() );
1090 for( y = 0; y < rows; y++ )
1092 const T1* src = m1[y];
1095 for( x = 0; x < cols; x++ )
1096 dst[x] = op(src[x]);
1100 template<typename T1, typename T2, typename T3, typename Op> inline void
1101 process( const Mat_<T1>& m1, const Mat_<T2>& m2, Mat_<T3>& m3, Op op )
1103 int y, x, rows = m1.rows, cols = m1.cols;
1105 CV_DbgAssert( m1.size() == m2.size() );
1107 for( y = 0; y < rows; y++ )
1109 const T1* src1 = m1[y];
1110 const T2* src2 = m2[y];
1113 for( x = 0; x < cols; x++ )
1114 dst[x] = op( src1[x], src2[x] );
1119 /////////////////////////////// Input/Output Arrays /////////////////////////////////
1121 template<typename _Tp> inline _InputArray::_InputArray(const vector<_Tp>& vec)
1122 : flags(FIXED_TYPE + STD_VECTOR + DataType<_Tp>::type), obj((void*)&vec) {}
1124 template<typename _Tp> inline _InputArray::_InputArray(const vector<vector<_Tp> >& vec)
1125 : flags(FIXED_TYPE + STD_VECTOR_VECTOR + DataType<_Tp>::type), obj((void*)&vec) {}
1127 template<typename _Tp> inline _InputArray::_InputArray(const vector<Mat_<_Tp> >& vec)
1128 : flags(FIXED_TYPE + STD_VECTOR_MAT + DataType<_Tp>::type), obj((void*)&vec) {}
1130 template<typename _Tp, int m, int n> inline _InputArray::_InputArray(const Matx<_Tp, m, n>& mtx)
1131 : flags(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type), obj((void*)&mtx), sz(n, m) {}
1133 template<typename _Tp> inline _InputArray::_InputArray(const _Tp* vec, int n)
1134 : flags(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type), obj((void*)vec), sz(n, 1) {}
1136 inline _InputArray::_InputArray(const Scalar& s)
1137 : flags(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F), obj((void*)&s), sz(1, 4) {}
1139 template<typename _Tp> inline _InputArray::_InputArray(const Mat_<_Tp>& m)
1140 : flags(FIXED_TYPE + MAT + DataType<_Tp>::type), obj((void*)&m) {}
1142 template<typename _Tp> inline _OutputArray::_OutputArray(vector<_Tp>& vec)
1143 : _InputArray(vec) {}
1144 template<typename _Tp> inline _OutputArray::_OutputArray(vector<vector<_Tp> >& vec)
1145 : _InputArray(vec) {}
1146 template<typename _Tp> inline _OutputArray::_OutputArray(vector<Mat_<_Tp> >& vec)
1147 : _InputArray(vec) {}
1148 template<typename _Tp> inline _OutputArray::_OutputArray(Mat_<_Tp>& m)
1150 template<typename _Tp, int m, int n> inline _OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx)
1151 : _InputArray(mtx) {}
1152 template<typename _Tp> inline _OutputArray::_OutputArray(_Tp* vec, int n)
1153 : _InputArray(vec, n) {}
1155 template<typename _Tp> inline _OutputArray::_OutputArray(const vector<_Tp>& vec)
1156 : _InputArray(vec) {flags |= FIXED_SIZE;}
1157 template<typename _Tp> inline _OutputArray::_OutputArray(const vector<vector<_Tp> >& vec)
1158 : _InputArray(vec) {flags |= FIXED_SIZE;}
1159 template<typename _Tp> inline _OutputArray::_OutputArray(const vector<Mat_<_Tp> >& vec)
1160 : _InputArray(vec) {flags |= FIXED_SIZE;}
1162 template<typename _Tp> inline _OutputArray::_OutputArray(const Mat_<_Tp>& m)
1163 : _InputArray(m) {flags |= FIXED_SIZE;}
1164 template<typename _Tp, int m, int n> inline _OutputArray::_OutputArray(const Matx<_Tp, m, n>& mtx)
1165 : _InputArray(mtx) {}
1166 template<typename _Tp> inline _OutputArray::_OutputArray(const _Tp* vec, int n)
1167 : _InputArray(vec, n) {}
1169 //////////////////////////////////// Matrix Expressions /////////////////////////////////////////
1171 class CV_EXPORTS MatOp
1175 virtual ~MatOp() {};
1177 virtual bool elementWise(const MatExpr& expr) const;
1178 virtual void assign(const MatExpr& expr, Mat& m, int type=-1) const = 0;
1179 virtual void roi(const MatExpr& expr, const Range& rowRange,
1180 const Range& colRange, MatExpr& res) const;
1181 virtual void diag(const MatExpr& expr, int d, MatExpr& res) const;
1182 virtual void augAssignAdd(const MatExpr& expr, Mat& m) const;
1183 virtual void augAssignSubtract(const MatExpr& expr, Mat& m) const;
1184 virtual void augAssignMultiply(const MatExpr& expr, Mat& m) const;
1185 virtual void augAssignDivide(const MatExpr& expr, Mat& m) const;
1186 virtual void augAssignAnd(const MatExpr& expr, Mat& m) const;
1187 virtual void augAssignOr(const MatExpr& expr, Mat& m) const;
1188 virtual void augAssignXor(const MatExpr& expr, Mat& m) const;
1190 virtual void add(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
1191 virtual void add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const;
1193 virtual void subtract(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
1194 virtual void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const;
1196 virtual void multiply(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const;
1197 virtual void multiply(const MatExpr& expr1, double s, MatExpr& res) const;
1199 virtual void divide(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const;
1200 virtual void divide(double s, const MatExpr& expr, MatExpr& res) const;
1202 virtual void abs(const MatExpr& expr, MatExpr& res) const;
1204 virtual void transpose(const MatExpr& expr, MatExpr& res) const;
1205 virtual void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
1206 virtual void invert(const MatExpr& expr, int method, MatExpr& res) const;
1208 virtual Size size(const MatExpr& expr) const;
1209 virtual int type(const MatExpr& expr) const;
1213 class CV_EXPORTS MatExpr
1216 MatExpr() : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s(Scalar()) {}
1217 MatExpr(const MatOp* _op, int _flags, const Mat& _a=Mat(), const Mat& _b=Mat(),
1218 const Mat& _c=Mat(), double _alpha=1, double _beta=1, const Scalar& _s=Scalar())
1219 : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s) {}
1220 explicit MatExpr(const Mat& m);
1221 operator Mat() const
1224 op->assign(*this, m);
1228 template<typename _Tp> operator Mat_<_Tp>() const
1231 op->assign(*this, m, DataType<_Tp>::type);
1235 MatExpr row(int y) const;
1236 MatExpr col(int x) const;
1237 MatExpr diag(int d=0) const;
1238 MatExpr operator()( const Range& rowRange, const Range& colRange ) const;
1239 MatExpr operator()( const Rect& roi ) const;
1241 Mat cross(const Mat& m) const;
1242 double dot(const Mat& m) const;
1245 MatExpr inv(int method = DECOMP_LU) const;
1246 MatExpr mul(const MatExpr& e, double scale=1) const;
1247 MatExpr mul(const Mat& m, double scale=1) const;
1261 CV_EXPORTS MatExpr operator + (const Mat& a, const Mat& b);
1262 CV_EXPORTS MatExpr operator + (const Mat& a, const Scalar& s);
1263 CV_EXPORTS MatExpr operator + (const Scalar& s, const Mat& a);
1264 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Mat& m);
1265 CV_EXPORTS MatExpr operator + (const Mat& m, const MatExpr& e);
1266 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Scalar& s);
1267 CV_EXPORTS MatExpr operator + (const Scalar& s, const MatExpr& e);
1268 CV_EXPORTS MatExpr operator + (const MatExpr& e1, const MatExpr& e2);
1270 CV_EXPORTS MatExpr operator - (const Mat& a, const Mat& b);
1271 CV_EXPORTS MatExpr operator - (const Mat& a, const Scalar& s);
1272 CV_EXPORTS MatExpr operator - (const Scalar& s, const Mat& a);
1273 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Mat& m);
1274 CV_EXPORTS MatExpr operator - (const Mat& m, const MatExpr& e);
1275 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Scalar& s);
1276 CV_EXPORTS MatExpr operator - (const Scalar& s, const MatExpr& e);
1277 CV_EXPORTS MatExpr operator - (const MatExpr& e1, const MatExpr& e2);
1279 CV_EXPORTS MatExpr operator - (const Mat& m);
1280 CV_EXPORTS MatExpr operator - (const MatExpr& e);
1282 CV_EXPORTS MatExpr operator * (const Mat& a, const Mat& b);
1283 CV_EXPORTS MatExpr operator * (const Mat& a, double s);
1284 CV_EXPORTS MatExpr operator * (double s, const Mat& a);
1285 CV_EXPORTS MatExpr operator * (const MatExpr& e, const Mat& m);
1286 CV_EXPORTS MatExpr operator * (const Mat& m, const MatExpr& e);
1287 CV_EXPORTS MatExpr operator * (const MatExpr& e, double s);
1288 CV_EXPORTS MatExpr operator * (double s, const MatExpr& e);
1289 CV_EXPORTS MatExpr operator * (const MatExpr& e1, const MatExpr& e2);
1291 CV_EXPORTS MatExpr operator / (const Mat& a, const Mat& b);
1292 CV_EXPORTS MatExpr operator / (const Mat& a, double s);
1293 CV_EXPORTS MatExpr operator / (double s, const Mat& a);
1294 CV_EXPORTS MatExpr operator / (const MatExpr& e, const Mat& m);
1295 CV_EXPORTS MatExpr operator / (const Mat& m, const MatExpr& e);
1296 CV_EXPORTS MatExpr operator / (const MatExpr& e, double s);
1297 CV_EXPORTS MatExpr operator / (double s, const MatExpr& e);
1298 CV_EXPORTS MatExpr operator / (const MatExpr& e1, const MatExpr& e2);
1300 CV_EXPORTS MatExpr operator < (const Mat& a, const Mat& b);
1301 CV_EXPORTS MatExpr operator < (const Mat& a, double s);
1302 CV_EXPORTS MatExpr operator < (double s, const Mat& a);
1304 CV_EXPORTS MatExpr operator <= (const Mat& a, const Mat& b);
1305 CV_EXPORTS MatExpr operator <= (const Mat& a, double s);
1306 CV_EXPORTS MatExpr operator <= (double s, const Mat& a);
1308 CV_EXPORTS MatExpr operator == (const Mat& a, const Mat& b);
1309 CV_EXPORTS MatExpr operator == (const Mat& a, double s);
1310 CV_EXPORTS MatExpr operator == (double s, const Mat& a);
1312 CV_EXPORTS MatExpr operator != (const Mat& a, const Mat& b);
1313 CV_EXPORTS MatExpr operator != (const Mat& a, double s);
1314 CV_EXPORTS MatExpr operator != (double s, const Mat& a);
1316 CV_EXPORTS MatExpr operator >= (const Mat& a, const Mat& b);
1317 CV_EXPORTS MatExpr operator >= (const Mat& a, double s);
1318 CV_EXPORTS MatExpr operator >= (double s, const Mat& a);
1320 CV_EXPORTS MatExpr operator > (const Mat& a, const Mat& b);
1321 CV_EXPORTS MatExpr operator > (const Mat& a, double s);
1322 CV_EXPORTS MatExpr operator > (double s, const Mat& a);
1324 CV_EXPORTS MatExpr min(const Mat& a, const Mat& b);
1325 CV_EXPORTS MatExpr min(const Mat& a, double s);
1326 CV_EXPORTS MatExpr min(double s, const Mat& a);
1328 CV_EXPORTS MatExpr max(const Mat& a, const Mat& b);
1329 CV_EXPORTS MatExpr max(const Mat& a, double s);
1330 CV_EXPORTS MatExpr max(double s, const Mat& a);
1332 template<typename _Tp> static inline MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1334 return cv::min((const Mat&)a, (const Mat&)b);
1337 template<typename _Tp> static inline MatExpr min(const Mat_<_Tp>& a, double s)
1339 return cv::min((const Mat&)a, s);
1342 template<typename _Tp> static inline MatExpr min(double s, const Mat_<_Tp>& a)
1344 return cv::min((const Mat&)a, s);
1347 template<typename _Tp> static inline MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1349 return cv::max((const Mat&)a, (const Mat&)b);
1352 template<typename _Tp> static inline MatExpr max(const Mat_<_Tp>& a, double s)
1354 return cv::max((const Mat&)a, s);
1357 template<typename _Tp> static inline MatExpr max(double s, const Mat_<_Tp>& a)
1359 return cv::max((const Mat&)a, s);
1362 template<typename _Tp> static inline void min(const Mat_<_Tp>& a, const Mat_<_Tp>& b, Mat_<_Tp>& c)
1364 cv::min((const Mat&)a, (const Mat&)b, (Mat&)c);
1367 template<typename _Tp> static inline void min(const Mat_<_Tp>& a, double s, Mat_<_Tp>& c)
1369 cv::min((const Mat&)a, s, (Mat&)c);
1372 template<typename _Tp> static inline void min(double s, const Mat_<_Tp>& a, Mat_<_Tp>& c)
1374 cv::min((const Mat&)a, s, (Mat&)c);
1377 template<typename _Tp> static inline void max(const Mat_<_Tp>& a, const Mat_<_Tp>& b, Mat_<_Tp>& c)
1379 cv::max((const Mat&)a, (const Mat&)b, (Mat&)c);
1382 template<typename _Tp> static inline void max(const Mat_<_Tp>& a, double s, Mat_<_Tp>& c)
1384 cv::max((const Mat&)a, s, (Mat&)c);
1387 template<typename _Tp> static inline void max(double s, const Mat_<_Tp>& a, Mat_<_Tp>& c)
1389 cv::max((const Mat&)a, s, (Mat&)c);
1393 CV_EXPORTS MatExpr operator & (const Mat& a, const Mat& b);
1394 CV_EXPORTS MatExpr operator & (const Mat& a, const Scalar& s);
1395 CV_EXPORTS MatExpr operator & (const Scalar& s, const Mat& a);
1397 CV_EXPORTS MatExpr operator | (const Mat& a, const Mat& b);
1398 CV_EXPORTS MatExpr operator | (const Mat& a, const Scalar& s);
1399 CV_EXPORTS MatExpr operator | (const Scalar& s, const Mat& a);
1401 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Mat& b);
1402 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Scalar& s);
1403 CV_EXPORTS MatExpr operator ^ (const Scalar& s, const Mat& a);
1405 CV_EXPORTS MatExpr operator ~(const Mat& m);
1407 CV_EXPORTS MatExpr abs(const Mat& m);
1408 CV_EXPORTS MatExpr abs(const MatExpr& e);
1410 template<typename _Tp> static inline MatExpr abs(const Mat_<_Tp>& m)
1412 return cv::abs((const Mat&)m);
1415 ////////////////////////////// Augmenting algebraic operations //////////////////////////////////
1417 inline Mat& Mat::operator = (const MatExpr& e)
1419 e.op->assign(e, *this);
1423 template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatExpr& e)
1425 e.op->assign(e, *this, DataType<_Tp>::type);
1428 template<typename _Tp> Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e)
1430 e.op->assign(e, *this, DataType<_Tp>::type);
1434 static inline Mat& operator += (const Mat& a, const Mat& b)
1440 static inline Mat& operator += (const Mat& a, const Scalar& s)
1446 template<typename _Tp> static inline
1447 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1450 return (Mat_<_Tp>&)a;
1453 template<typename _Tp> static inline
1454 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Scalar& s)
1457 return (Mat_<_Tp>&)a;
1460 static inline Mat& operator += (const Mat& a, const MatExpr& b)
1462 b.op->augAssignAdd(b, (Mat&)a);
1466 template<typename _Tp> static inline
1467 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b)
1469 b.op->augAssignAdd(b, (Mat&)a);
1470 return (Mat_<_Tp>&)a;
1473 static inline Mat& operator -= (const Mat& a, const Mat& b)
1475 subtract(a, b, (Mat&)a);
1479 static inline Mat& operator -= (const Mat& a, const Scalar& s)
1481 subtract(a, s, (Mat&)a);
1485 template<typename _Tp> static inline
1486 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1488 subtract(a, b, (Mat&)a);
1489 return (Mat_<_Tp>&)a;
1492 template<typename _Tp> static inline
1493 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Scalar& s)
1495 subtract(a, s, (Mat&)a);
1496 return (Mat_<_Tp>&)a;
1499 static inline Mat& operator -= (const Mat& a, const MatExpr& b)
1501 b.op->augAssignSubtract(b, (Mat&)a);
1505 template<typename _Tp> static inline
1506 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b)
1508 b.op->augAssignSubtract(b, (Mat&)a);
1509 return (Mat_<_Tp>&)a;
1512 static inline Mat& operator *= (const Mat& a, const Mat& b)
1514 gemm(a, b, 1, Mat(), 0, (Mat&)a, 0);
1518 static inline Mat& operator *= (const Mat& a, double s)
1520 a.convertTo((Mat&)a, -1, s);
1524 template<typename _Tp> static inline
1525 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1527 gemm(a, b, 1, Mat(), 0, (Mat&)a, 0);
1528 return (Mat_<_Tp>&)a;
1531 template<typename _Tp> static inline
1532 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, double s)
1534 a.convertTo((Mat&)a, -1, s);
1535 return (Mat_<_Tp>&)a;
1538 static inline Mat& operator *= (const Mat& a, const MatExpr& b)
1540 b.op->augAssignMultiply(b, (Mat&)a);
1544 template<typename _Tp> static inline
1545 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b)
1547 b.op->augAssignMultiply(b, (Mat&)a);
1548 return (Mat_<_Tp>&)a;
1551 static inline Mat& operator /= (const Mat& a, const Mat& b)
1553 divide(a, b, (Mat&)a);
1557 static inline Mat& operator /= (const Mat& a, double s)
1559 a.convertTo((Mat&)a, -1, 1./s);
1563 template<typename _Tp> static inline
1564 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1566 divide(a, b, (Mat&)a);
1567 return (Mat_<_Tp>&)a;
1570 template<typename _Tp> static inline
1571 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, double s)
1573 a.convertTo((Mat&)a, -1, 1./s);
1574 return (Mat_<_Tp>&)a;
1577 static inline Mat& operator /= (const Mat& a, const MatExpr& b)
1579 b.op->augAssignDivide(b, (Mat&)a);
1583 template<typename _Tp> static inline
1584 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b)
1586 b.op->augAssignDivide(b, (Mat&)a);
1587 return (Mat_<_Tp>&)a;
1590 ////////////////////////////// Logical operations ///////////////////////////////
1592 static inline Mat& operator &= (const Mat& a, const Mat& b)
1594 bitwise_and(a, b, (Mat&)a);
1598 static inline Mat& operator &= (const Mat& a, const Scalar& s)
1600 bitwise_and(a, s, (Mat&)a);
1604 template<typename _Tp> static inline Mat_<_Tp>&
1605 operator &= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1607 bitwise_and(a, b, (Mat&)a);
1608 return (Mat_<_Tp>&)a;
1611 template<typename _Tp> static inline Mat_<_Tp>&
1612 operator &= (const Mat_<_Tp>& a, const Scalar& s)
1614 bitwise_and(a, s, (Mat&)a);
1615 return (Mat_<_Tp>&)a;
1618 static inline Mat& operator |= (const Mat& a, const Mat& b)
1620 bitwise_or(a, b, (Mat&)a);
1624 static inline Mat& operator |= (const Mat& a, const Scalar& s)
1626 bitwise_or(a, s, (Mat&)a);
1630 template<typename _Tp> static inline Mat_<_Tp>&
1631 operator |= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1633 bitwise_or(a, b, (Mat&)a);
1634 return (Mat_<_Tp>&)a;
1637 template<typename _Tp> static inline Mat_<_Tp>&
1638 operator |= (const Mat_<_Tp>& a, const Scalar& s)
1640 bitwise_or(a, s, (Mat&)a);
1641 return (Mat_<_Tp>&)a;
1644 static inline Mat& operator ^= (const Mat& a, const Mat& b)
1646 bitwise_xor(a, b, (Mat&)a);
1650 static inline Mat& operator ^= (const Mat& a, const Scalar& s)
1652 bitwise_xor(a, s, (Mat&)a);
1656 template<typename _Tp> static inline Mat_<_Tp>&
1657 operator ^= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
1659 bitwise_xor(a, b, (Mat&)a);
1660 return (Mat_<_Tp>&)a;
1663 template<typename _Tp> static inline Mat_<_Tp>&
1664 operator ^= (const Mat_<_Tp>& a, const Scalar& s)
1666 bitwise_xor(a, s, (Mat&)a);
1667 return (Mat_<_Tp>&)a;
1670 /////////////////////////////// Miscellaneous operations //////////////////////////////
1672 template<typename _Tp> void split(const Mat& src, vector<Mat_<_Tp> >& mv)
1673 { split(src, (vector<Mat>&)mv ); }
1675 //////////////////////////////////////////////////////////////
1677 template<typename _Tp> inline MatExpr Mat_<_Tp>::zeros(int rows, int cols)
1679 return Mat::zeros(rows, cols, DataType<_Tp>::type);
1682 template<typename _Tp> inline MatExpr Mat_<_Tp>::zeros(Size sz)
1684 return Mat::zeros(sz, DataType<_Tp>::type);
1687 template<typename _Tp> inline MatExpr Mat_<_Tp>::ones(int rows, int cols)
1689 return Mat::ones(rows, cols, DataType<_Tp>::type);
1692 template<typename _Tp> inline MatExpr Mat_<_Tp>::ones(Size sz)
1694 return Mat::ones(sz, DataType<_Tp>::type);
1697 template<typename _Tp> inline MatExpr Mat_<_Tp>::eye(int rows, int cols)
1699 return Mat::eye(rows, cols, DataType<_Tp>::type);
1702 template<typename _Tp> inline MatExpr Mat_<_Tp>::eye(Size sz)
1704 return Mat::eye(sz, DataType<_Tp>::type);
1707 //////////////////////////////// Iterators & Comma initializers //////////////////////////////////
1709 inline MatConstIterator::MatConstIterator()
1710 : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0) {}
1712 inline MatConstIterator::MatConstIterator(const Mat* _m)
1713 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
1715 if( m && m->isContinuous() )
1717 sliceStart = m->data;
1718 sliceEnd = sliceStart + m->total()*elemSize;
1720 seek((const int*)0);
1723 inline MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
1724 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
1726 CV_Assert(m && m->dims <= 2);
1727 if( m->isContinuous() )
1729 sliceStart = m->data;
1730 sliceEnd = sliceStart + m->total()*elemSize;
1732 int idx[]={_row, _col};
1736 inline MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
1737 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
1739 CV_Assert(m && m->dims <= 2);
1740 if( m->isContinuous() )
1742 sliceStart = m->data;
1743 sliceEnd = sliceStart + m->total()*elemSize;
1745 int idx[]={_pt.y, _pt.x};
1749 inline MatConstIterator::MatConstIterator(const MatConstIterator& it)
1750 : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd)
1753 inline MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it )
1755 m = it.m; elemSize = it.elemSize; ptr = it.ptr;
1756 sliceStart = it.sliceStart; sliceEnd = it.sliceEnd;
1760 inline uchar* MatConstIterator::operator *() const { return ptr; }
1762 inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs)
1764 if( !m || ofs == 0 )
1766 ptrdiff_t ofsb = ofs*elemSize;
1768 if( ptr < sliceStart || sliceEnd <= ptr )
1776 inline MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs)
1777 { return (*this += -ofs); }
1779 inline MatConstIterator& MatConstIterator::operator --()
1781 if( m && (ptr -= elemSize) < sliceStart )
1789 inline MatConstIterator MatConstIterator::operator --(int)
1791 MatConstIterator b = *this;
1796 inline MatConstIterator& MatConstIterator::operator ++()
1798 if( m && (ptr += elemSize) >= sliceEnd )
1806 inline MatConstIterator MatConstIterator::operator ++(int)
1808 MatConstIterator b = *this;
1813 template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_() {}
1815 template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m)
1816 : MatConstIterator(_m) {}
1818 template<typename _Tp> inline MatConstIterator_<_Tp>::
1819 MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col)
1820 : MatConstIterator(_m, _row, _col) {}
1822 template<typename _Tp> inline MatConstIterator_<_Tp>::
1823 MatConstIterator_(const Mat_<_Tp>* _m, Point _pt)
1824 : MatConstIterator(_m, _pt) {}
1826 template<typename _Tp> inline MatConstIterator_<_Tp>::
1827 MatConstIterator_(const MatConstIterator_& it)
1828 : MatConstIterator(it) {}
1830 template<typename _Tp> inline MatConstIterator_<_Tp>&
1831 MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
1833 MatConstIterator::operator = (it);
1837 template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator *() const { return *(_Tp*)(this->ptr); }
1839 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs)
1841 MatConstIterator::operator += (ofs);
1845 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs)
1846 { return (*this += -ofs); }
1848 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
1850 MatConstIterator::operator --();
1854 template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
1856 MatConstIterator_ b = *this;
1857 MatConstIterator::operator --();
1861 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
1863 MatConstIterator::operator ++();
1867 template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
1869 MatConstIterator_ b = *this;
1870 MatConstIterator::operator ++();
1874 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_() : MatConstIterator_<_Tp>() {}
1876 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
1877 : MatConstIterator_<_Tp>(_m) {}
1879 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
1880 : MatConstIterator_<_Tp>(_m, _row, _col) {}
1882 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, Point _pt)
1883 : MatConstIterator_<_Tp>(_m, _pt) {}
1885 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, const int* _idx)
1886 : MatConstIterator_<_Tp>(_m, _idx) {}
1888 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
1889 : MatConstIterator_<_Tp>(it) {}
1891 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
1893 MatConstIterator::operator = (it);
1897 template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator *() const { return *(_Tp*)(this->ptr); }
1899 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs)
1901 MatConstIterator::operator += (ofs);
1905 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs)
1907 MatConstIterator::operator += (-ofs);
1911 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
1913 MatConstIterator::operator --();
1917 template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
1919 MatIterator_ b = *this;
1920 MatConstIterator::operator --();
1924 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
1926 MatConstIterator::operator ++();
1930 template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
1932 MatIterator_ b = *this;
1933 MatConstIterator::operator ++();
1937 template<typename _Tp> inline Point MatConstIterator_<_Tp>::pos() const
1941 CV_DbgAssert( m->dims <= 2 );
1942 if( m->isContinuous() )
1944 ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data;
1945 int y = (int)(ofs / m->cols), x = (int)(ofs - (ptrdiff_t)y*m->cols);
1950 ptrdiff_t ofs = (uchar*)ptr - m->data;
1951 int y = (int)(ofs / m->step), x = (int)((ofs - y*m->step)/sizeof(_Tp));
1957 operator == (const MatConstIterator& a, const MatConstIterator& b)
1958 { return a.m == b.m && a.ptr == b.ptr; }
1960 template<typename _Tp> static inline bool
1961 operator != (const MatConstIterator& a, const MatConstIterator& b)
1962 { return !(a == b); }
1964 template<typename _Tp> static inline bool
1965 operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
1966 { return a.m == b.m && a.ptr == b.ptr; }
1968 template<typename _Tp> static inline bool
1969 operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
1970 { return a.m != b.m || a.ptr != b.ptr; }
1972 template<typename _Tp> static inline bool
1973 operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
1974 { return a.m == b.m && a.ptr == b.ptr; }
1976 template<typename _Tp> static inline bool
1977 operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
1978 { return a.m != b.m || a.ptr != b.ptr; }
1981 operator < (const MatConstIterator& a, const MatConstIterator& b)
1982 { return a.ptr < b.ptr; }
1985 operator > (const MatConstIterator& a, const MatConstIterator& b)
1986 { return a.ptr > b.ptr; }
1989 operator <= (const MatConstIterator& a, const MatConstIterator& b)
1990 { return a.ptr <= b.ptr; }
1993 operator >= (const MatConstIterator& a, const MatConstIterator& b)
1994 { return a.ptr >= b.ptr; }
1996 CV_EXPORTS ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a);
1998 static inline MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs)
1999 { MatConstIterator b = a; return b += ofs; }
2001 static inline MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a)
2002 { MatConstIterator b = a; return b += ofs; }
2004 static inline MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs)
2005 { MatConstIterator b = a; return b += -ofs; }
2007 template<typename _Tp> static inline MatConstIterator_<_Tp>
2008 operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
2009 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; }
2011 template<typename _Tp> static inline MatConstIterator_<_Tp>
2012 operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a)
2013 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; }
2015 template<typename _Tp> static inline MatConstIterator_<_Tp>
2016 operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
2017 { MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatConstIterator_<_Tp>&)t; }
2019 inline uchar* MatConstIterator::operator [](ptrdiff_t i) const
2020 { return *(*this + i); }
2022 template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const
2023 { return *(_Tp*)MatConstIterator::operator [](i); }
2025 template<typename _Tp> static inline MatIterator_<_Tp>
2026 operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
2027 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; }
2029 template<typename _Tp> static inline MatIterator_<_Tp>
2030 operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a)
2031 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; }
2033 template<typename _Tp> static inline MatIterator_<_Tp>
2034 operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
2035 { MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatIterator_<_Tp>&)t; }
2037 template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const
2038 { return *(*this + i); }
2040 template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
2041 { return Mat::begin<_Tp>(); }
2043 template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::end() const
2044 { return Mat::end<_Tp>(); }
2046 template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::begin()
2047 { return Mat::begin<_Tp>(); }
2049 template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::end()
2050 { return Mat::end<_Tp>(); }
2052 template<typename _Tp> inline MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m) : it(_m) {}
2054 template<typename _Tp> template<typename T2> inline MatCommaInitializer_<_Tp>&
2055 MatCommaInitializer_<_Tp>::operator , (T2 v)
2057 CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() );
2058 *this->it = _Tp(v); ++this->it;
2062 template<typename _Tp> inline Mat_<_Tp> MatCommaInitializer_<_Tp>::operator *() const
2064 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
2065 return Mat_<_Tp>(*this->it.m);
2068 template<typename _Tp> inline MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
2070 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
2071 return Mat_<_Tp>(*this->it.m);
2074 template<typename _Tp, typename T2> static inline MatCommaInitializer_<_Tp>
2075 operator << (const Mat_<_Tp>& m, T2 val)
2077 MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
2078 return (commaInitializer, val);
2081 //////////////////////////////// SparseMat ////////////////////////////////
2083 inline SparseMat::SparseMat()
2084 : flags(MAGIC_VAL), hdr(0)
2088 inline SparseMat::SparseMat(int _dims, const int* _sizes, int _type)
2089 : flags(MAGIC_VAL), hdr(0)
2091 create(_dims, _sizes, _type);
2094 inline SparseMat::SparseMat(const SparseMat& m)
2095 : flags(m.flags), hdr(m.hdr)
2100 inline SparseMat::~SparseMat()
2105 inline SparseMat& SparseMat::operator = (const SparseMat& m)
2110 CV_XADD(&m.hdr->refcount, 1);
2118 inline SparseMat& SparseMat::operator = (const Mat& m)
2119 { return (*this = SparseMat(m)); }
2121 inline SparseMat SparseMat::clone() const
2129 inline void SparseMat::assignTo( SparseMat& m, int _type ) const
2134 convertTo(m, _type);
2137 inline void SparseMat::addref()
2138 { if( hdr ) CV_XADD(&hdr->refcount, 1); }
2140 inline void SparseMat::release()
2142 if( hdr && CV_XADD(&hdr->refcount, -1) == 1 )
2147 inline size_t SparseMat::elemSize() const
2148 { return CV_ELEM_SIZE(flags); }
2150 inline size_t SparseMat::elemSize1() const
2151 { return CV_ELEM_SIZE1(flags); }
2153 inline int SparseMat::type() const
2154 { return CV_MAT_TYPE(flags); }
2156 inline int SparseMat::depth() const
2157 { return CV_MAT_DEPTH(flags); }
2159 inline int SparseMat::channels() const
2160 { return CV_MAT_CN(flags); }
2162 inline const int* SparseMat::size() const
2164 return hdr ? hdr->size : 0;
2167 inline int SparseMat::size(int i) const
2171 CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
2172 return hdr->size[i];
2177 inline int SparseMat::dims() const
2179 return hdr ? hdr->dims : 0;
2182 inline size_t SparseMat::nzcount() const
2184 return hdr ? hdr->nodeCount : 0;
2187 inline size_t SparseMat::hash(int i0) const
2192 inline size_t SparseMat::hash(int i0, int i1) const
2194 return (size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1;
2197 inline size_t SparseMat::hash(int i0, int i1, int i2) const
2199 return ((size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1)*HASH_SCALE + (unsigned)i2;
2202 inline size_t SparseMat::hash(const int* idx) const
2204 size_t h = (unsigned)idx[0];
2207 int i, d = hdr->dims;
2208 for( i = 1; i < d; i++ )
2209 h = h*HASH_SCALE + (unsigned)idx[i];
2213 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, size_t* hashval)
2214 { return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval); }
2216 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
2217 { return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); }
2219 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
2220 { return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); }
2222 template<typename _Tp> inline _Tp& SparseMat::ref(const int* idx, size_t* hashval)
2223 { return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval); }
2225 template<typename _Tp> inline _Tp SparseMat::value(int i0, size_t* hashval) const
2227 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
2228 return p ? *p : _Tp();
2231 template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, size_t* hashval) const
2233 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
2234 return p ? *p : _Tp();
2237 template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
2239 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
2240 return p ? *p : _Tp();
2243 template<typename _Tp> inline _Tp SparseMat::value(const int* idx, size_t* hashval) const
2245 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
2246 return p ? *p : _Tp();
2249 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, size_t* hashval) const
2250 { return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval); }
2252 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
2253 { return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval); }
2255 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
2256 { return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval); }
2258 template<typename _Tp> inline const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
2259 { return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval); }
2261 template<typename _Tp> inline _Tp& SparseMat::value(Node* n)
2262 { return *(_Tp*)((uchar*)n + hdr->valueOffset); }
2264 template<typename _Tp> inline const _Tp& SparseMat::value(const Node* n) const
2265 { return *(const _Tp*)((const uchar*)n + hdr->valueOffset); }
2267 inline SparseMat::Node* SparseMat::node(size_t nidx)
2268 { return (Node*)(void*)&hdr->pool[nidx]; }
2270 inline const SparseMat::Node* SparseMat::node(size_t nidx) const
2271 { return (const Node*)(void*)&hdr->pool[nidx]; }
2273 inline SparseMatIterator SparseMat::begin()
2274 { return SparseMatIterator(this); }
2276 inline SparseMatConstIterator SparseMat::begin() const
2277 { return SparseMatConstIterator(this); }
2279 inline SparseMatIterator SparseMat::end()
2280 { SparseMatIterator it(this); it.seekEnd(); return it; }
2282 inline SparseMatConstIterator SparseMat::end() const
2283 { SparseMatConstIterator it(this); it.seekEnd(); return it; }
2285 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat::begin()
2286 { return SparseMatIterator_<_Tp>(this); }
2288 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat::begin() const
2289 { return SparseMatConstIterator_<_Tp>(this); }
2291 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat::end()
2292 { SparseMatIterator_<_Tp> it(this); it.seekEnd(); return it; }
2294 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat::end() const
2295 { SparseMatConstIterator_<_Tp> it(this); it.seekEnd(); return it; }
2298 inline SparseMatConstIterator::SparseMatConstIterator()
2299 : m(0), hashidx(0), ptr(0)
2303 inline SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
2304 : m(it.m), hashidx(it.hashidx), ptr(it.ptr)
2308 static inline bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
2309 { return it1.m == it2.m && it1.ptr == it2.ptr; }
2311 static inline bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
2312 { return !(it1 == it2); }
2315 inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
2320 hashidx = it.hashidx;
2326 template<typename _Tp> inline const _Tp& SparseMatConstIterator::value() const
2327 { return *(_Tp*)ptr; }
2329 inline const SparseMat::Node* SparseMatConstIterator::node() const
2331 return ptr && m && m->hdr ?
2332 (const SparseMat::Node*)(void*)(ptr - m->hdr->valueOffset) : 0;
2335 inline SparseMatConstIterator SparseMatConstIterator::operator ++(int)
2337 SparseMatConstIterator it = *this;
2343 inline void SparseMatConstIterator::seekEnd()
2347 hashidx = m->hdr->hashtab.size();
2352 inline SparseMatIterator::SparseMatIterator()
2355 inline SparseMatIterator::SparseMatIterator(SparseMat* _m)
2356 : SparseMatConstIterator(_m)
2359 inline SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
2360 : SparseMatConstIterator(it)
2364 inline SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
2366 (SparseMatConstIterator&)*this = it;
2370 template<typename _Tp> inline _Tp& SparseMatIterator::value() const
2371 { return *(_Tp*)ptr; }
2373 inline SparseMat::Node* SparseMatIterator::node() const
2375 return (SparseMat::Node*)SparseMatConstIterator::node();
2378 inline SparseMatIterator& SparseMatIterator::operator ++()
2380 SparseMatConstIterator::operator ++();
2384 inline SparseMatIterator SparseMatIterator::operator ++(int)
2386 SparseMatIterator it = *this;
2392 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_()
2393 { flags = MAGIC_VAL | DataType<_Tp>::type; }
2395 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes)
2396 : SparseMat(_dims, _sizes, DataType<_Tp>::type)
2399 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
2401 if( m.type() == DataType<_Tp>::type )
2402 *this = (const SparseMat_<_Tp>&)m;
2404 m.convertTo(*this, DataType<_Tp>::type);
2407 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
2409 this->flags = m.flags;
2412 CV_XADD(&this->hdr->refcount, 1);
2415 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Mat& m)
2421 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const CvSparseMat* m)
2427 template<typename _Tp> inline SparseMat_<_Tp>&
2428 SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
2432 if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
2440 template<typename _Tp> inline SparseMat_<_Tp>&
2441 SparseMat_<_Tp>::operator = (const SparseMat& m)
2443 if( m.type() == DataType<_Tp>::type )
2444 return (*this = (const SparseMat_<_Tp>&)m);
2445 m.convertTo(*this, DataType<_Tp>::type);
2449 template<typename _Tp> inline SparseMat_<_Tp>&
2450 SparseMat_<_Tp>::operator = (const Mat& m)
2451 { return (*this = SparseMat(m)); }
2453 template<typename _Tp> inline SparseMat_<_Tp>
2454 SparseMat_<_Tp>::clone() const
2461 template<typename _Tp> inline void
2462 SparseMat_<_Tp>::create(int _dims, const int* _sizes)
2464 SparseMat::create(_dims, _sizes, DataType<_Tp>::type);
2467 template<typename _Tp> inline
2468 SparseMat_<_Tp>::operator CvSparseMat*() const
2470 return SparseMat::operator CvSparseMat*();
2473 template<typename _Tp> inline int SparseMat_<_Tp>::type() const
2474 { return DataType<_Tp>::type; }
2476 template<typename _Tp> inline int SparseMat_<_Tp>::depth() const
2477 { return DataType<_Tp>::depth; }
2479 template<typename _Tp> inline int SparseMat_<_Tp>::channels() const
2480 { return DataType<_Tp>::channels; }
2482 template<typename _Tp> inline _Tp&
2483 SparseMat_<_Tp>::ref(int i0, size_t* hashval)
2484 { return SparseMat::ref<_Tp>(i0, hashval); }
2486 template<typename _Tp> inline _Tp
2487 SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const
2488 { return SparseMat::value<_Tp>(i0, hashval); }
2490 template<typename _Tp> inline _Tp&
2491 SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
2492 { return SparseMat::ref<_Tp>(i0, i1, hashval); }
2494 template<typename _Tp> inline _Tp
2495 SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
2496 { return SparseMat::value<_Tp>(i0, i1, hashval); }
2498 template<typename _Tp> inline _Tp&
2499 SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
2500 { return SparseMat::ref<_Tp>(i0, i1, i2, hashval); }
2502 template<typename _Tp> inline _Tp
2503 SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
2504 { return SparseMat::value<_Tp>(i0, i1, i2, hashval); }
2506 template<typename _Tp> inline _Tp&
2507 SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
2508 { return SparseMat::ref<_Tp>(idx, hashval); }
2510 template<typename _Tp> inline _Tp
2511 SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
2512 { return SparseMat::value<_Tp>(idx, hashval); }
2514 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
2515 { return SparseMatIterator_<_Tp>(this); }
2517 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
2518 { return SparseMatConstIterator_<_Tp>(this); }
2520 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
2521 { SparseMatIterator_<_Tp> it(this); it.seekEnd(); return it; }
2523 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
2524 { SparseMatConstIterator_<_Tp> it(this); it.seekEnd(); return it; }
2526 template<typename _Tp> inline
2527 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
2530 template<typename _Tp> inline
2531 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
2532 : SparseMatConstIterator(_m)
2535 template<typename _Tp> inline
2536 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat* _m)
2537 : SparseMatConstIterator(_m)
2539 CV_Assert( _m->type() == DataType<_Tp>::type );
2542 template<typename _Tp> inline
2543 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
2544 : SparseMatConstIterator(it)
2547 template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
2548 SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
2549 { return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
2550 (*reinterpret_cast<SparseMatConstIterator*>(this) =
2551 reinterpret_cast<const SparseMatConstIterator&>(it)); }
2553 template<typename _Tp> inline const _Tp&
2554 SparseMatConstIterator_<_Tp>::operator *() const
2555 { return *(const _Tp*)this->ptr; }
2557 template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
2558 SparseMatConstIterator_<_Tp>::operator ++()
2560 SparseMatConstIterator::operator ++();
2564 template<typename _Tp> inline SparseMatConstIterator_<_Tp>
2565 SparseMatConstIterator_<_Tp>::operator ++(int)
2567 SparseMatConstIterator it = *this;
2568 SparseMatConstIterator::operator ++();
2572 template<typename _Tp> inline
2573 SparseMatIterator_<_Tp>::SparseMatIterator_()
2576 template<typename _Tp> inline
2577 SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
2578 : SparseMatConstIterator_<_Tp>(_m)
2581 template<typename _Tp> inline
2582 SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat* _m)
2583 : SparseMatConstIterator_<_Tp>(_m)
2586 template<typename _Tp> inline
2587 SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
2588 : SparseMatConstIterator_<_Tp>(it)
2591 template<typename _Tp> inline SparseMatIterator_<_Tp>&
2592 SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
2593 { return reinterpret_cast<SparseMatIterator_<_Tp>&>
2594 (*reinterpret_cast<SparseMatConstIterator*>(this) =
2595 reinterpret_cast<const SparseMatConstIterator&>(it)); }
2597 template<typename _Tp> inline _Tp&
2598 SparseMatIterator_<_Tp>::operator *() const
2599 { return *(_Tp*)this->ptr; }
2601 template<typename _Tp> inline SparseMatIterator_<_Tp>&
2602 SparseMatIterator_<_Tp>::operator ++()
2604 SparseMatConstIterator::operator ++();
2608 template<typename _Tp> inline SparseMatIterator_<_Tp>
2609 SparseMatIterator_<_Tp>::operator ++(int)
2611 SparseMatIterator it = *this;
2612 SparseMatConstIterator::operator ++();