00001 /*M/////////////////////////////////////////////////////////////////////////////////////// 00002 // 00003 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 00004 // 00005 // By downloading, copying, installing or using the software you agree to this license. 00006 // If you do not agree to this license, do not download, install, 00007 // copy or use the software. 00008 // 00009 // 00010 // License Agreement 00011 // For Open Source Computer Vision Library 00012 // 00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 00015 // Third party copyrights are property of their respective owners. 00016 // 00017 // Redistribution and use in source and binary forms, with or without modification, 00018 // are permitted provided that the following conditions are met: 00019 // 00020 // * Redistribution's of source code must retain the above copyright notice, 00021 // this list of conditions and the following disclaimer. 00022 // 00023 // * Redistribution's in binary form must reproduce the above copyright notice, 00024 // this list of conditions and the following disclaimer in the documentation 00025 // and/or other materials provided with the distribution. 00026 // 00027 // * The name of the copyright holders may not be used to endorse or promote products 00028 // derived from this software without specific prior written permission. 00029 // 00030 // This software is provided by the copyright holders and contributors "as is" and 00031 // any express or implied warranties, including, but not limited to, the implied 00032 // warranties of merchantability and fitness for a particular purpose are disclaimed. 00033 // In no event shall the Intel Corporation or contributors be liable for any direct, 00034 // indirect, incidental, special, exemplary, or consequential damages 00035 // (including, but not limited to, procurement of substitute goods or services; 00036 // loss of use, data, or profits; or business interruption) however caused 00037 // and on any theory of liability, whether in contract, strict liability, 00038 // or tort (including negligence or otherwise) arising in any way out of 00039 // the use of this software, even if advised of the possibility of such damage. 00040 // 00041 //M*/ 00042 00043 #ifndef __OPENCV_CORE_MATRIX_OPERATIONS_HPP__ 00044 #define __OPENCV_CORE_MATRIX_OPERATIONS_HPP__ 00045 00046 #ifndef SKIP_INCLUDES 00047 #include <limits.h> 00048 #include <string.h> 00049 #endif // SKIP_INCLUDES 00050 00051 #ifdef __cplusplus 00052 00053 namespace cv 00054 { 00055 00057 00058 inline Mat::Mat() 00059 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00060 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00061 { 00062 } 00063 00064 inline Mat::Mat(int _rows, int _cols, int _type) 00065 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00066 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00067 { 00068 create(_rows, _cols, _type); 00069 } 00070 00071 inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s) 00072 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00073 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00074 { 00075 create(_rows, _cols, _type); 00076 *this = _s; 00077 } 00078 00079 inline Mat::Mat(Size _sz, int _type) 00080 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00081 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00082 { 00083 create( _sz.height, _sz.width, _type ); 00084 } 00085 00086 inline Mat::Mat(Size _sz, int _type, const Scalar& _s) 00087 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00088 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00089 { 00090 create(_sz.height, _sz.width, _type); 00091 *this = _s; 00092 } 00093 00094 inline Mat::Mat(int _dims, const int* _sz, int _type) 00095 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00096 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00097 { 00098 create(_dims, _sz, _type); 00099 } 00100 00101 inline Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s) 00102 : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), 00103 datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows) 00104 { 00105 create(_dims, _sz, _type); 00106 *this = _s; 00107 } 00108 00109 inline Mat::Mat(const Mat& m) 00110 : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data), 00111 refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), 00112 datalimit(m.datalimit), allocator(m.allocator), size(&rows) 00113 { 00114 if( refcount ) 00115 CV_XADD(refcount, 1); 00116 if( m.dims <= 2 ) 00117 { 00118 step[0] = m.step[0]; step[1] = m.step[1]; 00119 } 00120 else 00121 { 00122 dims = 0; 00123 copySize(m); 00124 } 00125 } 00126 00127 inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step) 00128 : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_rows), cols(_cols), 00129 data((uchar*)_data), refcount(0), datastart((uchar*)_data), dataend(0), 00130 datalimit(0), allocator(0), size(&rows) 00131 { 00132 size_t esz = CV_ELEM_SIZE(_type), minstep = cols*esz; 00133 if( _step == AUTO_STEP ) 00134 { 00135 _step = minstep; 00136 flags |= CONTINUOUS_FLAG; 00137 } 00138 else 00139 { 00140 if( rows == 1 ) _step = minstep; 00141 CV_DbgAssert( _step >= minstep ); 00142 flags |= _step == minstep ? CONTINUOUS_FLAG : 0; 00143 } 00144 step[0] = _step; step[1] = esz; 00145 datalimit = datastart + _step*rows; 00146 dataend = datalimit - _step + minstep; 00147 } 00148 00149 inline Mat::Mat(Size _sz, int _type, void* _data, size_t _step) 00150 : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_sz.height), cols(_sz.width), 00151 data((uchar*)_data), refcount(0), datastart((uchar*)_data), dataend(0), 00152 datalimit(0), allocator(0), size(&rows) 00153 { 00154 size_t esz = CV_ELEM_SIZE(_type), minstep = cols*esz; 00155 if( _step == AUTO_STEP ) 00156 { 00157 _step = minstep; 00158 flags |= CONTINUOUS_FLAG; 00159 } 00160 else 00161 { 00162 if( rows == 1 ) _step = minstep; 00163 CV_DbgAssert( _step >= minstep ); 00164 flags |= _step == minstep ? CONTINUOUS_FLAG : 0; 00165 } 00166 step[0] = _step; step[1] = esz; 00167 datalimit = datastart + _step*rows; 00168 dataend = datalimit - _step + minstep; 00169 } 00170 00171 00172 inline Mat::Mat(const CvMat* m, bool copyData) 00173 : flags(MAGIC_VAL + (m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG))), 00174 dims(2), rows(m->rows), cols(m->cols), data(m->data.ptr), refcount(0), 00175 datastart(m->data.ptr), allocator(0), size(&rows) 00176 { 00177 if( !copyData ) 00178 { 00179 size_t esz = CV_ELEM_SIZE(m->type), minstep = cols*esz, _step = m->step; 00180 if( _step == 0 ) 00181 _step = minstep; 00182 datalimit = datastart + _step*rows; 00183 dataend = datalimit - _step + minstep; 00184 step[0] = _step; step[1] = esz; 00185 } 00186 else 00187 { 00188 data = datastart = dataend = 0; 00189 Mat(m->rows, m->cols, m->type, m->data.ptr, m->step).copyTo(*this); 00190 } 00191 } 00192 00193 template<typename _Tp> inline Mat::Mat(const vector<_Tp>& vec, bool copyData) 00194 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), 00195 dims(2), rows((int)vec.size()), cols(1), data(0), refcount(0), 00196 datastart(0), dataend(0), allocator(0), size(&rows) 00197 { 00198 if(vec.empty()) 00199 return; 00200 if( !copyData ) 00201 { 00202 step[0] = step[1] = sizeof(_Tp); 00203 data = datastart = (uchar*)&vec[0]; 00204 datalimit = dataend = datastart + rows*step[0]; 00205 } 00206 else 00207 Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this); 00208 } 00209 00210 00211 template<typename _Tp, int n> inline Mat::Mat(const Vec<_Tp, n>& vec, bool copyData) 00212 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), 00213 dims(2), rows(n), cols(1), data(0), refcount(0), 00214 datastart(0), dataend(0), allocator(0), size(&rows) 00215 { 00216 if( !copyData ) 00217 { 00218 step[0] = step[1] = sizeof(_Tp); 00219 data = datastart = (uchar*)vec.val; 00220 datalimit = dataend = datastart + rows*step[0]; 00221 } 00222 else 00223 Mat(n, 1, DataType<_Tp>::type, (void*)vec.val).copyTo(*this); 00224 } 00225 00226 00227 template<typename _Tp, int m, int n> inline Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData) 00228 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), 00229 dims(2), rows(m), cols(n), data(0), refcount(0), 00230 datastart(0), dataend(0), allocator(0), size(&rows) 00231 { 00232 if( !copyData ) 00233 { 00234 step[0] = cols*sizeof(_Tp); 00235 step[1] = sizeof(_Tp); 00236 data = datastart = (uchar*)M.val; 00237 datalimit = dataend = datastart + rows*step[0]; 00238 } 00239 else 00240 Mat(m, n, DataType<_Tp>::type, (uchar*)M.val).copyTo(*this); 00241 } 00242 00243 00244 template<typename _Tp> inline Mat::Mat(const Point_<_Tp>& pt, bool copyData) 00245 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), 00246 dims(2), rows(2), cols(1), data(0), refcount(0), 00247 datastart(0), dataend(0), allocator(0), size(&rows) 00248 { 00249 if( !copyData ) 00250 { 00251 step[0] = step[1] = sizeof(_Tp); 00252 data = datastart = (uchar*)&pt.x; 00253 datalimit = dataend = datastart + rows*step[0]; 00254 } 00255 else 00256 { 00257 create(2, 1, DataType<_Tp>::type); 00258 ((_Tp*)data)[0] = pt.x; 00259 ((_Tp*)data)[1] = pt.y; 00260 } 00261 } 00262 00263 00264 template<typename _Tp> inline Mat::Mat(const Point3_<_Tp>& pt, bool copyData) 00265 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), 00266 dims(2), rows(3), cols(1), data(0), refcount(0), 00267 datastart(0), dataend(0), allocator(0), size(&rows) 00268 { 00269 if( !copyData ) 00270 { 00271 step[0] = step[1] = sizeof(_Tp); 00272 data = datastart = (uchar*)&pt.x; 00273 datalimit = dataend = datastart + rows*step[0]; 00274 } 00275 else 00276 { 00277 create(3, 1, DataType<_Tp>::type); 00278 ((_Tp*)data)[0] = pt.x; 00279 ((_Tp*)data)[1] = pt.y; 00280 ((_Tp*)data)[2] = pt.z; 00281 } 00282 } 00283 00284 00285 template<typename _Tp> inline Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer) 00286 : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), 00287 dims(0), rows(0), cols(0), data(0), refcount(0), 00288 datastart(0), dataend(0), allocator(0), size(&rows) 00289 { 00290 *this = *commaInitializer; 00291 } 00292 00293 inline Mat::~Mat() 00294 { 00295 release(); 00296 if( step.p != step.buf ) 00297 fastFree(step.p); 00298 } 00299 00300 inline Mat& Mat::operator = (const Mat& m) 00301 { 00302 if( this != &m ) 00303 { 00304 if( m.refcount ) 00305 CV_XADD(m.refcount, 1); 00306 release(); 00307 flags = m.flags; 00308 if( dims <= 2 && m.dims <= 2 ) 00309 { 00310 dims = m.dims; 00311 rows = m.rows; 00312 cols = m.cols; 00313 step[0] = m.step[0]; 00314 step[1] = m.step[1]; 00315 } 00316 else 00317 copySize(m); 00318 data = m.data; 00319 datastart = m.datastart; 00320 dataend = m.dataend; 00321 datalimit = m.datalimit; 00322 refcount = m.refcount; 00323 allocator = m.allocator; 00324 } 00325 return *this; 00326 } 00327 00328 inline Mat Mat::row(int y) const { return Mat(*this, Range(y, y+1), Range::all()); } 00329 inline Mat Mat::col(int x) const { return Mat(*this, Range::all(), Range(x, x+1)); } 00330 inline Mat Mat::rowRange(int startrow, int endrow) const 00331 { return Mat(*this, Range(startrow, endrow), Range::all()); } 00332 inline Mat Mat::rowRange(const Range& r) const 00333 { return Mat(*this, r, Range::all()); } 00334 inline Mat Mat::colRange(int startcol, int endcol) const 00335 { return Mat(*this, Range::all(), Range(startcol, endcol)); } 00336 inline Mat Mat::colRange(const Range& r) const 00337 { return Mat(*this, Range::all(), r); } 00338 00339 inline Mat Mat::diag(const Mat& d) 00340 { 00341 Mat m(d.rows, d.rows, d.type(), Scalar(0)), md = m.diag(); 00342 d.copyTo(md); 00343 return m; 00344 } 00345 00346 inline Mat Mat::clone() const 00347 { 00348 Mat m; 00349 copyTo(m); 00350 return m; 00351 } 00352 00353 inline void Mat::assignTo( Mat& m, int type ) const 00354 { 00355 if( type < 0 ) 00356 m = *this; 00357 else 00358 convertTo(m, type); 00359 } 00360 00361 inline void Mat::create(int _rows, int _cols, int _type) 00362 { 00363 _type &= TYPE_MASK; 00364 if( dims <= 2 && rows == _rows && cols == _cols && type() == _type && data ) 00365 return; 00366 int sz[] = {_rows, _cols}; 00367 create(2, sz, _type); 00368 } 00369 00370 inline void Mat::create(Size _sz, int _type) 00371 { 00372 create(_sz.height, _sz.width, _type); 00373 } 00374 00375 inline void Mat::addref() 00376 { if( refcount ) CV_XADD(refcount, 1); } 00377 00378 inline void Mat::release() 00379 { 00380 if( refcount && CV_XADD(refcount, -1) == 1 ) 00381 deallocate(); 00382 data = datastart = dataend = datalimit = 0; 00383 size.p[0] = 0; 00384 refcount = 0; 00385 } 00386 00387 inline Mat Mat::operator()( Range rowRange, Range colRange ) const 00388 { 00389 return Mat(*this, rowRange, colRange); 00390 } 00391 00392 inline Mat Mat::operator()( const Rect& roi ) const 00393 { return Mat(*this, roi); } 00394 00395 inline Mat Mat::operator()(const Range* ranges) const 00396 { 00397 return Mat(*this, ranges); 00398 } 00399 00400 inline Mat::operator CvMat() const 00401 { 00402 CV_DbgAssert(dims <= 2); 00403 CvMat m = cvMat(rows, dims == 1 ? 1 : cols, type(), data); 00404 m.step = (int)step[0]; 00405 m.type = (m.type & ~CONTINUOUS_FLAG) | (flags & CONTINUOUS_FLAG); 00406 return m; 00407 } 00408 00409 inline bool Mat::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; } 00410 inline bool Mat::isSubmatrix() const { return (flags & SUBMATRIX_FLAG) != 0; } 00411 inline size_t Mat::elemSize() const { return dims > 0 ? step.p[dims-1] : 0; } 00412 inline size_t Mat::elemSize1() const { return CV_ELEM_SIZE1(flags); } 00413 inline int Mat::type() const { return CV_MAT_TYPE(flags); } 00414 inline int Mat::depth() const { return CV_MAT_DEPTH(flags); } 00415 inline int Mat::channels() const { return CV_MAT_CN(flags); } 00416 inline size_t Mat::step1(int i) const { return step.p[i]/elemSize1(); } 00417 inline bool Mat::empty() const { return data == 0 || total() == 0; } 00418 inline size_t Mat::total() const 00419 { 00420 if( dims <= 2 ) 00421 return rows*cols; 00422 size_t p = 1; 00423 for( int i = 0; i < dims; i++ ) 00424 p *= size[i]; 00425 return p; 00426 } 00427 00428 inline uchar* Mat::ptr(int y) 00429 { 00430 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) ); 00431 return data + step.p[0]*y; 00432 } 00433 00434 inline const uchar* Mat::ptr(int y) const 00435 { 00436 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) ); 00437 return data + step.p[0]*y; 00438 } 00439 00440 template<typename _Tp> inline _Tp* Mat::ptr(int y) 00441 { 00442 CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) ); 00443 return (_Tp*)(data + step.p[0]*y); 00444 } 00445 00446 template<typename _Tp> inline const _Tp* Mat::ptr(int y) const 00447 { 00448 CV_DbgAssert( y == 0 || (data && dims >= 1 && data && (unsigned)y < (unsigned)size.p[0]) ); 00449 return (const _Tp*)(data + step.p[0]*y); 00450 } 00451 00452 00453 inline uchar* Mat::ptr(int i0, int i1) 00454 { 00455 CV_DbgAssert( dims >= 2 && data && 00456 (unsigned)i0 < (unsigned)size.p[0] && 00457 (unsigned)i1 < (unsigned)size.p[1] ); 00458 return data + i0*step.p[0] + i1*step.p[1]; 00459 } 00460 00461 inline const uchar* Mat::ptr(int i0, int i1) const 00462 { 00463 CV_DbgAssert( dims >= 2 && data && 00464 (unsigned)i0 < (unsigned)size.p[0] && 00465 (unsigned)i1 < (unsigned)size.p[1] ); 00466 return data + i0*step.p[0] + i1*step.p[1]; 00467 } 00468 00469 template<typename _Tp> inline _Tp* Mat::ptr(int i0, int i1) 00470 { 00471 CV_DbgAssert( dims >= 2 && data && 00472 (unsigned)i0 < (unsigned)size.p[0] && 00473 (unsigned)i1 < (unsigned)size.p[1] ); 00474 return (_Tp*)(data + i0*step.p[0] + i1*step.p[1]); 00475 } 00476 00477 template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1) const 00478 { 00479 CV_DbgAssert( dims >= 2 && data && 00480 (unsigned)i0 < (unsigned)size.p[0] && 00481 (unsigned)i1 < (unsigned)size.p[1] ); 00482 return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1]); 00483 } 00484 00485 inline uchar* Mat::ptr(int i0, int i1, int i2) 00486 { 00487 CV_DbgAssert( dims >= 3 && data && 00488 (unsigned)i0 < (unsigned)size.p[0] && 00489 (unsigned)i1 < (unsigned)size.p[1] && 00490 (unsigned)i2 < (unsigned)size.p[2] ); 00491 return data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]; 00492 } 00493 00494 inline const uchar* Mat::ptr(int i0, int i1, int i2) const 00495 { 00496 CV_DbgAssert( dims >= 3 && data && 00497 (unsigned)i0 < (unsigned)size.p[0] && 00498 (unsigned)i1 < (unsigned)size.p[1] && 00499 (unsigned)i2 < (unsigned)size.p[2] ); 00500 return data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]; 00501 } 00502 00503 template<typename _Tp> inline _Tp* Mat::ptr(int i0, int i1, int i2) 00504 { 00505 CV_DbgAssert( dims >= 3 && data && 00506 (unsigned)i0 < (unsigned)size.p[0] && 00507 (unsigned)i1 < (unsigned)size.p[1] && 00508 (unsigned)i2 < (unsigned)size.p[2] ); 00509 return (_Tp*)(data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]); 00510 } 00511 00512 template<typename _Tp> inline const _Tp* Mat::ptr(int i0, int i1, int i2) const 00513 { 00514 CV_DbgAssert( dims >= 3 && data && 00515 (unsigned)i0 < (unsigned)size.p[0] && 00516 (unsigned)i1 < (unsigned)size.p[1] && 00517 (unsigned)i2 < (unsigned)size.p[2] ); 00518 return (const _Tp*)(data + i0*step.p[0] + i1*step.p[1] + i2*step.p[2]); 00519 } 00520 00521 inline uchar* Mat::ptr(const int* idx) 00522 { 00523 int i, d = dims; 00524 uchar* p = data; 00525 CV_DbgAssert( d >= 1 && p ); 00526 for( i = 0; i < d; i++ ) 00527 { 00528 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] ); 00529 p += idx[i]*step.p[i]; 00530 } 00531 return p; 00532 } 00533 00534 inline const uchar* Mat::ptr(const int* idx) const 00535 { 00536 int i, d = dims; 00537 uchar* p = data; 00538 CV_DbgAssert( d >= 1 && p ); 00539 for( i = 0; i < d; i++ ) 00540 { 00541 CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] ); 00542 p += idx[i]*step.p[i]; 00543 } 00544 return p; 00545 } 00546 00547 template<typename _Tp> inline _Tp& Mat::at(int i0, int i1) 00548 { 00549 CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && 00550 (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && 00551 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1()); 00552 return ((_Tp*)(data + step.p[0]*i0))[i1]; 00553 } 00554 00555 template<typename _Tp> inline const _Tp& Mat::at(int i0, int i1) const 00556 { 00557 CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && 00558 (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && 00559 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1()); 00560 return ((const _Tp*)(data + step.p[0]*i0))[i1]; 00561 } 00562 00563 template<typename _Tp> inline _Tp& Mat::at(Point pt) 00564 { 00565 CV_DbgAssert( dims <= 2 && data && (unsigned)pt.y < (unsigned)size.p[0] && 00566 (unsigned)(pt.x*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && 00567 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1()); 00568 return ((_Tp*)(data + step.p[0]*pt.y))[pt.x]; 00569 } 00570 00571 template<typename _Tp> inline const _Tp& Mat::at(Point pt) const 00572 { 00573 CV_DbgAssert( dims <= 2 && data && (unsigned)pt.y < (unsigned)size.p[0] && 00574 (unsigned)(pt.x*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && 00575 CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1()); 00576 return ((const _Tp*)(data + step.p[0]*pt.y))[pt.x]; 00577 } 00578 00579 template<typename _Tp> inline _Tp& Mat::at(int i0) 00580 { 00581 CV_DbgAssert( dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) && 00582 (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && 00583 elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 00584 return *(_Tp*)(data + step.p[size.p[0]==1]*i0); 00585 } 00586 00587 template<typename _Tp> inline const _Tp& Mat::at(int i0) const 00588 { 00589 CV_DbgAssert( dims <= 2 && data && (size.p[0] == 1 || size.p[1] == 1) && 00590 (unsigned)i0 < (unsigned)(size.p[0] + size.p[1] - 1) && 00591 elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 00592 return *(_Tp*)(data + step.p[size.p[0]==1]*i0); 00593 } 00594 00595 template<typename _Tp> inline _Tp& Mat::at(int i0, int i1, int i2) 00596 { 00597 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 00598 return *(_Tp*)ptr(i0, i1, i2); 00599 } 00600 template<typename _Tp> inline const _Tp& Mat::at(int i0, int i1, int i2) const 00601 { 00602 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 00603 return *(const _Tp*)ptr(i0, i1, i2); 00604 } 00605 template<typename _Tp> inline _Tp& Mat::at(const int* idx) 00606 { 00607 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 00608 return *(_Tp*)ptr(idx); 00609 } 00610 template<typename _Tp> inline const _Tp& Mat::at(const int* idx) const 00611 { 00612 CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) ); 00613 return *(const _Tp*)ptr(idx); 00614 } 00615 00616 00617 template<typename _Tp> inline MatConstIterator_<_Tp> Mat::begin() const 00618 { 00619 CV_DbgAssert( elemSize() == sizeof(_Tp) ); 00620 return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this); 00621 } 00622 00623 template<typename _Tp> inline MatConstIterator_<_Tp> Mat::end() const 00624 { 00625 CV_DbgAssert( elemSize() == sizeof(_Tp) ); 00626 MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this); 00627 it += total(); 00628 return it; 00629 } 00630 00631 template<typename _Tp> inline MatIterator_<_Tp> Mat::begin() 00632 { 00633 CV_DbgAssert( elemSize() == sizeof(_Tp) ); 00634 return MatIterator_<_Tp>((Mat_<_Tp>*)this); 00635 } 00636 00637 template<typename _Tp> inline MatIterator_<_Tp> Mat::end() 00638 { 00639 CV_DbgAssert( elemSize() == sizeof(_Tp) ); 00640 MatIterator_<_Tp> it((Mat_<_Tp>*)this); 00641 it += total(); 00642 return it; 00643 } 00644 00645 template<typename _Tp> inline Mat::operator vector<_Tp>() const 00646 { 00647 vector<_Tp> v; 00648 copyTo(v); 00649 return v; 00650 } 00651 00652 template<typename _Tp, int n> inline Mat::operator Vec<_Tp, n>() const 00653 { 00654 CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) && 00655 rows + cols - 1 == n && channels() == 1 ); 00656 00657 if( isContinuous() && type() == DataType<_Tp>::type ) 00658 return Vec<_Tp, n>((_Tp*)data); 00659 Vec<_Tp, n> v; Mat tmp(rows, cols, DataType<_Tp>::type, v.val); 00660 convertTo(tmp, tmp.type()); 00661 return v; 00662 } 00663 00664 template<typename _Tp, int m, int n> inline Mat::operator Matx<_Tp, m, n>() const 00665 { 00666 CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 ); 00667 00668 if( isContinuous() && type() == DataType<_Tp>::type ) 00669 return Matx<_Tp, m, n>((_Tp*)data); 00670 Matx<_Tp, m, n> mtx; Mat tmp(rows, cols, DataType<_Tp>::type, mtx.val); 00671 convertTo(tmp, tmp.type()); 00672 return mtx; 00673 } 00674 00675 00676 template<typename _Tp> inline void Mat::push_back(const _Tp& elem) 00677 { 00678 CV_Assert(DataType<_Tp>::type == type() && cols == 1 00679 /* && dims == 2 (cols == 1 implies dims == 2) */); 00680 uchar* tmp = dataend + step[0]; 00681 if( !isSubmatrix() && isContinuous() && tmp <= datalimit ) 00682 { 00683 *(_Tp*)(data + (size.p[0]++)*step.p[0]) = elem; 00684 dataend = tmp; 00685 } 00686 else 00687 push_back_(&elem); 00688 } 00689 00690 template<typename _Tp> inline void Mat::push_back(const Mat_<_Tp>& m) 00691 { 00692 push_back((const Mat&)m); 00693 } 00694 00695 inline Mat::MSize::MSize(int* _p) : p(_p) {} 00696 inline Size Mat::MSize::operator()() const 00697 { 00698 CV_DbgAssert(p[-1] <= 2); 00699 return Size(p[1], p[0]); 00700 } 00701 inline const int& Mat::MSize::operator[](int i) const { return p[i]; } 00702 inline int& Mat::MSize::operator[](int i) { return p[i]; } 00703 inline Mat::MSize::operator const int*() const { return p; } 00704 00705 inline bool Mat::MSize::operator == (const MSize& sz) const 00706 { 00707 int d = p[-1], dsz = sz.p[-1]; 00708 if( d != dsz ) 00709 return false; 00710 if( d == 2 ) 00711 return p[0] == sz.p[0] && p[1] == sz.p[1]; 00712 00713 for( int i = 0; i < d; i++ ) 00714 if( p[i] != sz.p[i] ) 00715 return false; 00716 return true; 00717 } 00718 00719 inline bool Mat::MSize::operator != (const MSize& sz) const 00720 { 00721 return !(*this == sz); 00722 } 00723 00724 inline Mat::MStep::MStep() { p = buf; p[0] = p[1] = 0; } 00725 inline Mat::MStep::MStep(size_t s) { p = buf; p[0] = s; p[1] = 0; } 00726 inline const size_t& Mat::MStep::operator[](int i) const { return p[i]; } 00727 inline size_t& Mat::MStep::operator[](int i) { return p[i]; } 00728 inline Mat::MStep::operator size_t() const 00729 { 00730 CV_DbgAssert( p == buf ); 00731 return buf[0]; 00732 } 00733 inline Mat::MStep& Mat::MStep::operator = (size_t s) 00734 { 00735 CV_DbgAssert( p == buf ); 00736 buf[0] = s; 00737 return *this; 00738 } 00739 00740 static inline Mat cvarrToMatND(const CvArr* arr, bool copyData=false, int coiMode=0) 00741 { 00742 return cvarrToMat(arr, copyData, true, coiMode); 00743 } 00744 00746 00747 inline SVD::SVD() {} 00748 inline SVD::SVD( InputArray m, int flags ) { operator ()(m, flags); } 00749 inline void SVD::solveZ( InputArray m, OutputArray _dst ) 00750 { 00751 SVD svd(m); 00752 _dst.create(svd.vt.cols, 1, svd.vt.type()); 00753 Mat dst = _dst.getMat(); 00754 svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst); 00755 } 00756 00757 template<typename _Tp, int m, int n, int nm> inline void 00758 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w, Matx<_Tp, m, nm>& u, Matx<_Tp, n, nm>& vt ) 00759 { 00760 assert( nm == MIN(m, n)); 00761 Mat _a(a, false), _u(u, false), _w(w, false), _vt(vt, false); 00762 SVD::compute(_a, _w, _u, _vt); 00763 CV_Assert(_w.data == (uchar*)&w.val[0] && _u.data == (uchar*)&u.val[0] && _vt.data == (uchar*)&vt.val[0]); 00764 } 00765 00766 template<typename _Tp, int m, int n, int nm> inline void 00767 SVD::compute( const Matx<_Tp, m, n>& a, Matx<_Tp, nm, 1>& w ) 00768 { 00769 assert( nm == MIN(m, n)); 00770 Mat _a(a, false), _w(w, false); 00771 SVD::compute(_a, _w); 00772 CV_Assert(_w.data == (uchar*)&w.val[0]); 00773 } 00774 00775 template<typename _Tp, int m, int n, int nm, int nb> inline void 00776 SVD::backSubst( const Matx<_Tp, nm, 1>& w, const Matx<_Tp, m, nm>& u, 00777 const Matx<_Tp, n, nm>& vt, const Matx<_Tp, m, nb>& rhs, 00778 Matx<_Tp, n, nb>& dst ) 00779 { 00780 assert( nm == MIN(m, n)); 00781 Mat _u(u, false), _w(w, false), _vt(vt, false), _rhs(rhs, false), _dst(dst, false); 00782 SVD::backSubst(_w, _u, _vt, _rhs, _dst); 00783 CV_Assert(_dst.data == (uchar*)&dst.val[0]); 00784 } 00785 00787 00788 template<typename _Tp> inline Mat_<_Tp>::Mat_() 00789 : Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; } 00790 00791 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols) 00792 : Mat(_rows, _cols, DataType<_Tp>::type) {} 00793 00794 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value) 00795 : Mat(_rows, _cols, DataType<_Tp>::type) { *this = value; } 00796 00797 template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _sz) 00798 : Mat(_sz.height, _sz.width, DataType<_Tp>::type) {} 00799 00800 template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _sz, const _Tp& value) 00801 : Mat(_sz.height, _sz.width, DataType<_Tp>::type) { *this = value; } 00802 00803 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _dims, const int* _sz) 00804 : Mat(_dims, _sz, DataType<_Tp>::type) {} 00805 00806 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s) 00807 : Mat(_dims, _sz, DataType<_Tp>::type, Scalar(_s)) {} 00808 00809 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges) 00810 : Mat(m, ranges) {} 00811 00812 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat& m) 00813 : Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; *this = m; } 00814 00815 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m) 00816 : Mat(m) {} 00817 00818 template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps) 00819 : Mat(_rows, _cols, DataType<_Tp>::type, _data, steps) {} 00820 00821 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Range& rowRange, const Range& colRange) 00822 : Mat(m, rowRange, colRange) {} 00823 00824 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi) 00825 : Mat(m, roi) {} 00826 00827 template<typename _Tp> template<int n> inline 00828 Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData) 00829 : Mat(n/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&vec) 00830 { 00831 CV_Assert(n%DataType<_Tp>::channels == 0); 00832 if( copyData ) 00833 *this = clone(); 00834 } 00835 00836 template<typename _Tp> template<int m, int n> inline 00837 Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type,m,n>& M, bool copyData) 00838 : Mat(m, n/DataType<_Tp>::channels, DataType<_Tp>::type, (void*)&M) 00839 { 00840 CV_Assert(n % DataType<_Tp>::channels == 0); 00841 if( copyData ) 00842 *this = clone(); 00843 } 00844 00845 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData) 00846 : Mat(2/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt) 00847 { 00848 CV_Assert(2 % DataType<_Tp>::channels == 0); 00849 if( copyData ) 00850 *this = clone(); 00851 } 00852 00853 template<typename _Tp> inline Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData) 00854 : Mat(3/DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt) 00855 { 00856 CV_Assert(3 % DataType<_Tp>::channels == 0); 00857 if( copyData ) 00858 *this = clone(); 00859 } 00860 00861 template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer) 00862 : Mat(commaInitializer) {} 00863 00864 template<typename _Tp> inline Mat_<_Tp>::Mat_(const vector<_Tp>& vec, bool copyData) 00865 : Mat(vec, copyData) {} 00866 00867 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m) 00868 { 00869 if( DataType<_Tp>::type == m.type() ) 00870 { 00871 Mat::operator = (m); 00872 return *this; 00873 } 00874 if( DataType<_Tp>::depth == m.depth() ) 00875 { 00876 return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0)); 00877 } 00878 CV_DbgAssert(DataType<_Tp>::channels == m.channels()); 00879 m.convertTo(*this, type()); 00880 return *this; 00881 } 00882 00883 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m) 00884 { 00885 Mat::operator=(m); 00886 return *this; 00887 } 00888 00889 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s) 00890 { 00891 typedef typename DataType<_Tp>::vec_type VT; 00892 Mat::operator=(Scalar((const VT&)s)); 00893 return *this; 00894 } 00895 00896 template<typename _Tp> inline void Mat_<_Tp>::create(int _rows, int _cols) 00897 { 00898 Mat::create(_rows, _cols, DataType<_Tp>::type); 00899 } 00900 00901 template<typename _Tp> inline void Mat_<_Tp>::create(Size _sz) 00902 { 00903 Mat::create(_sz, DataType<_Tp>::type); 00904 } 00905 00906 template<typename _Tp> inline void Mat_<_Tp>::create(int _dims, const int* _sz) 00907 { 00908 Mat::create(_dims, _sz, DataType<_Tp>::type); 00909 } 00910 00911 00912 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const 00913 { return Mat_<_Tp>(Mat::cross(m)); } 00914 00915 template<typename _Tp> template<typename T2> inline Mat_<_Tp>::operator Mat_<T2>() const 00916 { return Mat_<T2>(*this); } 00917 00918 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::row(int y) const 00919 { return Mat_(*this, Range(y, y+1), Range::all()); } 00920 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::col(int x) const 00921 { return Mat_(*this, Range::all(), Range(x, x+1)); } 00922 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::diag(int d) const 00923 { return Mat_(Mat::diag(d)); } 00924 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::clone() const 00925 { return Mat_(Mat::clone()); } 00926 00927 template<typename _Tp> inline size_t Mat_<_Tp>::elemSize() const 00928 { 00929 CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) ); 00930 return sizeof(_Tp); 00931 } 00932 00933 template<typename _Tp> inline size_t Mat_<_Tp>::elemSize1() const 00934 { 00935 CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp)/DataType<_Tp>::channels ); 00936 return sizeof(_Tp)/DataType<_Tp>::channels; 00937 } 00938 template<typename _Tp> inline int Mat_<_Tp>::type() const 00939 { 00940 CV_DbgAssert( Mat::type() == DataType<_Tp>::type ); 00941 return DataType<_Tp>::type; 00942 } 00943 template<typename _Tp> inline int Mat_<_Tp>::depth() const 00944 { 00945 CV_DbgAssert( Mat::depth() == DataType<_Tp>::depth ); 00946 return DataType<_Tp>::depth; 00947 } 00948 template<typename _Tp> inline int Mat_<_Tp>::channels() const 00949 { 00950 CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels ); 00951 return DataType<_Tp>::channels; 00952 } 00953 template<typename _Tp> inline size_t Mat_<_Tp>::stepT(int i) const { return step.p[i]/elemSize(); } 00954 template<typename _Tp> inline size_t Mat_<_Tp>::step1(int i) const { return step.p[i]/elemSize1(); } 00955 00956 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::reshape(int _rows) const 00957 { return Mat_<_Tp>(Mat::reshape(0,_rows)); } 00958 00959 template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright ) 00960 { return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright)); } 00961 00962 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range& rowRange, const Range& colRange ) const 00963 { return Mat_<_Tp>(*this, rowRange, colRange); } 00964 00965 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const 00966 { return Mat_<_Tp>(*this, roi); } 00967 00968 template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const 00969 { return Mat_<_Tp>(*this, ranges); } 00970 00971 template<typename _Tp> inline _Tp* Mat_<_Tp>::operator [](int y) 00972 { return (_Tp*)ptr(y); } 00973 template<typename _Tp> inline const _Tp* Mat_<_Tp>::operator [](int y) const 00974 { return (const _Tp*)ptr(y); } 00975 00976 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0, int i1) 00977 { 00978 CV_DbgAssert( dims <= 2 && data && 00979 (unsigned)i0 < (unsigned)size.p[0] && 00980 (unsigned)i1 < (unsigned)size.p[1] && 00981 type() == DataType<_Tp>::type ); 00982 return ((_Tp*)(data + step.p[0]*i0))[i1]; 00983 } 00984 00985 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const 00986 { 00987 CV_DbgAssert( dims <= 2 && data && 00988 (unsigned)i0 < (unsigned)size.p[0] && 00989 (unsigned)i1 < (unsigned)size.p[1] && 00990 type() == DataType<_Tp>::type ); 00991 return ((const _Tp*)(data + step.p[0]*i0))[i1]; 00992 } 00993 00994 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(Point pt) 00995 { 00996 CV_DbgAssert( dims <= 2 && data && 00997 (unsigned)pt.y < (unsigned)size.p[0] && 00998 (unsigned)pt.x < (unsigned)size.p[1] && 00999 type() == DataType<_Tp>::type ); 01000 return ((_Tp*)(data + step.p[0]*pt.y))[pt.x]; 01001 } 01002 01003 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(Point pt) const 01004 { 01005 CV_DbgAssert( dims <= 2 && data && 01006 (unsigned)pt.y < (unsigned)size.p[0] && 01007 (unsigned)pt.x < (unsigned)size.p[1] && 01008 type() == DataType<_Tp>::type ); 01009 return ((const _Tp*)(data + step.p[0]*pt.y))[pt.x]; 01010 } 01011 01012 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(const int* idx) 01013 { 01014 return Mat::at<_Tp>(idx); 01015 } 01016 01017 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(const int* idx) const 01018 { 01019 return Mat::at<_Tp>(idx); 01020 } 01021 01022 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0) 01023 { 01024 return this->at<_Tp>(i0); 01025 } 01026 01027 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0) const 01028 { 01029 return this->at<_Tp>(i0); 01030 } 01031 01032 template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) 01033 { 01034 return this->at<_Tp>(i0, i1, i2); 01035 } 01036 01037 template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const 01038 { 01039 return this->at<_Tp>(i0, i1, i2); 01040 } 01041 01042 01043 template<typename _Tp> inline Mat_<_Tp>::operator vector<_Tp>() const 01044 { 01045 vector<_Tp> v; 01046 copyTo(v); 01047 return v; 01048 } 01049 01050 template<typename _Tp> template<int n> inline Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const 01051 { 01052 CV_Assert(n % DataType<_Tp>::channels == 0); 01053 return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>(); 01054 } 01055 01056 template<typename _Tp> template<int m, int n> inline Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const 01057 { 01058 CV_Assert(n % DataType<_Tp>::channels == 0); 01059 return this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>(); 01060 } 01061 01062 template<typename T1, typename T2, typename Op> inline void 01063 process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op ) 01064 { 01065 int y, x, rows = m1.rows, cols = m1.cols; 01066 int c1 = m1.channels(), c2 = m2.channels(); 01067 01068 CV_DbgAssert( m1.size() == m2.size() ); 01069 01070 for( y = 0; y < rows; y++ ) 01071 { 01072 const T1* src = m1[y]; 01073 T2* dst = m2[y]; 01074 01075 for( x = 0; x < cols; x++ ) 01076 dst[x] = op(src[x]); 01077 } 01078 } 01079 01080 template<typename T1, typename T2, typename T3, typename Op> inline void 01081 process( const Mat_<T1>& m1, const Mat_<T2>& m2, Mat_<T3>& m3, Op op ) 01082 { 01083 int y, x, rows = m1.rows, cols = m1.cols; 01084 01085 CV_DbgAssert( m1.size() == m2.size() ); 01086 01087 for( y = 0; y < rows; y++ ) 01088 { 01089 const T1* src1 = m1[y]; 01090 const T2* src2 = m2[y]; 01091 T3* dst = m3[y]; 01092 01093 for( x = 0; x < cols; x++ ) 01094 dst[x] = op( src1[x], src2[x] ); 01095 } 01096 } 01097 01098 01100 01101 template<typename _Tp> _InputArray::_InputArray(const vector<_Tp>& vec) 01102 : flags(STD_VECTOR + DataType<_Tp>::type), obj((void*)&vec) {} 01103 01104 template<typename _Tp> _InputArray::_InputArray(const vector<vector<_Tp> >& vec) 01105 : flags(STD_VECTOR_VECTOR + DataType<_Tp>::type), obj((void*)&vec) {} 01106 01107 template<typename _Tp, int m, int n> _InputArray::_InputArray(const Matx<_Tp, m, n>& mtx) 01108 : flags(MATX + DataType<_Tp>::type), obj((void*)&mtx), sz(n, m) {} 01109 01110 template<typename _Tp> _OutputArray::_OutputArray(vector<_Tp>& vec) : _InputArray(vec) {} 01111 template<typename _Tp> _OutputArray::_OutputArray(vector<vector<_Tp> >& vec) : _InputArray(vec) {} 01112 template<typename _Tp, int m, int n> _OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx) : _InputArray(mtx) {} 01113 01115 01116 class CV_EXPORTS MatOp 01117 { 01118 public: 01119 MatOp() {}; 01120 virtual ~MatOp() {}; 01121 01122 virtual bool elementWise(const MatExpr& expr) const; 01123 virtual void assign(const MatExpr& expr, Mat& m, int type=-1) const = 0; 01124 virtual void roi(const MatExpr& expr, const Range& rowRange, 01125 const Range& colRange, MatExpr& res) const; 01126 virtual void diag(const MatExpr& expr, int d, MatExpr& res) const; 01127 virtual void augAssignAdd(const MatExpr& expr, Mat& m) const; 01128 virtual void augAssignSubtract(const MatExpr& expr, Mat& m) const; 01129 virtual void augAssignMultiply(const MatExpr& expr, Mat& m) const; 01130 virtual void augAssignDivide(const MatExpr& expr, Mat& m) const; 01131 virtual void augAssignAnd(const MatExpr& expr, Mat& m) const; 01132 virtual void augAssignOr(const MatExpr& expr, Mat& m) const; 01133 virtual void augAssignXor(const MatExpr& expr, Mat& m) const; 01134 01135 virtual void add(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const; 01136 virtual void add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const; 01137 01138 virtual void subtract(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const; 01139 virtual void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const; 01140 01141 virtual void multiply(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const; 01142 virtual void multiply(const MatExpr& expr1, double s, MatExpr& res) const; 01143 01144 virtual void divide(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const; 01145 virtual void divide(double s, const MatExpr& expr, MatExpr& res) const; 01146 01147 virtual void abs(const MatExpr& expr, MatExpr& res) const; 01148 01149 virtual void transpose(const MatExpr& expr, MatExpr& res) const; 01150 virtual void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const; 01151 virtual void invert(const MatExpr& expr, int method, MatExpr& res) const; 01152 01153 virtual Size size(const MatExpr& expr) const; 01154 virtual int type(const MatExpr& expr) const; 01155 }; 01156 01157 01158 class CV_EXPORTS MatExpr 01159 { 01160 public: 01161 MatExpr() : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s(Scalar()) {} 01162 MatExpr(const MatOp* _op, int _flags, const Mat& _a=Mat(), const Mat& _b=Mat(), 01163 const Mat& _c=Mat(), double _alpha=1, double _beta=1, const Scalar& _s=Scalar()) 01164 : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s) {} 01165 explicit MatExpr(const Mat& m); 01166 operator Mat() const 01167 { 01168 Mat m; 01169 op->assign(*this, m); 01170 return m; 01171 } 01172 01173 template<typename _Tp> operator Mat_<_Tp>() const 01174 { 01175 Mat_<_Tp> m; 01176 op->assign(*this, m, DataType<_Tp>::type); 01177 return m; 01178 } 01179 01180 MatExpr row(int y) const; 01181 MatExpr col(int x) const; 01182 MatExpr diag(int d=0) const; 01183 MatExpr operator()( const Range& rowRange, const Range& colRange ) const; 01184 MatExpr operator()( const Rect& roi ) const; 01185 01186 Mat cross(const Mat& m) const; 01187 double dot(const Mat& m) const; 01188 01189 MatExpr t() const; 01190 MatExpr inv(int method = DECOMP_LU) const; 01191 MatExpr mul(const MatExpr& e, double scale=1) const; 01192 MatExpr mul(const Mat& m, double scale=1) const; 01193 01194 Size size() const; 01195 int type() const; 01196 01197 const MatOp* op; 01198 int flags; 01199 01200 Mat a, b, c; 01201 double alpha, beta; 01202 Scalar s; 01203 }; 01204 01205 01206 CV_EXPORTS MatExpr operator + (const Mat& a, const Mat& b); 01207 CV_EXPORTS MatExpr operator + (const Mat& a, const Scalar& s); 01208 CV_EXPORTS MatExpr operator + (const Scalar& s, const Mat& a); 01209 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Mat& m); 01210 CV_EXPORTS MatExpr operator + (const Mat& m, const MatExpr& e); 01211 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Scalar& s); 01212 CV_EXPORTS MatExpr operator + (const Scalar& s, const MatExpr& e); 01213 CV_EXPORTS MatExpr operator + (const MatExpr& e1, const MatExpr& e2); 01214 01215 CV_EXPORTS MatExpr operator - (const Mat& a, const Mat& b); 01216 CV_EXPORTS MatExpr operator - (const Mat& a, const Scalar& s); 01217 CV_EXPORTS MatExpr operator - (const Scalar& s, const Mat& a); 01218 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Mat& m); 01219 CV_EXPORTS MatExpr operator - (const Mat& m, const MatExpr& e); 01220 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Scalar& s); 01221 CV_EXPORTS MatExpr operator - (const Scalar& s, const MatExpr& e); 01222 CV_EXPORTS MatExpr operator - (const MatExpr& e1, const MatExpr& e2); 01223 01224 CV_EXPORTS MatExpr operator - (const Mat& m); 01225 CV_EXPORTS MatExpr operator - (const MatExpr& e); 01226 01227 CV_EXPORTS MatExpr operator * (const Mat& a, const Mat& b); 01228 CV_EXPORTS MatExpr operator * (const Mat& a, double s); 01229 CV_EXPORTS MatExpr operator * (double s, const Mat& a); 01230 CV_EXPORTS MatExpr operator * (const MatExpr& e, const Mat& m); 01231 CV_EXPORTS MatExpr operator * (const Mat& m, const MatExpr& e); 01232 CV_EXPORTS MatExpr operator * (const MatExpr& e, double s); 01233 CV_EXPORTS MatExpr operator * (double s, const MatExpr& e); 01234 CV_EXPORTS MatExpr operator * (const MatExpr& e1, const MatExpr& e2); 01235 01236 CV_EXPORTS MatExpr operator / (const Mat& a, const Mat& b); 01237 CV_EXPORTS MatExpr operator / (const Mat& a, double s); 01238 CV_EXPORTS MatExpr operator / (double s, const Mat& a); 01239 CV_EXPORTS MatExpr operator / (const MatExpr& e, const Mat& m); 01240 CV_EXPORTS MatExpr operator / (const Mat& m, const MatExpr& e); 01241 CV_EXPORTS MatExpr operator / (const MatExpr& e, double s); 01242 CV_EXPORTS MatExpr operator / (double s, const MatExpr& e); 01243 CV_EXPORTS MatExpr operator / (const MatExpr& e1, const MatExpr& e2); 01244 01245 CV_EXPORTS MatExpr operator < (const Mat& a, const Mat& b); 01246 CV_EXPORTS MatExpr operator < (const Mat& a, double s); 01247 CV_EXPORTS MatExpr operator < (double s, const Mat& a); 01248 01249 CV_EXPORTS MatExpr operator <= (const Mat& a, const Mat& b); 01250 CV_EXPORTS MatExpr operator <= (const Mat& a, double s); 01251 CV_EXPORTS MatExpr operator <= (double s, const Mat& a); 01252 01253 CV_EXPORTS MatExpr operator == (const Mat& a, const Mat& b); 01254 CV_EXPORTS MatExpr operator == (const Mat& a, double s); 01255 CV_EXPORTS MatExpr operator == (double s, const Mat& a); 01256 01257 CV_EXPORTS MatExpr operator != (const Mat& a, const Mat& b); 01258 CV_EXPORTS MatExpr operator != (const Mat& a, double s); 01259 CV_EXPORTS MatExpr operator != (double s, const Mat& a); 01260 01261 CV_EXPORTS MatExpr operator >= (const Mat& a, const Mat& b); 01262 CV_EXPORTS MatExpr operator >= (const Mat& a, double s); 01263 CV_EXPORTS MatExpr operator >= (double s, const Mat& a); 01264 01265 CV_EXPORTS MatExpr operator > (const Mat& a, const Mat& b); 01266 CV_EXPORTS MatExpr operator > (const Mat& a, double s); 01267 CV_EXPORTS MatExpr operator > (double s, const Mat& a); 01268 01269 CV_EXPORTS MatExpr min(const Mat& a, const Mat& b); 01270 CV_EXPORTS MatExpr min(const Mat& a, double s); 01271 CV_EXPORTS MatExpr min(double s, const Mat& a); 01272 01273 CV_EXPORTS MatExpr max(const Mat& a, const Mat& b); 01274 CV_EXPORTS MatExpr max(const Mat& a, double s); 01275 CV_EXPORTS MatExpr max(double s, const Mat& a); 01276 01277 template<typename _Tp> static inline MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01278 { 01279 return cv::min((const Mat&)a, (const Mat&)b); 01280 } 01281 01282 template<typename _Tp> static inline MatExpr min(const Mat_<_Tp>& a, double s) 01283 { 01284 return cv::min((const Mat&)a, s); 01285 } 01286 01287 template<typename _Tp> static inline MatExpr min(double s, const Mat_<_Tp>& a) 01288 { 01289 return cv::min((const Mat&)a, s); 01290 } 01291 01292 template<typename _Tp> static inline MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01293 { 01294 return cv::max((const Mat&)a, (const Mat&)b); 01295 } 01296 01297 template<typename _Tp> static inline MatExpr max(const Mat_<_Tp>& a, double s) 01298 { 01299 return cv::max((const Mat&)a, s); 01300 } 01301 01302 template<typename _Tp> static inline MatExpr max(double s, const Mat_<_Tp>& a) 01303 { 01304 return cv::max((const Mat&)a, s); 01305 } 01306 01307 CV_EXPORTS MatExpr operator & (const Mat& a, const Mat& b); 01308 CV_EXPORTS MatExpr operator & (const Mat& a, const Scalar& s); 01309 CV_EXPORTS MatExpr operator & (const Scalar& s, const Mat& a); 01310 01311 CV_EXPORTS MatExpr operator | (const Mat& a, const Mat& b); 01312 CV_EXPORTS MatExpr operator | (const Mat& a, const Scalar& s); 01313 CV_EXPORTS MatExpr operator | (const Scalar& s, const Mat& a); 01314 01315 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Mat& b); 01316 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Scalar& s); 01317 CV_EXPORTS MatExpr operator ^ (const Scalar& s, const Mat& a); 01318 01319 CV_EXPORTS MatExpr operator ~(const Mat& m); 01320 01321 CV_EXPORTS MatExpr abs(const Mat& m); 01322 CV_EXPORTS MatExpr abs(const MatExpr& e); 01323 01324 template<typename _Tp> static inline MatExpr abs(const Mat_<_Tp>& m) 01325 { 01326 return cv::abs((const Mat&)m); 01327 } 01328 01330 01331 inline Mat& Mat::operator = (const MatExpr& e) 01332 { 01333 e.op->assign(e, *this); 01334 return *this; 01335 } 01336 01337 template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatExpr& e) 01338 { 01339 e.op->assign(e, *this, DataType<_Tp>::type); 01340 } 01341 01342 template<typename _Tp> Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e) 01343 { 01344 e.op->assign(e, *this, DataType<_Tp>::type); 01345 return *this; 01346 } 01347 01348 static inline Mat& operator += (const Mat& a, const Mat& b) 01349 { 01350 add(a, b, (Mat&)a); 01351 return (Mat&)a; 01352 } 01353 01354 static inline Mat& operator += (const Mat& a, const Scalar& s) 01355 { 01356 add(a, s, (Mat&)a); 01357 return (Mat&)a; 01358 } 01359 01360 template<typename _Tp> static inline 01361 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01362 { 01363 add(a, b, (Mat&)a); 01364 return (Mat_<_Tp>&)a; 01365 } 01366 01367 template<typename _Tp> static inline 01368 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Scalar& s) 01369 { 01370 add(a, s, (Mat&)a); 01371 return (Mat_<_Tp>&)a; 01372 } 01373 01374 static inline Mat& operator += (const Mat& a, const MatExpr& b) 01375 { 01376 b.op->augAssignAdd(b, (Mat&)a); 01377 return (Mat&)a; 01378 } 01379 01380 template<typename _Tp> static inline 01381 Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b) 01382 { 01383 b.op->augAssignAdd(b, (Mat&)a); 01384 return (Mat_<_Tp>&)a; 01385 } 01386 01387 static inline Mat& operator -= (const Mat& a, const Mat& b) 01388 { 01389 subtract(a, b, (Mat&)a); 01390 return (Mat&)a; 01391 } 01392 01393 static inline Mat& operator -= (const Mat& a, const Scalar& s) 01394 { 01395 subtract(a, s, (Mat&)a); 01396 return (Mat&)a; 01397 } 01398 01399 template<typename _Tp> static inline 01400 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01401 { 01402 subtract(a, b, (Mat&)a); 01403 return (Mat_<_Tp>&)a; 01404 } 01405 01406 template<typename _Tp> static inline 01407 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Scalar& s) 01408 { 01409 subtract(a, s, (Mat&)a); 01410 return (Mat_<_Tp>&)a; 01411 } 01412 01413 static inline Mat& operator -= (const Mat& a, const MatExpr& b) 01414 { 01415 b.op->augAssignSubtract(b, (Mat&)a); 01416 return (Mat&)a; 01417 } 01418 01419 template<typename _Tp> static inline 01420 Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b) 01421 { 01422 b.op->augAssignSubtract(b, (Mat&)a); 01423 return (Mat_<_Tp>&)a; 01424 } 01425 01426 static inline Mat& operator *= (const Mat& a, const Mat& b) 01427 { 01428 gemm(a, b, 1, Mat(), 0, (Mat&)a, 0); 01429 return (Mat&)a; 01430 } 01431 01432 static inline Mat& operator *= (const Mat& a, double s) 01433 { 01434 a.convertTo((Mat&)a, -1, s); 01435 return (Mat&)a; 01436 } 01437 01438 template<typename _Tp> static inline 01439 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01440 { 01441 gemm(a, b, 1, Mat(), 0, (Mat&)a, 0); 01442 return (Mat_<_Tp>&)a; 01443 } 01444 01445 template<typename _Tp> static inline 01446 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, double s) 01447 { 01448 a.convertTo((Mat&)a, -1, s); 01449 return (Mat_<_Tp>&)a; 01450 } 01451 01452 static inline Mat& operator *= (const Mat& a, const MatExpr& b) 01453 { 01454 b.op->augAssignMultiply(b, (Mat&)a); 01455 return (Mat&)a; 01456 } 01457 01458 template<typename _Tp> static inline 01459 Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b) 01460 { 01461 b.op->augAssignMultiply(b, (Mat&)a); 01462 return (Mat_<_Tp>&)a; 01463 } 01464 01465 static inline Mat& operator /= (const Mat& a, const Mat& b) 01466 { 01467 divide(a, b, (Mat&)a); 01468 return (Mat&)a; 01469 } 01470 01471 static inline Mat& operator /= (const Mat& a, double s) 01472 { 01473 a.convertTo((Mat&)a, -1, 1./s); 01474 return (Mat&)a; 01475 } 01476 01477 template<typename _Tp> static inline 01478 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01479 { 01480 divide(a, b, (Mat&)a); 01481 return (Mat_<_Tp>&)a; 01482 } 01483 01484 template<typename _Tp> static inline 01485 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, double s) 01486 { 01487 a.convertTo((Mat&)a, -1, 1./s); 01488 return (Mat_<_Tp>&)a; 01489 } 01490 01491 static inline Mat& operator /= (const Mat& a, const MatExpr& b) 01492 { 01493 b.op->augAssignDivide(b, (Mat&)a); 01494 return (Mat&)a; 01495 } 01496 01497 template<typename _Tp> static inline 01498 Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b) 01499 { 01500 b.op->augAssignDivide(b, (Mat&)a); 01501 return (Mat_<_Tp>&)a; 01502 } 01503 01505 01506 static inline Mat& operator &= (const Mat& a, const Mat& b) 01507 { 01508 bitwise_and(a, b, (Mat&)a); 01509 return (Mat&)a; 01510 } 01511 01512 static inline Mat& operator &= (const Mat& a, const Scalar& s) 01513 { 01514 bitwise_and(a, s, (Mat&)a); 01515 return (Mat&)a; 01516 } 01517 01518 template<typename _Tp> static inline Mat_<_Tp>& 01519 operator &= (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01520 { 01521 bitwise_and(a, b, (Mat&)a); 01522 return (Mat_<_Tp>&)a; 01523 } 01524 01525 template<typename _Tp> static inline Mat_<_Tp>& 01526 operator &= (const Mat_<_Tp>& a, const Scalar& s) 01527 { 01528 bitwise_and(a, s, (Mat&)a); 01529 return (Mat_<_Tp>&)a; 01530 } 01531 01532 static inline Mat& operator |= (const Mat& a, const Mat& b) 01533 { 01534 bitwise_or(a, b, (Mat&)a); 01535 return (Mat&)a; 01536 } 01537 01538 static inline Mat& operator |= (const Mat& a, const Scalar& s) 01539 { 01540 bitwise_or(a, s, (Mat&)a); 01541 return (Mat&)a; 01542 } 01543 01544 template<typename _Tp> static inline Mat_<_Tp>& 01545 operator |= (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01546 { 01547 bitwise_or(a, b, (Mat&)a); 01548 return (Mat_<_Tp>&)a; 01549 } 01550 01551 template<typename _Tp> static inline Mat_<_Tp>& 01552 operator |= (const Mat_<_Tp>& a, const Scalar& s) 01553 { 01554 bitwise_or(a, s, (Mat&)a); 01555 return (Mat_<_Tp>&)a; 01556 } 01557 01558 static inline Mat& operator ^= (const Mat& a, const Mat& b) 01559 { 01560 bitwise_xor(a, b, (Mat&)a); 01561 return (Mat&)a; 01562 } 01563 01564 static inline Mat& operator ^= (const Mat& a, const Scalar& s) 01565 { 01566 bitwise_xor(a, s, (Mat&)a); 01567 return (Mat&)a; 01568 } 01569 01570 template<typename _Tp> static inline Mat_<_Tp>& 01571 operator ^= (const Mat_<_Tp>& a, const Mat_<_Tp>& b) 01572 { 01573 bitwise_xor(a, b, (Mat&)a); 01574 return (Mat_<_Tp>&)a; 01575 } 01576 01577 template<typename _Tp> static inline Mat_<_Tp>& 01578 operator ^= (const Mat_<_Tp>& a, const Scalar& s) 01579 { 01580 bitwise_xor(a, s, (Mat&)a); 01581 return (Mat_<_Tp>&)a; 01582 } 01583 01585 01586 template<typename _Tp> void split(const Mat& src, vector<Mat_<_Tp> >& mv) 01587 { split(src, (vector<Mat>&)mv ); } 01588 01590 01591 template<typename _Tp> inline MatExpr Mat_<_Tp>::zeros(int rows, int cols) 01592 { 01593 return Mat::zeros(rows, cols, DataType<_Tp>::type); 01594 } 01595 01596 template<typename _Tp> inline MatExpr Mat_<_Tp>::zeros(Size sz) 01597 { 01598 return Mat::zeros(sz, DataType<_Tp>::type); 01599 } 01600 01601 template<typename _Tp> inline MatExpr Mat_<_Tp>::ones(int rows, int cols) 01602 { 01603 return Mat::ones(rows, cols, DataType<_Tp>::type); 01604 } 01605 01606 template<typename _Tp> inline MatExpr Mat_<_Tp>::ones(Size sz) 01607 { 01608 return Mat::ones(sz, DataType<_Tp>::type); 01609 } 01610 01611 template<typename _Tp> inline MatExpr Mat_<_Tp>::eye(int rows, int cols) 01612 { 01613 return Mat::eye(rows, cols, DataType<_Tp>::type); 01614 } 01615 01616 template<typename _Tp> inline MatExpr Mat_<_Tp>::eye(Size sz) 01617 { 01618 return Mat::eye(sz, DataType<_Tp>::type); 01619 } 01620 01622 01623 inline MatConstIterator::MatConstIterator() 01624 : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0) {} 01625 01626 inline MatConstIterator::MatConstIterator(const Mat* _m) 01627 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0) 01628 { 01629 if( m && m->isContinuous() ) 01630 { 01631 sliceStart = m->data; 01632 sliceEnd = sliceStart + m->total()*elemSize; 01633 } 01634 seek((const int*)0); 01635 } 01636 01637 inline MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col) 01638 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0) 01639 { 01640 CV_Assert(m && m->dims <= 2); 01641 if( m->isContinuous() ) 01642 { 01643 sliceStart = m->data; 01644 sliceEnd = sliceStart + m->total()*elemSize; 01645 } 01646 int idx[]={_row, _col}; 01647 seek(idx); 01648 } 01649 01650 inline MatConstIterator::MatConstIterator(const Mat* _m, Point _pt) 01651 : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0) 01652 { 01653 CV_Assert(m && m->dims <= 2); 01654 if( m->isContinuous() ) 01655 { 01656 sliceStart = m->data; 01657 sliceEnd = sliceStart + m->total()*elemSize; 01658 } 01659 int idx[]={_pt.y, _pt.x}; 01660 seek(idx); 01661 } 01662 01663 inline MatConstIterator::MatConstIterator(const MatConstIterator& it) 01664 : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd) 01665 {} 01666 01667 inline MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it ) 01668 { 01669 m = it.m; elemSize = it.elemSize; ptr = it.ptr; 01670 sliceStart = it.sliceStart; sliceEnd = it.sliceEnd; 01671 return *this; 01672 } 01673 01674 inline uchar* MatConstIterator::operator *() const { return ptr; } 01675 01676 inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs) 01677 { 01678 if( !m || ofs == 0 ) 01679 return *this; 01680 ptrdiff_t ofsb = ofs*elemSize; 01681 ptr += ofsb; 01682 if( ptr < sliceStart || sliceEnd <= ptr ) 01683 { 01684 ptr -= ofsb; 01685 seek(ofs, true); 01686 } 01687 return *this; 01688 } 01689 01690 inline MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs) 01691 { return (*this += -ofs); } 01692 01693 inline MatConstIterator& MatConstIterator::operator --() 01694 { 01695 if( m && (ptr -= elemSize) < sliceStart ) 01696 { 01697 ptr += elemSize; 01698 seek(-1, true); 01699 } 01700 return *this; 01701 } 01702 01703 inline MatConstIterator MatConstIterator::operator --(int) 01704 { 01705 MatConstIterator b = *this; 01706 *this += -1; 01707 return b; 01708 } 01709 01710 inline MatConstIterator& MatConstIterator::operator ++() 01711 { 01712 if( m && (ptr += elemSize) >= sliceEnd ) 01713 { 01714 ptr -= elemSize; 01715 seek(1, true); 01716 } 01717 return *this; 01718 } 01719 01720 inline MatConstIterator MatConstIterator::operator ++(int) 01721 { 01722 MatConstIterator b = *this; 01723 *this += 1; 01724 return b; 01725 } 01726 01727 template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_() {} 01728 01729 template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m) 01730 : MatConstIterator(_m) {} 01731 01732 template<typename _Tp> inline MatConstIterator_<_Tp>:: 01733 MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col) 01734 : MatConstIterator(_m, _row, _col) {} 01735 01736 template<typename _Tp> inline MatConstIterator_<_Tp>:: 01737 MatConstIterator_(const Mat_<_Tp>* _m, Point _pt) 01738 : MatConstIterator(_m, _pt) {} 01739 01740 template<typename _Tp> inline MatConstIterator_<_Tp>:: 01741 MatConstIterator_(const MatConstIterator_& it) 01742 : MatConstIterator(it) {} 01743 01744 template<typename _Tp> inline MatConstIterator_<_Tp>& 01745 MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it ) 01746 { 01747 MatConstIterator::operator = (it); 01748 return *this; 01749 } 01750 01751 template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator *() const { return *(_Tp*)(this->ptr); } 01752 01753 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs) 01754 { 01755 MatConstIterator::operator += (ofs); 01756 return *this; 01757 } 01758 01759 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs) 01760 { return (*this += -ofs); } 01761 01762 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --() 01763 { 01764 MatConstIterator::operator --(); 01765 return *this; 01766 } 01767 01768 template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int) 01769 { 01770 MatConstIterator_ b = *this; 01771 MatConstIterator::operator --(); 01772 return b; 01773 } 01774 01775 template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++() 01776 { 01777 MatConstIterator::operator ++(); 01778 return *this; 01779 } 01780 01781 template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int) 01782 { 01783 MatConstIterator_ b = *this; 01784 MatConstIterator::operator ++(); 01785 return b; 01786 } 01787 01788 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_() : MatConstIterator_<_Tp>() {} 01789 01790 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m) 01791 : MatConstIterator_<_Tp>(_m) {} 01792 01793 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col) 01794 : MatConstIterator_<_Tp>(_m, _row, _col) {} 01795 01796 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, Point _pt) 01797 : MatConstIterator_<_Tp>(_m, _pt) {} 01798 01799 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, const int* _idx) 01800 : MatConstIterator_<_Tp>(_m, _idx) {} 01801 01802 template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const MatIterator_& it) 01803 : MatConstIterator_<_Tp>(it) {} 01804 01805 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it ) 01806 { 01807 MatConstIterator::operator = (it); 01808 return *this; 01809 } 01810 01811 template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator *() const { return *(_Tp*)(this->ptr); } 01812 01813 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs) 01814 { 01815 MatConstIterator::operator += (ofs); 01816 return *this; 01817 } 01818 01819 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs) 01820 { 01821 MatConstIterator::operator += (-ofs); 01822 return *this; 01823 } 01824 01825 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator --() 01826 { 01827 MatConstIterator::operator --(); 01828 return *this; 01829 } 01830 01831 template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int) 01832 { 01833 MatIterator_ b = *this; 01834 MatConstIterator::operator --(); 01835 return b; 01836 } 01837 01838 template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++() 01839 { 01840 MatConstIterator::operator ++(); 01841 return *this; 01842 } 01843 01844 template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int) 01845 { 01846 MatIterator_ b = *this; 01847 MatConstIterator::operator ++(); 01848 return b; 01849 } 01850 01851 template<typename _Tp> inline Point MatConstIterator_<_Tp>::pos() const 01852 { 01853 if( !m ) 01854 return Point(); 01855 CV_DbgAssert( m->dims <= 2 ); 01856 if( m->isContinuous() ) 01857 { 01858 ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data; 01859 int y = (int)(ofs / m->cols), x = (int)(ofs - (ptrdiff_t)y*m->cols); 01860 return Point(x, y); 01861 } 01862 else 01863 { 01864 ptrdiff_t ofs = (uchar*)ptr - m->data; 01865 int y = (int)(ofs / m->step), x = (int)((ofs - y*m->step)/sizeof(_Tp)); 01866 return Point(x, y); 01867 } 01868 } 01869 01870 static inline bool 01871 operator == (const MatConstIterator& a, const MatConstIterator& b) 01872 { return a.m == b.m && a.ptr == b.ptr; } 01873 01874 template<typename _Tp> static inline bool 01875 operator != (const MatConstIterator& a, const MatConstIterator& b) 01876 { return !(a == b); } 01877 01878 template<typename _Tp> static inline bool 01879 operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b) 01880 { return a.m == b.m && a.ptr == b.ptr; } 01881 01882 template<typename _Tp> static inline bool 01883 operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b) 01884 { return a.m != b.m || a.ptr != b.ptr; } 01885 01886 template<typename _Tp> static inline bool 01887 operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b) 01888 { return a.m == b.m && a.ptr == b.ptr; } 01889 01890 template<typename _Tp> static inline bool 01891 operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b) 01892 { return a.m != b.m || a.ptr != b.ptr; } 01893 01894 static inline bool 01895 operator < (const MatConstIterator& a, const MatConstIterator& b) 01896 { return a.ptr < b.ptr; } 01897 01898 static inline bool 01899 operator > (const MatConstIterator& a, const MatConstIterator& b) 01900 { return a.ptr > b.ptr; } 01901 01902 static inline bool 01903 operator <= (const MatConstIterator& a, const MatConstIterator& b) 01904 { return a.ptr <= b.ptr; } 01905 01906 static inline bool 01907 operator >= (const MatConstIterator& a, const MatConstIterator& b) 01908 { return a.ptr >= b.ptr; } 01909 01910 CV_EXPORTS ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a); 01911 01912 static inline MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs) 01913 { MatConstIterator b = a; return b += ofs; } 01914 01915 static inline MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a) 01916 { MatConstIterator b = a; return b += ofs; } 01917 01918 static inline MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs) 01919 { MatConstIterator b = a; return b += -ofs; } 01920 01921 template<typename _Tp> static inline MatConstIterator_<_Tp> 01922 operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs) 01923 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; } 01924 01925 template<typename _Tp> static inline MatConstIterator_<_Tp> 01926 operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a) 01927 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatConstIterator_<_Tp>&)t; } 01928 01929 template<typename _Tp> static inline MatConstIterator_<_Tp> 01930 operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs) 01931 { MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatConstIterator_<_Tp>&)t; } 01932 01933 inline uchar* MatConstIterator::operator [](ptrdiff_t i) const 01934 { return *(*this + i); } 01935 01936 template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const 01937 { return *(_Tp*)MatConstIterator::operator [](i); } 01938 01939 template<typename _Tp> static inline MatIterator_<_Tp> 01940 operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs) 01941 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; } 01942 01943 template<typename _Tp> static inline MatIterator_<_Tp> 01944 operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a) 01945 { MatConstIterator t = (const MatConstIterator&)a + ofs; return (MatIterator_<_Tp>&)t; } 01946 01947 template<typename _Tp> static inline MatIterator_<_Tp> 01948 operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs) 01949 { MatConstIterator t = (const MatConstIterator&)a - ofs; return (MatIterator_<_Tp>&)t; } 01950 01951 template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const 01952 { return *(*this + i); } 01953 01954 template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::begin() const 01955 { return Mat::begin<_Tp>(); } 01956 01957 template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::end() const 01958 { return Mat::end<_Tp>(); } 01959 01960 template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::begin() 01961 { return Mat::begin<_Tp>(); } 01962 01963 template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::end() 01964 { return Mat::end<_Tp>(); } 01965 01966 template<typename _Tp> inline MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m) : it(_m) {} 01967 01968 template<typename _Tp> template<typename T2> inline MatCommaInitializer_<_Tp>& 01969 MatCommaInitializer_<_Tp>::operator , (T2 v) 01970 { 01971 CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() ); 01972 *this->it = _Tp(v); ++this->it; 01973 return *this; 01974 } 01975 01976 template<typename _Tp> inline Mat_<_Tp> MatCommaInitializer_<_Tp>::operator *() const 01977 { 01978 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() ); 01979 return Mat_<_Tp>(*this->it.m); 01980 } 01981 01982 template<typename _Tp> inline MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const 01983 { 01984 CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() ); 01985 return Mat_<_Tp>(*this->it.m); 01986 } 01987 01988 template<typename _Tp, typename T2> static inline MatCommaInitializer_<_Tp> 01989 operator << (const Mat_<_Tp>& m, T2 val) 01990 { 01991 MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m); 01992 return (commaInitializer, val); 01993 } 01994 01996 01997 inline SparseMat::SparseMat() 01998 : flags(MAGIC_VAL), hdr(0) 01999 { 02000 } 02001 02002 inline SparseMat::SparseMat(int _dims, const int* _sizes, int _type) 02003 : flags(MAGIC_VAL), hdr(0) 02004 { 02005 create(_dims, _sizes, _type); 02006 } 02007 02008 inline SparseMat::SparseMat(const SparseMat& m) 02009 : flags(m.flags), hdr(m.hdr) 02010 { 02011 addref(); 02012 } 02013 02014 inline SparseMat::~SparseMat() 02015 { 02016 release(); 02017 } 02018 02019 inline SparseMat& SparseMat::operator = (const SparseMat& m) 02020 { 02021 if( this != &m ) 02022 { 02023 if( m.hdr ) 02024 CV_XADD(&m.hdr->refcount, 1); 02025 release(); 02026 flags = m.flags; 02027 hdr = m.hdr; 02028 } 02029 return *this; 02030 } 02031 02032 inline SparseMat& SparseMat::operator = (const Mat& m) 02033 { return (*this = SparseMat(m)); } 02034 02035 inline SparseMat SparseMat::clone() const 02036 { 02037 SparseMat temp; 02038 this->copyTo(temp); 02039 return temp; 02040 } 02041 02042 02043 inline void SparseMat::assignTo( SparseMat& m, int type ) const 02044 { 02045 if( type < 0 ) 02046 m = *this; 02047 else 02048 convertTo(m, type); 02049 } 02050 02051 inline void SparseMat::addref() 02052 { if( hdr ) CV_XADD(&hdr->refcount, 1); } 02053 02054 inline void SparseMat::release() 02055 { 02056 if( hdr && CV_XADD(&hdr->refcount, -1) == 1 ) 02057 delete hdr; 02058 hdr = 0; 02059 } 02060 02061 inline size_t SparseMat::elemSize() const 02062 { return CV_ELEM_SIZE(flags); } 02063 02064 inline size_t SparseMat::elemSize1() const 02065 { return CV_ELEM_SIZE1(flags); } 02066 02067 inline int SparseMat::type() const 02068 { return CV_MAT_TYPE(flags); } 02069 02070 inline int SparseMat::depth() const 02071 { return CV_MAT_DEPTH(flags); } 02072 02073 inline int SparseMat::channels() const 02074 { return CV_MAT_CN(flags); } 02075 02076 inline const int* SparseMat::size() const 02077 { 02078 return hdr ? hdr->size : 0; 02079 } 02080 02081 inline int SparseMat::size(int i) const 02082 { 02083 if( hdr ) 02084 { 02085 CV_DbgAssert((unsigned)i < (unsigned)hdr->dims); 02086 return hdr->size[i]; 02087 } 02088 return 0; 02089 } 02090 02091 inline int SparseMat::dims() const 02092 { 02093 return hdr ? hdr->dims : 0; 02094 } 02095 02096 inline size_t SparseMat::nzcount() const 02097 { 02098 return hdr ? hdr->nodeCount : 0; 02099 } 02100 02101 inline size_t SparseMat::hash(int i0) const 02102 { 02103 return (size_t)i0; 02104 } 02105 02106 inline size_t SparseMat::hash(int i0, int i1) const 02107 { 02108 return (size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1; 02109 } 02110 02111 inline size_t SparseMat::hash(int i0, int i1, int i2) const 02112 { 02113 return ((size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1)*HASH_SCALE + (unsigned)i2; 02114 } 02115 02116 inline size_t SparseMat::hash(const int* idx) const 02117 { 02118 size_t h = (unsigned)idx[0]; 02119 if( !hdr ) 02120 return 0; 02121 int i, d = hdr->dims; 02122 for( i = 1; i < d; i++ ) 02123 h = h*HASH_SCALE + (unsigned)idx[i]; 02124 return h; 02125 } 02126 02127 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, size_t* hashval) 02128 { return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval); } 02129 02130 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, size_t* hashval) 02131 { return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); } 02132 02133 template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval) 02134 { return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); } 02135 02136 template<typename _Tp> inline _Tp& SparseMat::ref(const int* idx, size_t* hashval) 02137 { return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval); } 02138 02139 template<typename _Tp> inline _Tp SparseMat::value(int i0, size_t* hashval) const 02140 { 02141 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval); 02142 return p ? *p : _Tp(); 02143 } 02144 02145 template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, size_t* hashval) const 02146 { 02147 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval); 02148 return p ? *p : _Tp(); 02149 } 02150 02151 template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const 02152 { 02153 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval); 02154 return p ? *p : _Tp(); 02155 } 02156 02157 template<typename _Tp> inline _Tp SparseMat::value(const int* idx, size_t* hashval) const 02158 { 02159 const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval); 02160 return p ? *p : _Tp(); 02161 } 02162 02163 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, size_t* hashval) const 02164 { return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval); } 02165 02166 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const 02167 { return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval); } 02168 02169 template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const 02170 { return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval); } 02171 02172 template<typename _Tp> inline const _Tp* SparseMat::find(const int* idx, size_t* hashval) const 02173 { return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval); } 02174 02175 template<typename _Tp> inline _Tp& SparseMat::value(Node* n) 02176 { return *(_Tp*)((uchar*)n + hdr->valueOffset); } 02177 02178 template<typename _Tp> inline const _Tp& SparseMat::value(const Node* n) const 02179 { return *(const _Tp*)((const uchar*)n + hdr->valueOffset); } 02180 02181 inline SparseMat::Node* SparseMat::node(size_t nidx) 02182 { return (Node*)&hdr->pool[nidx]; } 02183 02184 inline const SparseMat::Node* SparseMat::node(size_t nidx) const 02185 { return (const Node*)&hdr->pool[nidx]; } 02186 02187 inline SparseMatIterator SparseMat::begin() 02188 { return SparseMatIterator(this); } 02189 02190 inline SparseMatConstIterator SparseMat::begin() const 02191 { return SparseMatConstIterator(this); } 02192 02193 inline SparseMatIterator SparseMat::end() 02194 { SparseMatIterator it(this); it.seekEnd(); return it; } 02195 02196 inline SparseMatConstIterator SparseMat::end() const 02197 { SparseMatConstIterator it(this); it.seekEnd(); return it; } 02198 02199 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat::begin() 02200 { return SparseMatIterator_<_Tp>(this); } 02201 02202 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat::begin() const 02203 { return SparseMatConstIterator_<_Tp>(this); } 02204 02205 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat::end() 02206 { SparseMatIterator_<_Tp> it(this); it.seekEnd(); return it; } 02207 02208 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat::end() const 02209 { SparseMatConstIterator_<_Tp> it(this); it.seekEnd(); return it; } 02210 02211 02212 inline SparseMatConstIterator::SparseMatConstIterator() 02213 : m(0), hashidx(0), ptr(0) 02214 { 02215 } 02216 02217 inline SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it) 02218 : m(it.m), hashidx(it.hashidx), ptr(it.ptr) 02219 { 02220 } 02221 02222 static inline bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2) 02223 { return it1.m == it2.m && it1.hashidx == it2.hashidx && it1.ptr == it2.ptr; } 02224 02225 static inline bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2) 02226 { return !(it1 == it2); } 02227 02228 02229 inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it) 02230 { 02231 if( this != &it ) 02232 { 02233 m = it.m; 02234 hashidx = it.hashidx; 02235 ptr = it.ptr; 02236 } 02237 return *this; 02238 } 02239 02240 template<typename _Tp> inline const _Tp& SparseMatConstIterator::value() const 02241 { return *(_Tp*)ptr; } 02242 02243 inline const SparseMat::Node* SparseMatConstIterator::node() const 02244 { 02245 return ptr && m && m->hdr ? 02246 (const SparseMat::Node*)(ptr - m->hdr->valueOffset) : 0; 02247 } 02248 02249 inline SparseMatConstIterator SparseMatConstIterator::operator ++(int) 02250 { 02251 SparseMatConstIterator it = *this; 02252 ++*this; 02253 return it; 02254 } 02255 02256 02257 inline void SparseMatConstIterator::seekEnd() 02258 { 02259 if( m && m->hdr ) 02260 { 02261 hashidx = m->hdr->hashtab.size(); 02262 ptr = 0; 02263 } 02264 } 02265 02266 inline SparseMatIterator::SparseMatIterator() 02267 {} 02268 02269 inline SparseMatIterator::SparseMatIterator(SparseMat* _m) 02270 : SparseMatConstIterator(_m) 02271 {} 02272 02273 inline SparseMatIterator::SparseMatIterator(const SparseMatIterator& it) 02274 : SparseMatConstIterator(it) 02275 { 02276 } 02277 02278 inline SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it) 02279 { 02280 (SparseMatConstIterator&)*this = it; 02281 return *this; 02282 } 02283 02284 template<typename _Tp> inline _Tp& SparseMatIterator::value() const 02285 { return *(_Tp*)ptr; } 02286 02287 inline SparseMat::Node* SparseMatIterator::node() const 02288 { 02289 return (SparseMat::Node*)SparseMatConstIterator::node(); 02290 } 02291 02292 inline SparseMatIterator& SparseMatIterator::operator ++() 02293 { 02294 SparseMatConstIterator::operator ++(); 02295 return *this; 02296 } 02297 02298 inline SparseMatIterator SparseMatIterator::operator ++(int) 02299 { 02300 SparseMatIterator it = *this; 02301 ++*this; 02302 return it; 02303 } 02304 02305 02306 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_() 02307 { flags = MAGIC_VAL | DataType<_Tp>::type; } 02308 02309 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes) 02310 : SparseMat(_dims, _sizes, DataType<_Tp>::type) 02311 {} 02312 02313 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat& m) 02314 { 02315 if( m.type() == DataType<_Tp>::type ) 02316 *this = (const SparseMat_<_Tp>&)m; 02317 else 02318 m.convertTo(this, DataType<_Tp>::type); 02319 } 02320 02321 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m) 02322 { 02323 this->flags = m.flags; 02324 this->hdr = m.hdr; 02325 if( this->hdr ) 02326 CV_XADD(&this->hdr->refcount, 1); 02327 } 02328 02329 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Mat& m) 02330 { 02331 SparseMat sm(m); 02332 *this = sm; 02333 } 02334 02335 template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const CvSparseMat* m) 02336 { 02337 SparseMat sm(m); 02338 *this = sm; 02339 } 02340 02341 template<typename _Tp> inline SparseMat_<_Tp>& 02342 SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m) 02343 { 02344 if( this != &m ) 02345 { 02346 if( m.hdr ) CV_XADD(&m.hdr->refcount, 1); 02347 release(); 02348 flags = m.flags; 02349 hdr = m.hdr; 02350 } 02351 return *this; 02352 } 02353 02354 template<typename _Tp> inline SparseMat_<_Tp>& 02355 SparseMat_<_Tp>::operator = (const SparseMat& m) 02356 { 02357 if( m.type() == DataType<_Tp>::type ) 02358 return (*this = (const SparseMat_<_Tp>&)m); 02359 m.convertTo(*this, DataType<_Tp>::type); 02360 return *this; 02361 } 02362 02363 template<typename _Tp> inline SparseMat_<_Tp>& 02364 SparseMat_<_Tp>::operator = (const Mat& m) 02365 { return (*this = SparseMat(m)); } 02366 02367 template<typename _Tp> inline SparseMat_<_Tp> 02368 SparseMat_<_Tp>::clone() const 02369 { 02370 SparseMat_<_Tp> m; 02371 this->copyTo(m); 02372 return m; 02373 } 02374 02375 template<typename _Tp> inline void 02376 SparseMat_<_Tp>::create(int _dims, const int* _sizes) 02377 { 02378 SparseMat::create(_dims, _sizes, DataType<_Tp>::type); 02379 } 02380 02381 template<typename _Tp> inline 02382 SparseMat_<_Tp>::operator CvSparseMat*() const 02383 { 02384 return SparseMat::operator CvSparseMat*(); 02385 } 02386 02387 template<typename _Tp> inline int SparseMat_<_Tp>::type() const 02388 { return DataType<_Tp>::type; } 02389 02390 template<typename _Tp> inline int SparseMat_<_Tp>::depth() const 02391 { return DataType<_Tp>::depth; } 02392 02393 template<typename _Tp> inline int SparseMat_<_Tp>::channels() const 02394 { return DataType<_Tp>::channels; } 02395 02396 template<typename _Tp> inline _Tp& 02397 SparseMat_<_Tp>::ref(int i0, size_t* hashval) 02398 { return SparseMat::ref<_Tp>(i0, hashval); } 02399 02400 template<typename _Tp> inline _Tp 02401 SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const 02402 { return SparseMat::value<_Tp>(i0, hashval); } 02403 02404 template<typename _Tp> inline _Tp& 02405 SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval) 02406 { return SparseMat::ref<_Tp>(i0, i1, hashval); } 02407 02408 template<typename _Tp> inline _Tp 02409 SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const 02410 { return SparseMat::value<_Tp>(i0, i1, hashval); } 02411 02412 template<typename _Tp> inline _Tp& 02413 SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval) 02414 { return SparseMat::ref<_Tp>(i0, i1, i2, hashval); } 02415 02416 template<typename _Tp> inline _Tp 02417 SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const 02418 { return SparseMat::value<_Tp>(i0, i1, i2, hashval); } 02419 02420 template<typename _Tp> inline _Tp& 02421 SparseMat_<_Tp>::ref(const int* idx, size_t* hashval) 02422 { return SparseMat::ref<_Tp>(idx, hashval); } 02423 02424 template<typename _Tp> inline _Tp 02425 SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const 02426 { return SparseMat::value<_Tp>(idx, hashval); } 02427 02428 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin() 02429 { return SparseMatIterator_<_Tp>(this); } 02430 02431 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const 02432 { return SparseMatConstIterator_<_Tp>(this); } 02433 02434 template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::end() 02435 { SparseMatIterator_<_Tp> it(this); it.seekEnd(); return it; } 02436 02437 template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const 02438 { SparseMatConstIterator_<_Tp> it(this); it.seekEnd(); return it; } 02439 02440 template<typename _Tp> inline 02441 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_() 02442 {} 02443 02444 template<typename _Tp> inline 02445 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m) 02446 : SparseMatConstIterator(_m) 02447 {} 02448 02449 template<typename _Tp> inline 02450 SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it) 02451 : SparseMatConstIterator(it) 02452 {} 02453 02454 template<typename _Tp> inline SparseMatConstIterator_<_Tp>& 02455 SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it) 02456 { return ((SparseMatConstIterator&)*this = it); } 02457 02458 template<typename _Tp> inline const _Tp& 02459 SparseMatConstIterator_<_Tp>::operator *() const 02460 { return *(const _Tp*)this->ptr; } 02461 02462 template<typename _Tp> inline SparseMatConstIterator_<_Tp>& 02463 SparseMatConstIterator_<_Tp>::operator ++() 02464 { 02465 SparseMatConstIterator::operator ++(); 02466 return *this; 02467 } 02468 02469 template<typename _Tp> inline SparseMatConstIterator_<_Tp> 02470 SparseMatConstIterator_<_Tp>::operator ++(int) 02471 { 02472 SparseMatConstIterator it = *this; 02473 SparseMatConstIterator::operator ++(); 02474 return it; 02475 } 02476 02477 template<typename _Tp> inline 02478 SparseMatIterator_<_Tp>::SparseMatIterator_() 02479 {} 02480 02481 template<typename _Tp> inline 02482 SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m) 02483 : SparseMatConstIterator_<_Tp>(_m) 02484 {} 02485 02486 template<typename _Tp> inline 02487 SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it) 02488 : SparseMatConstIterator_<_Tp>(it) 02489 {} 02490 02491 template<typename _Tp> inline SparseMatIterator_<_Tp>& 02492 SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it) 02493 { return ((SparseMatIterator&)*this = it); } 02494 02495 template<typename _Tp> inline _Tp& 02496 SparseMatIterator_<_Tp>::operator *() const 02497 { return *(_Tp*)this->ptr; } 02498 02499 template<typename _Tp> inline SparseMatIterator_<_Tp>& 02500 SparseMatIterator_<_Tp>::operator ++() 02501 { 02502 SparseMatConstIterator::operator ++(); 02503 return *this; 02504 } 02505 02506 template<typename _Tp> inline SparseMatIterator_<_Tp> 02507 SparseMatIterator_<_Tp>::operator ++(int) 02508 { 02509 SparseMatIterator it = *this; 02510 SparseMatConstIterator::operator ++(); 02511 return it; 02512 } 02513 02514 } 02515 02516 #endif 02517 #endif