Merge pull request #2142 from KonstantinMatskevich:ocl_tapi_bfmatcher
[profile/ivi/opencv.git] / modules / core / include / opencv2 / core / mat.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #ifndef __OPENCV_CORE_MAT_HPP__
45 #define __OPENCV_CORE_MAT_HPP__
46
47 #ifndef __cplusplus
48 #  error mat.hpp header must be compiled as C++
49 #endif
50
51 #include "opencv2/core/matx.hpp"
52 #include "opencv2/core/types.hpp"
53
54
55 namespace cv
56 {
57
58 enum { ACCESS_READ=1<<24, ACCESS_WRITE=1<<25,
59     ACCESS_RW=3<<24, ACCESS_MASK=ACCESS_RW, ACCESS_FAST=1<<26 };
60
61 class CV_EXPORTS _OutputArray;
62
63 //////////////////////// Input/Output Array Arguments /////////////////////////////////
64
65 /*!
66  Proxy datatype for passing Mat's and vector<>'s as input parameters
67  */
68 class CV_EXPORTS _InputArray
69 {
70 public:
71     enum {
72         KIND_SHIFT = 16,
73         FIXED_TYPE = 0x8000 << KIND_SHIFT,
74         FIXED_SIZE = 0x4000 << KIND_SHIFT,
75         KIND_MASK = 31 << KIND_SHIFT,
76
77         NONE              = 0 << KIND_SHIFT,
78         MAT               = 1 << KIND_SHIFT,
79         MATX              = 2 << KIND_SHIFT,
80         STD_VECTOR        = 3 << KIND_SHIFT,
81         STD_VECTOR_VECTOR = 4 << KIND_SHIFT,
82         STD_VECTOR_MAT    = 5 << KIND_SHIFT,
83         EXPR              = 6 << KIND_SHIFT,
84         OPENGL_BUFFER     = 7 << KIND_SHIFT,
85         CUDA_MEM          = 8 << KIND_SHIFT,
86         GPU_MAT           = 9 << KIND_SHIFT,
87         OCL_MAT           =10 << KIND_SHIFT,
88         UMAT              =11 << KIND_SHIFT,
89         STD_VECTOR_UMAT   =12 << KIND_SHIFT,
90         UEXPR             =13 << KIND_SHIFT
91     };
92
93     _InputArray();
94     _InputArray(int _flags, void* _obj);
95     _InputArray(const Mat& m);
96     _InputArray(const MatExpr& expr);
97     _InputArray(const std::vector<Mat>& vec);
98     template<typename _Tp> _InputArray(const Mat_<_Tp>& m);
99     template<typename _Tp> _InputArray(const std::vector<_Tp>& vec);
100     template<typename _Tp> _InputArray(const std::vector<std::vector<_Tp> >& vec);
101     template<typename _Tp> _InputArray(const std::vector<Mat_<_Tp> >& vec);
102     template<typename _Tp> _InputArray(const _Tp* vec, int n);
103     template<typename _Tp, int m, int n> _InputArray(const Matx<_Tp, m, n>& matx);
104     _InputArray(const double& val);
105     _InputArray(const cuda::GpuMat& d_mat);
106     _InputArray(const ogl::Buffer& buf);
107     _InputArray(const cuda::CudaMem& cuda_mem);
108     template<typename _Tp> _InputArray(const cudev::GpuMat_<_Tp>& m);
109     _InputArray(const UMat& um);
110     _InputArray(const std::vector<UMat>& umv);
111     _InputArray(const UMatExpr& uexpr);
112
113     virtual Mat getMat(int idx=-1) const;
114     virtual UMat getUMat(int idx=-1) const;
115     virtual void getMatVector(std::vector<Mat>& mv) const;
116     virtual void getUMatVector(std::vector<UMat>& umv) const;
117     virtual cuda::GpuMat getGpuMat() const;
118     virtual ogl::Buffer getOGlBuffer() const;
119     void* getObj() const;
120
121     virtual int kind() const;
122     virtual int dims(int i=-1) const;
123     virtual Size size(int i=-1) const;
124     virtual int sizend(int* sz, int i=-1) const;
125     virtual bool sameSize(const _InputArray& arr) const;
126     virtual size_t total(int i=-1) const;
127     virtual int type(int i=-1) const;
128     virtual int depth(int i=-1) const;
129     virtual int channels(int i=-1) const;
130     virtual bool isContinuous(int i=-1) const;
131     virtual bool isSubmatrix(int i=-1) const;
132     virtual bool empty() const;
133     virtual void copyTo(const _OutputArray& arr) const;
134     virtual size_t offset(int i=-1) const;
135     virtual size_t step(int i=-1) const;
136     bool isMat() const;
137     bool isUMat() const;
138     bool isMatVector() const;
139     bool isUMatVector() const;
140     bool isMatx();
141
142     virtual ~_InputArray();
143
144 protected:
145     int flags;
146     void* obj;
147     Size sz;
148
149     void init(int _flags, const void* _obj);
150     void init(int _flags, const void* _obj, Size _sz);
151 };
152
153
154 /*!
155  Proxy datatype for passing Mat's and vector<>'s as input parameters
156  */
157 class CV_EXPORTS _OutputArray : public _InputArray
158 {
159 public:
160     enum
161     {
162         DEPTH_MASK_8U = 1 << CV_8U,
163         DEPTH_MASK_8S = 1 << CV_8S,
164         DEPTH_MASK_16U = 1 << CV_16U,
165         DEPTH_MASK_16S = 1 << CV_16S,
166         DEPTH_MASK_32S = 1 << CV_32S,
167         DEPTH_MASK_32F = 1 << CV_32F,
168         DEPTH_MASK_64F = 1 << CV_64F,
169         DEPTH_MASK_ALL = (DEPTH_MASK_64F<<1)-1,
170         DEPTH_MASK_ALL_BUT_8S = DEPTH_MASK_ALL & ~DEPTH_MASK_8S,
171         DEPTH_MASK_FLT = DEPTH_MASK_32F + DEPTH_MASK_64F
172     };
173
174     _OutputArray();
175     _OutputArray(int _flags, void* _obj);
176     _OutputArray(Mat& m);
177     _OutputArray(std::vector<Mat>& vec);
178     _OutputArray(cuda::GpuMat& d_mat);
179     _OutputArray(ogl::Buffer& buf);
180     _OutputArray(cuda::CudaMem& cuda_mem);
181     template<typename _Tp> _OutputArray(cudev::GpuMat_<_Tp>& m);
182     template<typename _Tp> _OutputArray(std::vector<_Tp>& vec);
183     template<typename _Tp> _OutputArray(std::vector<std::vector<_Tp> >& vec);
184     template<typename _Tp> _OutputArray(std::vector<Mat_<_Tp> >& vec);
185     template<typename _Tp> _OutputArray(Mat_<_Tp>& m);
186     template<typename _Tp> _OutputArray(_Tp* vec, int n);
187     template<typename _Tp, int m, int n> _OutputArray(Matx<_Tp, m, n>& matx);
188     _OutputArray(UMat& m);
189     _OutputArray(std::vector<UMat>& vec);
190
191     _OutputArray(const Mat& m);
192     _OutputArray(const std::vector<Mat>& vec);
193     _OutputArray(const cuda::GpuMat& d_mat);
194     _OutputArray(const ogl::Buffer& buf);
195     _OutputArray(const cuda::CudaMem& cuda_mem);
196     template<typename _Tp> _OutputArray(const cudev::GpuMat_<_Tp>& m);
197     template<typename _Tp> _OutputArray(const std::vector<_Tp>& vec);
198     template<typename _Tp> _OutputArray(const std::vector<std::vector<_Tp> >& vec);
199     template<typename _Tp> _OutputArray(const std::vector<Mat_<_Tp> >& vec);
200     template<typename _Tp> _OutputArray(const Mat_<_Tp>& m);
201     template<typename _Tp> _OutputArray(const _Tp* vec, int n);
202     template<typename _Tp, int m, int n> _OutputArray(const Matx<_Tp, m, n>& matx);
203     _OutputArray(const UMat& m);
204     _OutputArray(const std::vector<UMat>& vec);
205
206     virtual bool fixedSize() const;
207     virtual bool fixedType() const;
208     virtual bool needed() const;
209     virtual Mat& getMatRef(int i=-1) const;
210     virtual cuda::GpuMat& getGpuMatRef() const;
211     virtual ogl::Buffer& getOGlBufferRef() const;
212     virtual cuda::CudaMem& getCudaMemRef() const;
213     virtual void create(Size sz, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
214     virtual void create(int rows, int cols, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
215     virtual void create(int dims, const int* size, int type, int i=-1, bool allowTransposed=false, int fixedDepthMask=0) const;
216     virtual void createSameSize(const _InputArray& arr, int mtype) const;
217     virtual void release() const;
218     virtual void clear() const;
219     virtual void setTo(const _InputArray& value) const;
220 };
221
222
223 class CV_EXPORTS _InputOutputArray : public _OutputArray
224 {
225 public:
226     _InputOutputArray();
227     _InputOutputArray(int _flags, void* _obj);
228     _InputOutputArray(Mat& m);
229     _InputOutputArray(std::vector<Mat>& vec);
230     _InputOutputArray(cuda::GpuMat& d_mat);
231     _InputOutputArray(ogl::Buffer& buf);
232     _InputOutputArray(cuda::CudaMem& cuda_mem);
233     template<typename _Tp> _InputOutputArray(cudev::GpuMat_<_Tp>& m);
234     template<typename _Tp> _InputOutputArray(std::vector<_Tp>& vec);
235     template<typename _Tp> _InputOutputArray(std::vector<std::vector<_Tp> >& vec);
236     template<typename _Tp> _InputOutputArray(std::vector<Mat_<_Tp> >& vec);
237     template<typename _Tp> _InputOutputArray(Mat_<_Tp>& m);
238     template<typename _Tp> _InputOutputArray(_Tp* vec, int n);
239     template<typename _Tp, int m, int n> _InputOutputArray(Matx<_Tp, m, n>& matx);
240     _InputOutputArray(UMat& m);
241     _InputOutputArray(std::vector<UMat>& vec);
242
243     _InputOutputArray(const Mat& m);
244     _InputOutputArray(const std::vector<Mat>& vec);
245     _InputOutputArray(const cuda::GpuMat& d_mat);
246     _InputOutputArray(const ogl::Buffer& buf);
247     _InputOutputArray(const cuda::CudaMem& cuda_mem);
248     template<typename _Tp> _InputOutputArray(const cudev::GpuMat_<_Tp>& m);
249     template<typename _Tp> _InputOutputArray(const std::vector<_Tp>& vec);
250     template<typename _Tp> _InputOutputArray(const std::vector<std::vector<_Tp> >& vec);
251     template<typename _Tp> _InputOutputArray(const std::vector<Mat_<_Tp> >& vec);
252     template<typename _Tp> _InputOutputArray(const Mat_<_Tp>& m);
253     template<typename _Tp> _InputOutputArray(const _Tp* vec, int n);
254     template<typename _Tp, int m, int n> _InputOutputArray(const Matx<_Tp, m, n>& matx);
255     _InputOutputArray(const UMat& m);
256     _InputOutputArray(const std::vector<UMat>& vec);
257 };
258
259 typedef const _InputArray& InputArray;
260 typedef InputArray InputArrayOfArrays;
261 typedef const _OutputArray& OutputArray;
262 typedef OutputArray OutputArrayOfArrays;
263 typedef const _InputOutputArray& InputOutputArray;
264 typedef InputOutputArray InputOutputArrayOfArrays;
265
266 CV_EXPORTS InputOutputArray noArray();
267
268 /////////////////////////////////// MatAllocator //////////////////////////////////////
269
270 struct CV_EXPORTS UMatData;
271
272 /*!
273    Custom array allocator
274
275 */
276 class CV_EXPORTS MatAllocator
277 {
278 public:
279     MatAllocator() {}
280     virtual ~MatAllocator() {}
281
282     // let's comment it off for now to detect and fix all the uses of allocator
283     //virtual void allocate(int dims, const int* sizes, int type, int*& refcount,
284     //                      uchar*& datastart, uchar*& data, size_t* step) = 0;
285     //virtual void deallocate(int* refcount, uchar* datastart, uchar* data) = 0;
286     virtual UMatData* allocate(int dims, const int* sizes, int type,
287                                void* data, size_t* step, int flags) const = 0;
288     virtual bool allocate(UMatData* data, int accessflags) const = 0;
289     virtual void deallocate(UMatData* data) const = 0;
290     virtual void map(UMatData* data, int accessflags) const;
291     virtual void unmap(UMatData* data) const;
292     virtual void download(UMatData* data, void* dst, int dims, const size_t sz[],
293                           const size_t srcofs[], const size_t srcstep[],
294                           const size_t dststep[]) const;
295     virtual void upload(UMatData* data, const void* src, int dims, const size_t sz[],
296                         const size_t dstofs[], const size_t dststep[],
297                         const size_t srcstep[]) const;
298     virtual void copy(UMatData* srcdata, UMatData* dstdata, int dims, const size_t sz[],
299                       const size_t srcofs[], const size_t srcstep[],
300                       const size_t dstofs[], const size_t dststep[], bool sync) const;
301 };
302
303
304 //////////////////////////////// MatCommaInitializer //////////////////////////////////
305
306 /*!
307  Comma-separated Matrix Initializer
308
309  The class instances are usually not created explicitly.
310  Instead, they are created on "matrix << firstValue" operator.
311
312  The sample below initializes 2x2 rotation matrix:
313
314  \code
315  double angle = 30, a = cos(angle*CV_PI/180), b = sin(angle*CV_PI/180);
316  Mat R = (Mat_<double>(2,2) << a, -b, b, a);
317  \endcode
318 */
319 template<typename _Tp> class MatCommaInitializer_
320 {
321 public:
322     //! the constructor, created by "matrix << firstValue" operator, where matrix is cv::Mat
323     MatCommaInitializer_(Mat_<_Tp>* _m);
324     //! the operator that takes the next value and put it to the matrix
325     template<typename T2> MatCommaInitializer_<_Tp>& operator , (T2 v);
326     //! another form of conversion operator
327     operator Mat_<_Tp>() const;
328 protected:
329     MatIterator_<_Tp> it;
330 };
331
332
333 /////////////////////////////////////// Mat ///////////////////////////////////////////
334
335 // note that umatdata might be allocated together
336 // with the matrix data, not as a separate object.
337 // therefore, it does not have constructor or destructor;
338 // it should be explicitly initialized using init().
339 struct CV_EXPORTS UMatData
340 {
341     enum { COPY_ON_MAP=1, HOST_COPY_OBSOLETE=2,
342         DEVICE_COPY_OBSOLETE=4, TEMP_UMAT=8, TEMP_COPIED_UMAT=24,
343         USER_ALLOCATED=32 };
344     UMatData(const MatAllocator* allocator);
345     ~UMatData();
346
347     // provide atomic access to the structure
348     void lock();
349     void unlock();
350
351     bool hostCopyObsolete() const;
352     bool deviceCopyObsolete() const;
353     bool copyOnMap() const;
354     bool tempUMat() const;
355     bool tempCopiedUMat() const;
356     void markHostCopyObsolete(bool flag);
357     void markDeviceCopyObsolete(bool flag);
358
359     const MatAllocator* prevAllocator;
360     const MatAllocator* currAllocator;
361     int urefcount;
362     int refcount;
363     uchar* data;
364     uchar* origdata;
365     size_t size;
366
367     int flags;
368     void* handle;
369     void* userdata;
370 };
371
372
373 struct CV_EXPORTS UMatDataAutoLock
374 {
375     UMatDataAutoLock(UMatData* u);
376     ~UMatDataAutoLock();
377     UMatData* u;
378 };
379
380
381 struct CV_EXPORTS MatSize
382 {
383     MatSize(int* _p);
384     Size operator()() const;
385     const int& operator[](int i) const;
386     int& operator[](int i);
387     operator const int*() const;
388     bool operator == (const MatSize& sz) const;
389     bool operator != (const MatSize& sz) const;
390
391     int* p;
392 };
393
394 struct CV_EXPORTS MatStep
395 {
396     MatStep();
397     MatStep(size_t s);
398     const size_t& operator[](int i) const;
399     size_t& operator[](int i);
400     operator size_t() const;
401     MatStep& operator = (size_t s);
402
403     size_t* p;
404     size_t buf[2];
405 protected:
406     MatStep& operator = (const MatStep&);
407 };
408
409  /*!
410    The n-dimensional matrix class.
411
412    The class represents an n-dimensional dense numerical array that can act as
413    a matrix, image, optical flow map, 3-focal tensor etc.
414    It is very similar to CvMat and CvMatND types from earlier versions of OpenCV,
415    and similarly to those types, the matrix can be multi-channel. It also fully supports ROI mechanism.
416
417    There are many different ways to create cv::Mat object. Here are the some popular ones:
418    <ul>
419    <li> using cv::Mat::create(nrows, ncols, type) method or
420      the similar constructor cv::Mat::Mat(nrows, ncols, type[, fill_value]) constructor.
421      A new matrix of the specified size and specifed type will be allocated.
422      "type" has the same meaning as in cvCreateMat function,
423      e.g. CV_8UC1 means 8-bit single-channel matrix, CV_32FC2 means 2-channel (i.e. complex)
424      floating-point matrix etc:
425
426      \code
427      // make 7x7 complex matrix filled with 1+3j.
428      cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
429      // and now turn M to 100x60 15-channel 8-bit matrix.
430      // The old content will be deallocated
431      M.create(100,60,CV_8UC(15));
432      \endcode
433
434      As noted in the introduction of this chapter, Mat::create()
435      will only allocate a new matrix when the current matrix dimensionality
436      or type are different from the specified.
437
438    <li> by using a copy constructor or assignment operator, where on the right side it can
439      be a matrix or expression, see below. Again, as noted in the introduction,
440      matrix assignment is O(1) operation because it only copies the header
441      and increases the reference counter. cv::Mat::clone() method can be used to get a full
442      (a.k.a. deep) copy of the matrix when you need it.
443
444    <li> by constructing a header for a part of another matrix. It can be a single row, single column,
445      several rows, several columns, rectangular region in the matrix (called a minor in algebra) or
446      a diagonal. Such operations are also O(1), because the new header will reference the same data.
447      You can actually modify a part of the matrix using this feature, e.g.
448
449      \code
450      // add 5-th row, multiplied by 3 to the 3rd row
451      M.row(3) = M.row(3) + M.row(5)*3;
452
453      // now copy 7-th column to the 1-st column
454      // M.col(1) = M.col(7); // this will not work
455      Mat M1 = M.col(1);
456      M.col(7).copyTo(M1);
457
458      // create new 320x240 image
459      cv::Mat img(Size(320,240),CV_8UC3);
460      // select a roi
461      cv::Mat roi(img, Rect(10,10,100,100));
462      // fill the ROI with (0,255,0) (which is green in RGB space);
463      // the original 320x240 image will be modified
464      roi = Scalar(0,255,0);
465      \endcode
466
467      Thanks to the additional cv::Mat::datastart and cv::Mat::dataend members, it is possible to
468      compute the relative sub-matrix position in the main "container" matrix using cv::Mat::locateROI():
469
470      \code
471      Mat A = Mat::eye(10, 10, CV_32S);
472      // extracts A columns, 1 (inclusive) to 3 (exclusive).
473      Mat B = A(Range::all(), Range(1, 3));
474      // extracts B rows, 5 (inclusive) to 9 (exclusive).
475      // that is, C ~ A(Range(5, 9), Range(1, 3))
476      Mat C = B(Range(5, 9), Range::all());
477      Size size; Point ofs;
478      C.locateROI(size, ofs);
479      // size will be (width=10,height=10) and the ofs will be (x=1, y=5)
480      \endcode
481
482      As in the case of whole matrices, if you need a deep copy, use cv::Mat::clone() method
483      of the extracted sub-matrices.
484
485    <li> by making a header for user-allocated-data. It can be useful for
486       <ol>
487       <li> processing "foreign" data using OpenCV (e.g. when you implement
488          a DirectShow filter or a processing module for gstreamer etc.), e.g.
489
490          \code
491          void process_video_frame(const unsigned char* pixels,
492                                   int width, int height, int step)
493          {
494             cv::Mat img(height, width, CV_8UC3, pixels, step);
495             cv::GaussianBlur(img, img, cv::Size(7,7), 1.5, 1.5);
496          }
497          \endcode
498
499       <li> for quick initialization of small matrices and/or super-fast element access
500
501          \code
502          double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};
503          cv::Mat M = cv::Mat(3, 3, CV_64F, m).inv();
504          \endcode
505       </ol>
506
507        partial yet very common cases of this "user-allocated data" case are conversions
508        from CvMat and IplImage to cv::Mat. For this purpose there are special constructors
509        taking pointers to CvMat or IplImage and the optional
510        flag indicating whether to copy the data or not.
511
512        Backward conversion from cv::Mat to CvMat or IplImage is provided via cast operators
513        cv::Mat::operator CvMat() an cv::Mat::operator IplImage().
514        The operators do not copy the data.
515
516
517        \code
518        IplImage* img = cvLoadImage("greatwave.jpg", 1);
519        Mat mtx(img); // convert IplImage* -> cv::Mat
520        CvMat oldmat = mtx; // convert cv::Mat -> CvMat
521        CV_Assert(oldmat.cols == img->width && oldmat.rows == img->height &&
522            oldmat.data.ptr == (uchar*)img->imageData && oldmat.step == img->widthStep);
523        \endcode
524
525    <li> by using MATLAB-style matrix initializers, cv::Mat::zeros(), cv::Mat::ones(), cv::Mat::eye(), e.g.:
526
527    \code
528    // create a double-precision identity martix and add it to M.
529    M += Mat::eye(M.rows, M.cols, CV_64F);
530    \endcode
531
532    <li> by using comma-separated initializer:
533
534    \code
535    // create 3x3 double-precision identity matrix
536    Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
537    \endcode
538
539    here we first call constructor of cv::Mat_ class (that we describe further) with the proper matrix,
540    and then we just put "<<" operator followed by comma-separated values that can be constants,
541    variables, expressions etc. Also, note the extra parentheses that are needed to avoid compiler errors.
542
543    </ul>
544
545    Once matrix is created, it will be automatically managed by using reference-counting mechanism
546    (unless the matrix header is built on top of user-allocated data,
547    in which case you should handle the data by yourself).
548    The matrix data will be deallocated when no one points to it;
549    if you want to release the data pointed by a matrix header before the matrix destructor is called,
550    use cv::Mat::release().
551
552    The next important thing to learn about the matrix class is element access. Here is how the matrix is stored.
553    The elements are stored in row-major order (row by row). The cv::Mat::data member points to the first element of the first row,
554    cv::Mat::rows contains the number of matrix rows and cv::Mat::cols - the number of matrix columns. There is yet another member,
555    cv::Mat::step that is used to actually compute address of a matrix element. cv::Mat::step is needed because the matrix can be
556    a part of another matrix or because there can some padding space in the end of each row for a proper alignment.
557
558    \image html roi.png
559
560    Given these parameters, address of the matrix element M_{ij} is computed as following:
561
562    addr(M_{ij})=M.data + M.step*i + j*M.elemSize()
563
564    if you know the matrix element type, e.g. it is float, then you can use cv::Mat::at() method:
565
566    addr(M_{ij})=&M.at<float>(i,j)
567
568    (where & is used to convert the reference returned by cv::Mat::at() to a pointer).
569    if you need to process a whole row of matrix, the most efficient way is to get
570    the pointer to the row first, and then just use plain C operator []:
571
572    \code
573    // compute sum of positive matrix elements
574    // (assuming that M is double-precision matrix)
575    double sum=0;
576    for(int i = 0; i < M.rows; i++)
577    {
578        const double* Mi = M.ptr<double>(i);
579        for(int j = 0; j < M.cols; j++)
580            sum += std::max(Mi[j], 0.);
581    }
582    \endcode
583
584    Some operations, like the above one, do not actually depend on the matrix shape,
585    they just process elements of a matrix one by one (or elements from multiple matrices
586    that are sitting in the same place, e.g. matrix addition). Such operations are called
587    element-wise and it makes sense to check whether all the input/output matrices are continuous,
588    i.e. have no gaps in the end of each row, and if yes, process them as a single long row:
589
590    \code
591    // compute sum of positive matrix elements, optimized variant
592    double sum=0;
593    int cols = M.cols, rows = M.rows;
594    if(M.isContinuous())
595    {
596        cols *= rows;
597        rows = 1;
598    }
599    for(int i = 0; i < rows; i++)
600    {
601        const double* Mi = M.ptr<double>(i);
602        for(int j = 0; j < cols; j++)
603            sum += std::max(Mi[j], 0.);
604    }
605    \endcode
606    in the case of continuous matrix the outer loop body will be executed just once,
607    so the overhead will be smaller, which will be especially noticeable in the case of small matrices.
608
609    Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows:
610    \code
611    // compute sum of positive matrix elements, iterator-based variant
612    double sum=0;
613    MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>();
614    for(; it != it_end; ++it)
615        sum += std::max(*it, 0.);
616    \endcode
617
618    The matrix iterators are random-access iterators, so they can be passed
619    to any STL algorithm, including std::sort().
620 */
621 class CV_EXPORTS Mat
622 {
623 public:
624     //! default constructor
625     Mat();
626     //! constructs 2D matrix of the specified size and type
627     // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
628     Mat(int rows, int cols, int type);
629     Mat(Size size, int type);
630     //! constucts 2D matrix and fills it with the specified value _s.
631     Mat(int rows, int cols, int type, const Scalar& s);
632     Mat(Size size, int type, const Scalar& s);
633
634     //! constructs n-dimensional matrix
635     Mat(int ndims, const int* sizes, int type);
636     Mat(int ndims, const int* sizes, int type, const Scalar& s);
637
638     //! copy constructor
639     Mat(const Mat& m);
640     //! constructor for matrix headers pointing to user-allocated data
641     Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
642     Mat(Size size, int type, void* data, size_t step=AUTO_STEP);
643     Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);
644
645     //! creates a matrix header for a part of the bigger matrix
646     Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
647     Mat(const Mat& m, const Rect& roi);
648     Mat(const Mat& m, const Range* ranges);
649     //! builds matrix from std::vector with or without copying the data
650     template<typename _Tp> explicit Mat(const std::vector<_Tp>& vec, bool copyData=false);
651     //! builds matrix from cv::Vec; the data is copied by default
652     template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true);
653     //! builds matrix from cv::Matx; the data is copied by default
654     template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true);
655     //! builds matrix from a 2D point
656     template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);
657     //! builds matrix from a 3D point
658     template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true);
659     //! builds matrix from comma initializer
660     template<typename _Tp> explicit Mat(const MatCommaInitializer_<_Tp>& commaInitializer);
661
662     //! download data from GpuMat
663     explicit Mat(const cuda::GpuMat& m);
664
665     //! destructor - calls release()
666     ~Mat();
667     //! assignment operators
668     Mat& operator = (const Mat& m);
669     Mat& operator = (const MatExpr& expr);
670
671     //! retrieve UMat from Mat
672     UMat getUMat(int accessFlags) const;
673
674     //! returns a new matrix header for the specified row
675     Mat row(int y) const;
676     //! returns a new matrix header for the specified column
677     Mat col(int x) const;
678     //! ... for the specified row span
679     Mat rowRange(int startrow, int endrow) const;
680     Mat rowRange(const Range& r) const;
681     //! ... for the specified column span
682     Mat colRange(int startcol, int endcol) const;
683     Mat colRange(const Range& r) const;
684     //! ... for the specified diagonal
685     // (d=0 - the main diagonal,
686     //  >0 - a diagonal from the lower half,
687     //  <0 - a diagonal from the upper half)
688     Mat diag(int d=0) const;
689     //! constructs a square diagonal matrix which main diagonal is vector "d"
690     static Mat diag(const Mat& d);
691
692     //! returns deep copy of the matrix, i.e. the data is copied
693     Mat clone() const;
694     //! copies the matrix content to "m".
695     // It calls m.create(this->size(), this->type()).
696     void copyTo( OutputArray m ) const;
697     //! copies those matrix elements to "m" that are marked with non-zero mask elements.
698     void copyTo( OutputArray m, InputArray mask ) const;
699     //! converts matrix to another datatype with optional scalng. See cvConvertScale.
700     void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
701
702     void assignTo( Mat& m, int type=-1 ) const;
703
704     //! sets every matrix element to s
705     Mat& operator = (const Scalar& s);
706     //! sets some of the matrix elements to s, according to the mask
707     Mat& setTo(InputArray value, InputArray mask=noArray());
708     //! creates alternative matrix header for the same data, with different
709     // number of channels and/or different number of rows. see cvReshape.
710     Mat reshape(int cn, int rows=0) const;
711     Mat reshape(int cn, int newndims, const int* newsz) const;
712
713     //! matrix transposition by means of matrix expressions
714     MatExpr t() const;
715     //! matrix inversion by means of matrix expressions
716     MatExpr inv(int method=DECOMP_LU) const;
717     //! per-element matrix multiplication by means of matrix expressions
718     MatExpr mul(InputArray m, double scale=1) const;
719
720     //! computes cross-product of 2 3D vectors
721     Mat cross(InputArray m) const;
722     //! computes dot-product
723     double dot(InputArray m) const;
724
725     //! Matlab-style matrix initialization
726     static MatExpr zeros(int rows, int cols, int type);
727     static MatExpr zeros(Size size, int type);
728     static MatExpr zeros(int ndims, const int* sz, int type);
729     static MatExpr ones(int rows, int cols, int type);
730     static MatExpr ones(Size size, int type);
731     static MatExpr ones(int ndims, const int* sz, int type);
732     static MatExpr eye(int rows, int cols, int type);
733     static MatExpr eye(Size size, int type);
734
735     //! allocates new matrix data unless the matrix already has specified size and type.
736     // previous data is unreferenced if needed.
737     void create(int rows, int cols, int type);
738     void create(Size size, int type);
739     void create(int ndims, const int* sizes, int type);
740
741     //! increases the reference counter; use with care to avoid memleaks
742     void addref();
743     //! decreases reference counter;
744     // deallocates the data when reference counter reaches 0.
745     void release();
746
747     //! deallocates the matrix data
748     void deallocate();
749     //! internal use function; properly re-allocates _size, _step arrays
750     void copySize(const Mat& m);
751
752     //! reserves enough space to fit sz hyper-planes
753     void reserve(size_t sz);
754     //! resizes matrix to the specified number of hyper-planes
755     void resize(size_t sz);
756     //! resizes matrix to the specified number of hyper-planes; initializes the newly added elements
757     void resize(size_t sz, const Scalar& s);
758     //! internal function
759     void push_back_(const void* elem);
760     //! adds element to the end of 1d matrix (or possibly multiple elements when _Tp=Mat)
761     template<typename _Tp> void push_back(const _Tp& elem);
762     template<typename _Tp> void push_back(const Mat_<_Tp>& elem);
763     void push_back(const Mat& m);
764     //! removes several hyper-planes from bottom of the matrix
765     void pop_back(size_t nelems=1);
766
767     //! locates matrix header within a parent matrix. See below
768     void locateROI( Size& wholeSize, Point& ofs ) const;
769     //! moves/resizes the current matrix ROI inside the parent matrix.
770     Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
771     //! extracts a rectangular sub-matrix
772     // (this is a generalized form of row, rowRange etc.)
773     Mat operator()( Range rowRange, Range colRange ) const;
774     Mat operator()( const Rect& roi ) const;
775     Mat operator()( const Range* ranges ) const;
776
777     // //! converts header to CvMat; no data is copied
778     // operator CvMat() const;
779     // //! converts header to CvMatND; no data is copied
780     // operator CvMatND() const;
781     // //! converts header to IplImage; no data is copied
782     // operator IplImage() const;
783
784     template<typename _Tp> operator std::vector<_Tp>() const;
785     template<typename _Tp, int n> operator Vec<_Tp, n>() const;
786     template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
787
788     //! returns true iff the matrix data is continuous
789     // (i.e. when there are no gaps between successive rows).
790     // similar to CV_IS_MAT_CONT(cvmat->type)
791     bool isContinuous() const;
792
793     //! returns true if the matrix is a submatrix of another matrix
794     bool isSubmatrix() const;
795
796     //! returns element size in bytes,
797     // similar to CV_ELEM_SIZE(cvmat->type)
798     size_t elemSize() const;
799     //! returns the size of element channel in bytes.
800     size_t elemSize1() const;
801     //! returns element type, similar to CV_MAT_TYPE(cvmat->type)
802     int type() const;
803     //! returns element type, similar to CV_MAT_DEPTH(cvmat->type)
804     int depth() const;
805     //! returns element type, similar to CV_MAT_CN(cvmat->type)
806     int channels() const;
807     //! returns step/elemSize1()
808     size_t step1(int i=0) const;
809     //! returns true if matrix data is NULL
810     bool empty() const;
811     //! returns the total number of matrix elements
812     size_t total() const;
813
814     //! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise
815     int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;
816
817     //! returns pointer to i0-th submatrix along the dimension #0
818     uchar* ptr(int i0=0);
819     const uchar* ptr(int i0=0) const;
820
821     //! returns pointer to (i0,i1) submatrix along the dimensions #0 and #1
822     uchar* ptr(int i0, int i1);
823     const uchar* ptr(int i0, int i1) const;
824
825     //! returns pointer to (i0,i1,i3) submatrix along the dimensions #0, #1, #2
826     uchar* ptr(int i0, int i1, int i2);
827     const uchar* ptr(int i0, int i1, int i2) const;
828
829     //! returns pointer to the matrix element
830     uchar* ptr(const int* idx);
831     //! returns read-only pointer to the matrix element
832     const uchar* ptr(const int* idx) const;
833
834     template<int n> uchar* ptr(const Vec<int, n>& idx);
835     template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
836
837     //! template version of the above method
838     template<typename _Tp> _Tp* ptr(int i0=0);
839     template<typename _Tp> const _Tp* ptr(int i0=0) const;
840
841     template<typename _Tp> _Tp* ptr(int i0, int i1);
842     template<typename _Tp> const _Tp* ptr(int i0, int i1) const;
843
844     template<typename _Tp> _Tp* ptr(int i0, int i1, int i2);
845     template<typename _Tp> const _Tp* ptr(int i0, int i1, int i2) const;
846
847     template<typename _Tp> _Tp* ptr(const int* idx);
848     template<typename _Tp> const _Tp* ptr(const int* idx) const;
849
850     template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);
851     template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;
852
853     //! the same as above, with the pointer dereferencing
854     template<typename _Tp> _Tp& at(int i0=0);
855     template<typename _Tp> const _Tp& at(int i0=0) const;
856
857     template<typename _Tp> _Tp& at(int i0, int i1);
858     template<typename _Tp> const _Tp& at(int i0, int i1) const;
859
860     template<typename _Tp> _Tp& at(int i0, int i1, int i2);
861     template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
862
863     template<typename _Tp> _Tp& at(const int* idx);
864     template<typename _Tp> const _Tp& at(const int* idx) const;
865
866     template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
867     template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
868
869     //! special versions for 2D arrays (especially convenient for referencing image pixels)
870     template<typename _Tp> _Tp& at(Point pt);
871     template<typename _Tp> const _Tp& at(Point pt) const;
872
873     //! template methods for iteration over matrix elements.
874     // the iterators take care of skipping gaps in the end of rows (if any)
875     template<typename _Tp> MatIterator_<_Tp> begin();
876     template<typename _Tp> MatIterator_<_Tp> end();
877     template<typename _Tp> MatConstIterator_<_Tp> begin() const;
878     template<typename _Tp> MatConstIterator_<_Tp> end() const;
879
880     enum { MAGIC_VAL  = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG };
881     enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 };
882
883     /*! includes several bit-fields:
884          - the magic signature
885          - continuity flag
886          - depth
887          - number of channels
888      */
889     int flags;
890     //! the matrix dimensionality, >= 2
891     int dims;
892     //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
893     int rows, cols;
894     //! pointer to the data
895     uchar* data;
896
897     //! helper fields used in locateROI and adjustROI
898     uchar* datastart;
899     uchar* dataend;
900     uchar* datalimit;
901
902     //! custom allocator
903     MatAllocator* allocator;
904     //! and the standard allocator
905     static MatAllocator* getStdAllocator();
906
907     //! interaction with UMat
908     UMatData* u;
909
910     MatSize size;
911     MatStep step;
912
913 protected:
914 };
915
916
917 ///////////////////////////////// Mat_<_Tp> ////////////////////////////////////
918
919 /*!
920  Template matrix class derived from Mat
921
922  The class Mat_ is a "thin" template wrapper on top of cv::Mat. It does not have any extra data fields,
923  nor it or cv::Mat have any virtual methods and thus references or pointers to these two classes
924  can be safely converted one to another. But do it with care, for example:
925
926  \code
927  // create 100x100 8-bit matrix
928  Mat M(100,100,CV_8U);
929  // this will compile fine. no any data conversion will be done.
930  Mat_<float>& M1 = (Mat_<float>&)M;
931  // the program will likely crash at the statement below
932  M1(99,99) = 1.f;
933  \endcode
934
935  While cv::Mat is sufficient in most cases, cv::Mat_ can be more convenient if you use a lot of element
936  access operations and if you know matrix type at compile time.
937  Note that cv::Mat::at<_Tp>(int y, int x) and cv::Mat_<_Tp>::operator ()(int y, int x) do absolutely the
938  same thing and run at the same speed, but the latter is certainly shorter:
939
940  \code
941  Mat_<double> M(20,20);
942  for(int i = 0; i < M.rows; i++)
943     for(int j = 0; j < M.cols; j++)
944        M(i,j) = 1./(i+j+1);
945  Mat E, V;
946  eigen(M,E,V);
947  cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0);
948  \endcode
949
950  It is easy to use Mat_ for multi-channel images/matrices - just pass cv::Vec as cv::Mat_ template parameter:
951
952  \code
953  // allocate 320x240 color image and fill it with green (in RGB space)
954  Mat_<Vec3b> img(240, 320, Vec3b(0,255,0));
955  // now draw a diagonal white line
956  for(int i = 0; i < 100; i++)
957      img(i,i)=Vec3b(255,255,255);
958  // and now modify the 2nd (red) channel of each pixel
959  for(int i = 0; i < img.rows; i++)
960     for(int j = 0; j < img.cols; j++)
961        img(i,j)[2] ^= (uchar)(i ^ j); // img(y,x)[c] accesses c-th channel of the pixel (x,y)
962  \endcode
963 */
964 template<typename _Tp> class Mat_ : public Mat
965 {
966 public:
967     typedef _Tp value_type;
968     typedef typename DataType<_Tp>::channel_type channel_type;
969     typedef MatIterator_<_Tp> iterator;
970     typedef MatConstIterator_<_Tp> const_iterator;
971
972     //! default constructor
973     Mat_();
974     //! equivalent to Mat(_rows, _cols, DataType<_Tp>::type)
975     Mat_(int _rows, int _cols);
976     //! constructor that sets each matrix element to specified value
977     Mat_(int _rows, int _cols, const _Tp& value);
978     //! equivalent to Mat(_size, DataType<_Tp>::type)
979     explicit Mat_(Size _size);
980     //! constructor that sets each matrix element to specified value
981     Mat_(Size _size, const _Tp& value);
982     //! n-dim array constructor
983     Mat_(int _ndims, const int* _sizes);
984     //! n-dim array constructor that sets each matrix element to specified value
985     Mat_(int _ndims, const int* _sizes, const _Tp& value);
986     //! copy/conversion contructor. If m is of different type, it's converted
987     Mat_(const Mat& m);
988     //! copy constructor
989     Mat_(const Mat_& m);
990     //! constructs a matrix on top of user-allocated data. step is in bytes(!!!), regardless of the type
991     Mat_(int _rows, int _cols, _Tp* _data, size_t _step=AUTO_STEP);
992     //! constructs n-dim matrix on top of user-allocated data. steps are in bytes(!!!), regardless of the type
993     Mat_(int _ndims, const int* _sizes, _Tp* _data, const size_t* _steps=0);
994     //! selects a submatrix
995     Mat_(const Mat_& m, const Range& rowRange, const Range& colRange=Range::all());
996     //! selects a submatrix
997     Mat_(const Mat_& m, const Rect& roi);
998     //! selects a submatrix, n-dim version
999     Mat_(const Mat_& m, const Range* ranges);
1000     //! from a matrix expression
1001     explicit Mat_(const MatExpr& e);
1002     //! makes a matrix out of Vec, std::vector, Point_ or Point3_. The matrix will have a single column
1003     explicit Mat_(const std::vector<_Tp>& vec, bool copyData=false);
1004     template<int n> explicit Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData=true);
1005     template<int m, int n> explicit Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& mtx, bool copyData=true);
1006     explicit Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true);
1007     explicit Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData=true);
1008     explicit Mat_(const MatCommaInitializer_<_Tp>& commaInitializer);
1009
1010     Mat_& operator = (const Mat& m);
1011     Mat_& operator = (const Mat_& m);
1012     //! set all the elements to s.
1013     Mat_& operator = (const _Tp& s);
1014     //! assign a matrix expression
1015     Mat_& operator = (const MatExpr& e);
1016
1017     //! iterators; they are smart enough to skip gaps in the end of rows
1018     iterator begin();
1019     iterator end();
1020     const_iterator begin() const;
1021     const_iterator end() const;
1022
1023     //! equivalent to Mat::create(_rows, _cols, DataType<_Tp>::type)
1024     void create(int _rows, int _cols);
1025     //! equivalent to Mat::create(_size, DataType<_Tp>::type)
1026     void create(Size _size);
1027     //! equivalent to Mat::create(_ndims, _sizes, DatType<_Tp>::type)
1028     void create(int _ndims, const int* _sizes);
1029     //! cross-product
1030     Mat_ cross(const Mat_& m) const;
1031     //! data type conversion
1032     template<typename T2> operator Mat_<T2>() const;
1033     //! overridden forms of Mat::row() etc.
1034     Mat_ row(int y) const;
1035     Mat_ col(int x) const;
1036     Mat_ diag(int d=0) const;
1037     Mat_ clone() const;
1038
1039     //! overridden forms of Mat::elemSize() etc.
1040     size_t elemSize() const;
1041     size_t elemSize1() const;
1042     int type() const;
1043     int depth() const;
1044     int channels() const;
1045     size_t step1(int i=0) const;
1046     //! returns step()/sizeof(_Tp)
1047     size_t stepT(int i=0) const;
1048
1049     //! overridden forms of Mat::zeros() etc. Data type is omitted, of course
1050     static MatExpr zeros(int rows, int cols);
1051     static MatExpr zeros(Size size);
1052     static MatExpr zeros(int _ndims, const int* _sizes);
1053     static MatExpr ones(int rows, int cols);
1054     static MatExpr ones(Size size);
1055     static MatExpr ones(int _ndims, const int* _sizes);
1056     static MatExpr eye(int rows, int cols);
1057     static MatExpr eye(Size size);
1058
1059     //! some more overriden methods
1060     Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright );
1061     Mat_ operator()( const Range& rowRange, const Range& colRange ) const;
1062     Mat_ operator()( const Rect& roi ) const;
1063     Mat_ operator()( const Range* ranges ) const;
1064
1065     //! more convenient forms of row and element access operators
1066     _Tp* operator [](int y);
1067     const _Tp* operator [](int y) const;
1068
1069     //! returns reference to the specified element
1070     _Tp& operator ()(const int* idx);
1071     //! returns read-only reference to the specified element
1072     const _Tp& operator ()(const int* idx) const;
1073
1074     //! returns reference to the specified element
1075     template<int n> _Tp& operator ()(const Vec<int, n>& idx);
1076     //! returns read-only reference to the specified element
1077     template<int n> const _Tp& operator ()(const Vec<int, n>& idx) const;
1078
1079     //! returns reference to the specified element (1D case)
1080     _Tp& operator ()(int idx0);
1081     //! returns read-only reference to the specified element (1D case)
1082     const _Tp& operator ()(int idx0) const;
1083     //! returns reference to the specified element (2D case)
1084     _Tp& operator ()(int idx0, int idx1);
1085     //! returns read-only reference to the specified element (2D case)
1086     const _Tp& operator ()(int idx0, int idx1) const;
1087     //! returns reference to the specified element (3D case)
1088     _Tp& operator ()(int idx0, int idx1, int idx2);
1089     //! returns read-only reference to the specified element (3D case)
1090     const _Tp& operator ()(int idx0, int idx1, int idx2) const;
1091
1092     _Tp& operator ()(Point pt);
1093     const _Tp& operator ()(Point pt) const;
1094
1095     //! conversion to vector.
1096     operator std::vector<_Tp>() const;
1097     //! conversion to Vec
1098     template<int n> operator Vec<typename DataType<_Tp>::channel_type, n>() const;
1099     //! conversion to Matx
1100     template<int m, int n> operator Matx<typename DataType<_Tp>::channel_type, m, n>() const;
1101 };
1102
1103 typedef Mat_<uchar> Mat1b;
1104 typedef Mat_<Vec2b> Mat2b;
1105 typedef Mat_<Vec3b> Mat3b;
1106 typedef Mat_<Vec4b> Mat4b;
1107
1108 typedef Mat_<short> Mat1s;
1109 typedef Mat_<Vec2s> Mat2s;
1110 typedef Mat_<Vec3s> Mat3s;
1111 typedef Mat_<Vec4s> Mat4s;
1112
1113 typedef Mat_<ushort> Mat1w;
1114 typedef Mat_<Vec2w> Mat2w;
1115 typedef Mat_<Vec3w> Mat3w;
1116 typedef Mat_<Vec4w> Mat4w;
1117
1118 typedef Mat_<int>   Mat1i;
1119 typedef Mat_<Vec2i> Mat2i;
1120 typedef Mat_<Vec3i> Mat3i;
1121 typedef Mat_<Vec4i> Mat4i;
1122
1123 typedef Mat_<float> Mat1f;
1124 typedef Mat_<Vec2f> Mat2f;
1125 typedef Mat_<Vec3f> Mat3f;
1126 typedef Mat_<Vec4f> Mat4f;
1127
1128 typedef Mat_<double> Mat1d;
1129 typedef Mat_<Vec2d> Mat2d;
1130 typedef Mat_<Vec3d> Mat3d;
1131 typedef Mat_<Vec4d> Mat4d;
1132
1133
1134 class CV_EXPORTS UMatExpr;
1135
1136 class CV_EXPORTS UMat
1137 {
1138 public:
1139     //! default constructor
1140     UMat();
1141     //! constructs 2D matrix of the specified size and type
1142     // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
1143     UMat(int rows, int cols, int type);
1144     UMat(Size size, int type);
1145     //! constucts 2D matrix and fills it with the specified value _s.
1146     UMat(int rows, int cols, int type, const Scalar& s);
1147     UMat(Size size, int type, const Scalar& s);
1148
1149     //! constructs n-dimensional matrix
1150     UMat(int ndims, const int* sizes, int type);
1151     UMat(int ndims, const int* sizes, int type, const Scalar& s);
1152
1153     //! copy constructor
1154     UMat(const UMat& m);
1155
1156     //! creates a matrix header for a part of the bigger matrix
1157     UMat(const UMat& m, const Range& rowRange, const Range& colRange=Range::all());
1158     UMat(const UMat& m, const Rect& roi);
1159     UMat(const UMat& m, const Range* ranges);
1160     //! builds matrix from std::vector with or without copying the data
1161     template<typename _Tp> explicit UMat(const std::vector<_Tp>& vec, bool copyData=false);
1162     //! builds matrix from cv::Vec; the data is copied by default
1163     template<typename _Tp, int n> explicit UMat(const Vec<_Tp, n>& vec, bool copyData=true);
1164     //! builds matrix from cv::Matx; the data is copied by default
1165     template<typename _Tp, int m, int n> explicit UMat(const Matx<_Tp, m, n>& mtx, bool copyData=true);
1166     //! builds matrix from a 2D point
1167     template<typename _Tp> explicit UMat(const Point_<_Tp>& pt, bool copyData=true);
1168     //! builds matrix from a 3D point
1169     template<typename _Tp> explicit UMat(const Point3_<_Tp>& pt, bool copyData=true);
1170     //! builds matrix from comma initializer
1171     template<typename _Tp> explicit UMat(const MatCommaInitializer_<_Tp>& commaInitializer);
1172
1173     //! destructor - calls release()
1174     ~UMat();
1175     //! assignment operators
1176     UMat& operator = (const UMat& m);
1177     UMat& operator = (const UMatExpr& expr);
1178
1179     Mat getMat(int flags) const;
1180
1181     //! returns a new matrix header for the specified row
1182     UMat row(int y) const;
1183     //! returns a new matrix header for the specified column
1184     UMat col(int x) const;
1185     //! ... for the specified row span
1186     UMat rowRange(int startrow, int endrow) const;
1187     UMat rowRange(const Range& r) const;
1188     //! ... for the specified column span
1189     UMat colRange(int startcol, int endcol) const;
1190     UMat colRange(const Range& r) const;
1191     //! ... for the specified diagonal
1192     // (d=0 - the main diagonal,
1193     //  >0 - a diagonal from the lower half,
1194     //  <0 - a diagonal from the upper half)
1195     UMat diag(int d=0) const;
1196     //! constructs a square diagonal matrix which main diagonal is vector "d"
1197     static UMat diag(const UMat& d);
1198
1199     //! returns deep copy of the matrix, i.e. the data is copied
1200     UMat clone() const;
1201     //! copies the matrix content to "m".
1202     // It calls m.create(this->size(), this->type()).
1203     void copyTo( OutputArray m ) const;
1204     //! copies those matrix elements to "m" that are marked with non-zero mask elements.
1205     void copyTo( OutputArray m, InputArray mask ) const;
1206     //! converts matrix to another datatype with optional scalng. See cvConvertScale.
1207     void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;
1208
1209     void assignTo( UMat& m, int type=-1 ) const;
1210
1211     //! sets every matrix element to s
1212     UMat& operator = (const Scalar& s);
1213     //! sets some of the matrix elements to s, according to the mask
1214     UMat& setTo(InputArray value, InputArray mask=noArray());
1215     //! creates alternative matrix header for the same data, with different
1216     // number of channels and/or different number of rows. see cvReshape.
1217     UMat reshape(int cn, int rows=0) const;
1218     UMat reshape(int cn, int newndims, const int* newsz) const;
1219
1220     //! matrix transposition by means of matrix expressions
1221     UMatExpr t() const;
1222     //! matrix inversion by means of matrix expressions
1223     UMatExpr inv(int method=DECOMP_LU) const;
1224     //! per-element matrix multiplication by means of matrix expressions
1225     UMatExpr mul(InputArray m, double scale=1) const;
1226
1227     //! computes cross-product of 2 3D vectors
1228     UMat cross(InputArray m) const;
1229     //! computes dot-product
1230     double dot(InputArray m) const;
1231
1232     //! Matlab-style matrix initialization
1233     static UMatExpr zeros(int rows, int cols, int type);
1234     static UMatExpr zeros(Size size, int type);
1235     static UMatExpr zeros(int ndims, const int* sz, int type);
1236     static UMatExpr ones(int rows, int cols, int type);
1237     static UMatExpr ones(Size size, int type);
1238     static UMatExpr ones(int ndims, const int* sz, int type);
1239     static UMatExpr eye(int rows, int cols, int type);
1240     static UMatExpr eye(Size size, int type);
1241
1242     //! allocates new matrix data unless the matrix already has specified size and type.
1243     // previous data is unreferenced if needed.
1244     void create(int rows, int cols, int type);
1245     void create(Size size, int type);
1246     void create(int ndims, const int* sizes, int type);
1247
1248     //! increases the reference counter; use with care to avoid memleaks
1249     void addref();
1250     //! decreases reference counter;
1251     // deallocates the data when reference counter reaches 0.
1252     void release();
1253
1254     //! deallocates the matrix data
1255     void deallocate();
1256     //! internal use function; properly re-allocates _size, _step arrays
1257     void copySize(const UMat& m);
1258
1259     //! locates matrix header within a parent matrix. See below
1260     void locateROI( Size& wholeSize, Point& ofs ) const;
1261     //! moves/resizes the current matrix ROI inside the parent matrix.
1262     UMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
1263     //! extracts a rectangular sub-matrix
1264     // (this is a generalized form of row, rowRange etc.)
1265     UMat operator()( Range rowRange, Range colRange ) const;
1266     UMat operator()( const Rect& roi ) const;
1267     UMat operator()( const Range* ranges ) const;
1268
1269     //! returns true iff the matrix data is continuous
1270     // (i.e. when there are no gaps between successive rows).
1271     // similar to CV_IS_MAT_CONT(cvmat->type)
1272     bool isContinuous() const;
1273
1274     //! returns true if the matrix is a submatrix of another matrix
1275     bool isSubmatrix() const;
1276
1277     //! returns element size in bytes,
1278     // similar to CV_ELEM_SIZE(cvmat->type)
1279     size_t elemSize() const;
1280     //! returns the size of element channel in bytes.
1281     size_t elemSize1() const;
1282     //! returns element type, similar to CV_MAT_TYPE(cvmat->type)
1283     int type() const;
1284     //! returns element type, similar to CV_MAT_DEPTH(cvmat->type)
1285     int depth() const;
1286     //! returns element type, similar to CV_MAT_CN(cvmat->type)
1287     int channels() const;
1288     //! returns step/elemSize1()
1289     size_t step1(int i=0) const;
1290     //! returns true if matrix data is NULL
1291     bool empty() const;
1292     //! returns the total number of matrix elements
1293     size_t total() const;
1294
1295     //! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise
1296     int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;
1297
1298     void* handle(int accessFlags) const;
1299     void ndoffset(size_t* ofs) const;
1300
1301     enum { MAGIC_VAL  = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG };
1302     enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 };
1303
1304     /*! includes several bit-fields:
1305          - the magic signature
1306          - continuity flag
1307          - depth
1308          - number of channels
1309      */
1310     int flags;
1311     //! the matrix dimensionality, >= 2
1312     int dims;
1313     //! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
1314     int rows, cols;
1315
1316     //! custom allocator
1317     MatAllocator* allocator;
1318     //! and the standard allocator
1319     static MatAllocator* getStdAllocator();
1320
1321     // black-box container of UMat data
1322     UMatData* u;
1323
1324     // offset of the submatrix (or 0)
1325     size_t offset;
1326
1327     MatSize size;
1328     MatStep step;
1329
1330 protected:
1331 };
1332
1333
1334 /////////////////////////// multi-dimensional sparse matrix //////////////////////////
1335
1336 /*!
1337  Sparse matrix class.
1338
1339  The class represents multi-dimensional sparse numerical arrays. Such a sparse array can store elements
1340  of any type that cv::Mat is able to store. "Sparse" means that only non-zero elements
1341  are stored (though, as a result of some operations on a sparse matrix, some of its stored elements
1342  can actually become 0. It's user responsibility to detect such elements and delete them using cv::SparseMat::erase().
1343  The non-zero elements are stored in a hash table that grows when it's filled enough,
1344  so that the search time remains O(1) in average. Elements can be accessed using the following methods:
1345
1346  <ol>
1347  <li>Query operations: cv::SparseMat::ptr() and the higher-level cv::SparseMat::ref(),
1348       cv::SparseMat::value() and cv::SparseMat::find, for example:
1349  \code
1350  const int dims = 5;
1351  int size[] = {10, 10, 10, 10, 10};
1352  SparseMat sparse_mat(dims, size, CV_32F);
1353  for(int i = 0; i < 1000; i++)
1354  {
1355      int idx[dims];
1356      for(int k = 0; k < dims; k++)
1357         idx[k] = rand()%sparse_mat.size(k);
1358      sparse_mat.ref<float>(idx) += 1.f;
1359  }
1360  \endcode
1361
1362  <li>Sparse matrix iterators. Like cv::Mat iterators and unlike cv::Mat iterators, the sparse matrix iterators are STL-style,
1363  that is, the iteration is done as following:
1364  \code
1365  // prints elements of a sparse floating-point matrix and the sum of elements.
1366  SparseMatConstIterator_<float>
1367         it = sparse_mat.begin<float>(),
1368         it_end = sparse_mat.end<float>();
1369  double s = 0;
1370  int dims = sparse_mat.dims();
1371  for(; it != it_end; ++it)
1372  {
1373      // print element indices and the element value
1374      const Node* n = it.node();
1375      printf("(")
1376      for(int i = 0; i < dims; i++)
1377         printf("%3d%c", n->idx[i], i < dims-1 ? ',' : ')');
1378      printf(": %f\n", *it);
1379      s += *it;
1380  }
1381  printf("Element sum is %g\n", s);
1382  \endcode
1383  If you run this loop, you will notice that elements are enumerated
1384  in no any logical order (lexicographical etc.),
1385  they come in the same order as they stored in the hash table, i.e. semi-randomly.
1386
1387  You may collect pointers to the nodes and sort them to get the proper ordering.
1388  Note, however, that pointers to the nodes may become invalid when you add more
1389  elements to the matrix; this is because of possible buffer reallocation.
1390
1391  <li>A combination of the above 2 methods when you need to process 2 or more sparse
1392  matrices simultaneously, e.g. this is how you can compute unnormalized
1393  cross-correlation of the 2 floating-point sparse matrices:
1394  \code
1395  double crossCorr(const SparseMat& a, const SparseMat& b)
1396  {
1397      const SparseMat *_a = &a, *_b = &b;
1398      // if b contains less elements than a,
1399      // it's faster to iterate through b
1400      if(_a->nzcount() > _b->nzcount())
1401         std::swap(_a, _b);
1402      SparseMatConstIterator_<float> it = _a->begin<float>(),
1403                                     it_end = _a->end<float>();
1404      double ccorr = 0;
1405      for(; it != it_end; ++it)
1406      {
1407          // take the next element from the first matrix
1408          float avalue = *it;
1409          const Node* anode = it.node();
1410          // and try to find element with the same index in the second matrix.
1411          // since the hash value depends only on the element index,
1412          // we reuse hashvalue stored in the node
1413          float bvalue = _b->value<float>(anode->idx,&anode->hashval);
1414          ccorr += avalue*bvalue;
1415      }
1416      return ccorr;
1417  }
1418  \endcode
1419  </ol>
1420 */
1421 class CV_EXPORTS SparseMat
1422 {
1423 public:
1424     typedef SparseMatIterator iterator;
1425     typedef SparseMatConstIterator const_iterator;
1426
1427     enum { MAGIC_VAL=0x42FD0000, MAX_DIM=32, HASH_SCALE=0x5bd1e995, HASH_BIT=0x80000000 };
1428
1429     //! the sparse matrix header
1430     struct CV_EXPORTS Hdr
1431     {
1432         Hdr(int _dims, const int* _sizes, int _type);
1433         void clear();
1434         int refcount;
1435         int dims;
1436         int valueOffset;
1437         size_t nodeSize;
1438         size_t nodeCount;
1439         size_t freeList;
1440         std::vector<uchar> pool;
1441         std::vector<size_t> hashtab;
1442         int size[MAX_DIM];
1443     };
1444
1445     //! sparse matrix node - element of a hash table
1446     struct CV_EXPORTS Node
1447     {
1448         //! hash value
1449         size_t hashval;
1450         //! index of the next node in the same hash table entry
1451         size_t next;
1452         //! index of the matrix element
1453         int idx[MAX_DIM];
1454     };
1455
1456     //! default constructor
1457     SparseMat();
1458     //! creates matrix of the specified size and type
1459     SparseMat(int dims, const int* _sizes, int _type);
1460     //! copy constructor
1461     SparseMat(const SparseMat& m);
1462     //! converts dense 2d matrix to the sparse form
1463     /*!
1464      \param m the input matrix
1465     */
1466     explicit SparseMat(const Mat& m);
1467     //! converts old-style sparse matrix to the new-style. All the data is copied
1468     //SparseMat(const CvSparseMat* m);
1469     //! the destructor
1470     ~SparseMat();
1471
1472     //! assignment operator. This is O(1) operation, i.e. no data is copied
1473     SparseMat& operator = (const SparseMat& m);
1474     //! equivalent to the corresponding constructor
1475     SparseMat& operator = (const Mat& m);
1476
1477     //! creates full copy of the matrix
1478     SparseMat clone() const;
1479
1480     //! copies all the data to the destination matrix. All the previous content of m is erased
1481     void copyTo( SparseMat& m ) const;
1482     //! converts sparse matrix to dense matrix.
1483     void copyTo( Mat& m ) const;
1484     //! multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type
1485     void convertTo( SparseMat& m, int rtype, double alpha=1 ) const;
1486     //! converts sparse matrix to dense n-dim matrix with optional type conversion and scaling.
1487     /*!
1488       \param rtype The output matrix data type. When it is =-1, the output array will have the same data type as (*this)
1489       \param alpha The scale factor
1490       \param beta The optional delta added to the scaled values before the conversion
1491     */
1492     void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;
1493
1494     // not used now
1495     void assignTo( SparseMat& m, int type=-1 ) const;
1496
1497     //! reallocates sparse matrix.
1498     /*!
1499         If the matrix already had the proper size and type,
1500         it is simply cleared with clear(), otherwise,
1501         the old matrix is released (using release()) and the new one is allocated.
1502     */
1503     void create(int dims, const int* _sizes, int _type);
1504     //! sets all the sparse matrix elements to 0, which means clearing the hash table.
1505     void clear();
1506     //! manually increments the reference counter to the header.
1507     void addref();
1508     // decrements the header reference counter. When the counter reaches 0, the header and all the underlying data are deallocated.
1509     void release();
1510
1511     //! converts sparse matrix to the old-style representation; all the elements are copied.
1512     //operator CvSparseMat*() const;
1513     //! returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements)
1514     size_t elemSize() const;
1515     //! returns elemSize()/channels()
1516     size_t elemSize1() const;
1517
1518     //! returns type of sparse matrix elements
1519     int type() const;
1520     //! returns the depth of sparse matrix elements
1521     int depth() const;
1522     //! returns the number of channels
1523     int channels() const;
1524
1525     //! returns the array of sizes, or NULL if the matrix is not allocated
1526     const int* size() const;
1527     //! returns the size of i-th matrix dimension (or 0)
1528     int size(int i) const;
1529     //! returns the matrix dimensionality
1530     int dims() const;
1531     //! returns the number of non-zero elements (=the number of hash table nodes)
1532     size_t nzcount() const;
1533
1534     //! computes the element hash value (1D case)
1535     size_t hash(int i0) const;
1536     //! computes the element hash value (2D case)
1537     size_t hash(int i0, int i1) const;
1538     //! computes the element hash value (3D case)
1539     size_t hash(int i0, int i1, int i2) const;
1540     //! computes the element hash value (nD case)
1541     size_t hash(const int* idx) const;
1542
1543     //@{
1544     /*!
1545      specialized variants for 1D, 2D, 3D cases and the generic_type one for n-D case.
1546
1547      return pointer to the matrix element.
1548      <ul>
1549       <li>if the element is there (it's non-zero), the pointer to it is returned
1550       <li>if it's not there and createMissing=false, NULL pointer is returned
1551       <li>if it's not there and createMissing=true, then the new element
1552         is created and initialized with 0. Pointer to it is returned
1553       <li>if the optional hashval pointer is not NULL, the element hash value is
1554       not computed, but *hashval is taken instead.
1555      </ul>
1556     */
1557     //! returns pointer to the specified element (1D case)
1558     uchar* ptr(int i0, bool createMissing, size_t* hashval=0);
1559     //! returns pointer to the specified element (2D case)
1560     uchar* ptr(int i0, int i1, bool createMissing, size_t* hashval=0);
1561     //! returns pointer to the specified element (3D case)
1562     uchar* ptr(int i0, int i1, int i2, bool createMissing, size_t* hashval=0);
1563     //! returns pointer to the specified element (nD case)
1564     uchar* ptr(const int* idx, bool createMissing, size_t* hashval=0);
1565     //@}
1566
1567     //@{
1568     /*!
1569      return read-write reference to the specified sparse matrix element.
1570
1571      ref<_Tp>(i0,...[,hashval]) is equivalent to *(_Tp*)ptr(i0,...,true[,hashval]).
1572      The methods always return a valid reference.
1573      If the element did not exist, it is created and initialiazed with 0.
1574     */
1575     //! returns reference to the specified element (1D case)
1576     template<typename _Tp> _Tp& ref(int i0, size_t* hashval=0);
1577     //! returns reference to the specified element (2D case)
1578     template<typename _Tp> _Tp& ref(int i0, int i1, size_t* hashval=0);
1579     //! returns reference to the specified element (3D case)
1580     template<typename _Tp> _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
1581     //! returns reference to the specified element (nD case)
1582     template<typename _Tp> _Tp& ref(const int* idx, size_t* hashval=0);
1583     //@}
1584
1585     //@{
1586     /*!
1587      return value of the specified sparse matrix element.
1588
1589      value<_Tp>(i0,...[,hashval]) is equivalent
1590
1591      \code
1592      { const _Tp* p = find<_Tp>(i0,...[,hashval]); return p ? *p : _Tp(); }
1593      \endcode
1594
1595      That is, if the element did not exist, the methods return 0.
1596      */
1597     //! returns value of the specified element (1D case)
1598     template<typename _Tp> _Tp value(int i0, size_t* hashval=0) const;
1599     //! returns value of the specified element (2D case)
1600     template<typename _Tp> _Tp value(int i0, int i1, size_t* hashval=0) const;
1601     //! returns value of the specified element (3D case)
1602     template<typename _Tp> _Tp value(int i0, int i1, int i2, size_t* hashval=0) const;
1603     //! returns value of the specified element (nD case)
1604     template<typename _Tp> _Tp value(const int* idx, size_t* hashval=0) const;
1605     //@}
1606
1607     //@{
1608     /*!
1609      Return pointer to the specified sparse matrix element if it exists
1610
1611      find<_Tp>(i0,...[,hashval]) is equivalent to (_const Tp*)ptr(i0,...false[,hashval]).
1612
1613      If the specified element does not exist, the methods return NULL.
1614     */
1615     //! returns pointer to the specified element (1D case)
1616     template<typename _Tp> const _Tp* find(int i0, size_t* hashval=0) const;
1617     //! returns pointer to the specified element (2D case)
1618     template<typename _Tp> const _Tp* find(int i0, int i1, size_t* hashval=0) const;
1619     //! returns pointer to the specified element (3D case)
1620     template<typename _Tp> const _Tp* find(int i0, int i1, int i2, size_t* hashval=0) const;
1621     //! returns pointer to the specified element (nD case)
1622     template<typename _Tp> const _Tp* find(const int* idx, size_t* hashval=0) const;
1623
1624     //! erases the specified element (2D case)
1625     void erase(int i0, int i1, size_t* hashval=0);
1626     //! erases the specified element (3D case)
1627     void erase(int i0, int i1, int i2, size_t* hashval=0);
1628     //! erases the specified element (nD case)
1629     void erase(const int* idx, size_t* hashval=0);
1630
1631     //@{
1632     /*!
1633        return the sparse matrix iterator pointing to the first sparse matrix element
1634     */
1635     //! returns the sparse matrix iterator at the matrix beginning
1636     SparseMatIterator begin();
1637     //! returns the sparse matrix iterator at the matrix beginning
1638     template<typename _Tp> SparseMatIterator_<_Tp> begin();
1639     //! returns the read-only sparse matrix iterator at the matrix beginning
1640     SparseMatConstIterator begin() const;
1641     //! returns the read-only sparse matrix iterator at the matrix beginning
1642     template<typename _Tp> SparseMatConstIterator_<_Tp> begin() const;
1643     //@}
1644     /*!
1645        return the sparse matrix iterator pointing to the element following the last sparse matrix element
1646     */
1647     //! returns the sparse matrix iterator at the matrix end
1648     SparseMatIterator end();
1649     //! returns the read-only sparse matrix iterator at the matrix end
1650     SparseMatConstIterator end() const;
1651     //! returns the typed sparse matrix iterator at the matrix end
1652     template<typename _Tp> SparseMatIterator_<_Tp> end();
1653     //! returns the typed read-only sparse matrix iterator at the matrix end
1654     template<typename _Tp> SparseMatConstIterator_<_Tp> end() const;
1655
1656     //! returns the value stored in the sparse martix node
1657     template<typename _Tp> _Tp& value(Node* n);
1658     //! returns the value stored in the sparse martix node
1659     template<typename _Tp> const _Tp& value(const Node* n) const;
1660
1661     ////////////// some internal-use methods ///////////////
1662     Node* node(size_t nidx);
1663     const Node* node(size_t nidx) const;
1664
1665     uchar* newNode(const int* idx, size_t hashval);
1666     void removeNode(size_t hidx, size_t nidx, size_t previdx);
1667     void resizeHashTab(size_t newsize);
1668
1669     int flags;
1670     Hdr* hdr;
1671 };
1672
1673
1674
1675 ///////////////////////////////// SparseMat_<_Tp> ////////////////////////////////////
1676
1677 /*!
1678  The Template Sparse Matrix class derived from cv::SparseMat
1679
1680  The class provides slightly more convenient operations for accessing elements.
1681
1682  \code
1683  SparseMat m;
1684  ...
1685  SparseMat_<int> m_ = (SparseMat_<int>&)m;
1686  m_.ref(1)++; // equivalent to m.ref<int>(1)++;
1687  m_.ref(2) += m_(3); // equivalent to m.ref<int>(2) += m.value<int>(3);
1688  \endcode
1689 */
1690 template<typename _Tp> class SparseMat_ : public SparseMat
1691 {
1692 public:
1693     typedef SparseMatIterator_<_Tp> iterator;
1694     typedef SparseMatConstIterator_<_Tp> const_iterator;
1695
1696     //! the default constructor
1697     SparseMat_();
1698     //! the full constructor equivelent to SparseMat(dims, _sizes, DataType<_Tp>::type)
1699     SparseMat_(int dims, const int* _sizes);
1700     //! the copy constructor. If DataType<_Tp>.type != m.type(), the m elements are converted
1701     SparseMat_(const SparseMat& m);
1702     //! the copy constructor. This is O(1) operation - no data is copied
1703     SparseMat_(const SparseMat_& m);
1704     //! converts dense matrix to the sparse form
1705     SparseMat_(const Mat& m);
1706     //! converts the old-style sparse matrix to the C++ class. All the elements are copied
1707     //SparseMat_(const CvSparseMat* m);
1708     //! the assignment operator. If DataType<_Tp>.type != m.type(), the m elements are converted
1709     SparseMat_& operator = (const SparseMat& m);
1710     //! the assignment operator. This is O(1) operation - no data is copied
1711     SparseMat_& operator = (const SparseMat_& m);
1712     //! converts dense matrix to the sparse form
1713     SparseMat_& operator = (const Mat& m);
1714
1715     //! makes full copy of the matrix. All the elements are duplicated
1716     SparseMat_ clone() const;
1717     //! equivalent to cv::SparseMat::create(dims, _sizes, DataType<_Tp>::type)
1718     void create(int dims, const int* _sizes);
1719     //! converts sparse matrix to the old-style CvSparseMat. All the elements are copied
1720     //operator CvSparseMat*() const;
1721
1722     //! returns type of the matrix elements
1723     int type() const;
1724     //! returns depth of the matrix elements
1725     int depth() const;
1726     //! returns the number of channels in each matrix element
1727     int channels() const;
1728
1729     //! equivalent to SparseMat::ref<_Tp>(i0, hashval)
1730     _Tp& ref(int i0, size_t* hashval=0);
1731     //! equivalent to SparseMat::ref<_Tp>(i0, i1, hashval)
1732     _Tp& ref(int i0, int i1, size_t* hashval=0);
1733     //! equivalent to SparseMat::ref<_Tp>(i0, i1, i2, hashval)
1734     _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
1735     //! equivalent to SparseMat::ref<_Tp>(idx, hashval)
1736     _Tp& ref(const int* idx, size_t* hashval=0);
1737
1738     //! equivalent to SparseMat::value<_Tp>(i0, hashval)
1739     _Tp operator()(int i0, size_t* hashval=0) const;
1740     //! equivalent to SparseMat::value<_Tp>(i0, i1, hashval)
1741     _Tp operator()(int i0, int i1, size_t* hashval=0) const;
1742     //! equivalent to SparseMat::value<_Tp>(i0, i1, i2, hashval)
1743     _Tp operator()(int i0, int i1, int i2, size_t* hashval=0) const;
1744     //! equivalent to SparseMat::value<_Tp>(idx, hashval)
1745     _Tp operator()(const int* idx, size_t* hashval=0) const;
1746
1747     //! returns sparse matrix iterator pointing to the first sparse matrix element
1748     SparseMatIterator_<_Tp> begin();
1749     //! returns read-only sparse matrix iterator pointing to the first sparse matrix element
1750     SparseMatConstIterator_<_Tp> begin() const;
1751     //! returns sparse matrix iterator pointing to the element following the last sparse matrix element
1752     SparseMatIterator_<_Tp> end();
1753     //! returns read-only sparse matrix iterator pointing to the element following the last sparse matrix element
1754     SparseMatConstIterator_<_Tp> end() const;
1755 };
1756
1757
1758
1759 ////////////////////////////////// MatConstIterator //////////////////////////////////
1760
1761 class CV_EXPORTS MatConstIterator
1762 {
1763 public:
1764     typedef uchar* value_type;
1765     typedef ptrdiff_t difference_type;
1766     typedef const uchar** pointer;
1767     typedef uchar* reference;
1768
1769 #ifndef OPENCV_NOSTL
1770     typedef std::random_access_iterator_tag iterator_category;
1771 #endif
1772
1773     //! default constructor
1774     MatConstIterator();
1775     //! constructor that sets the iterator to the beginning of the matrix
1776     MatConstIterator(const Mat* _m);
1777     //! constructor that sets the iterator to the specified element of the matrix
1778     MatConstIterator(const Mat* _m, int _row, int _col=0);
1779     //! constructor that sets the iterator to the specified element of the matrix
1780     MatConstIterator(const Mat* _m, Point _pt);
1781     //! constructor that sets the iterator to the specified element of the matrix
1782     MatConstIterator(const Mat* _m, const int* _idx);
1783     //! copy constructor
1784     MatConstIterator(const MatConstIterator& it);
1785
1786     //! copy operator
1787     MatConstIterator& operator = (const MatConstIterator& it);
1788     //! returns the current matrix element
1789     uchar* operator *() const;
1790     //! returns the i-th matrix element, relative to the current
1791     uchar* operator [](ptrdiff_t i) const;
1792
1793     //! shifts the iterator forward by the specified number of elements
1794     MatConstIterator& operator += (ptrdiff_t ofs);
1795     //! shifts the iterator backward by the specified number of elements
1796     MatConstIterator& operator -= (ptrdiff_t ofs);
1797     //! decrements the iterator
1798     MatConstIterator& operator --();
1799     //! decrements the iterator
1800     MatConstIterator operator --(int);
1801     //! increments the iterator
1802     MatConstIterator& operator ++();
1803     //! increments the iterator
1804     MatConstIterator operator ++(int);
1805     //! returns the current iterator position
1806     Point pos() const;
1807     //! returns the current iterator position
1808     void pos(int* _idx) const;
1809
1810     ptrdiff_t lpos() const;
1811     void seek(ptrdiff_t ofs, bool relative = false);
1812     void seek(const int* _idx, bool relative = false);
1813
1814     const Mat* m;
1815     size_t elemSize;
1816     uchar* ptr;
1817     uchar* sliceStart;
1818     uchar* sliceEnd;
1819 };
1820
1821
1822
1823 ////////////////////////////////// MatConstIterator_ /////////////////////////////////
1824
1825 /*!
1826  Matrix read-only iterator
1827  */
1828 template<typename _Tp>
1829 class MatConstIterator_ : public MatConstIterator
1830 {
1831 public:
1832     typedef _Tp value_type;
1833     typedef ptrdiff_t difference_type;
1834     typedef const _Tp* pointer;
1835     typedef const _Tp& reference;
1836
1837 #ifndef OPENCV_NOSTL
1838     typedef std::random_access_iterator_tag iterator_category;
1839 #endif
1840
1841     //! default constructor
1842     MatConstIterator_();
1843     //! constructor that sets the iterator to the beginning of the matrix
1844     MatConstIterator_(const Mat_<_Tp>* _m);
1845     //! constructor that sets the iterator to the specified element of the matrix
1846     MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col=0);
1847     //! constructor that sets the iterator to the specified element of the matrix
1848     MatConstIterator_(const Mat_<_Tp>* _m, Point _pt);
1849     //! constructor that sets the iterator to the specified element of the matrix
1850     MatConstIterator_(const Mat_<_Tp>* _m, const int* _idx);
1851     //! copy constructor
1852     MatConstIterator_(const MatConstIterator_& it);
1853
1854     //! copy operator
1855     MatConstIterator_& operator = (const MatConstIterator_& it);
1856     //! returns the current matrix element
1857     _Tp operator *() const;
1858     //! returns the i-th matrix element, relative to the current
1859     _Tp operator [](ptrdiff_t i) const;
1860
1861     //! shifts the iterator forward by the specified number of elements
1862     MatConstIterator_& operator += (ptrdiff_t ofs);
1863     //! shifts the iterator backward by the specified number of elements
1864     MatConstIterator_& operator -= (ptrdiff_t ofs);
1865     //! decrements the iterator
1866     MatConstIterator_& operator --();
1867     //! decrements the iterator
1868     MatConstIterator_ operator --(int);
1869     //! increments the iterator
1870     MatConstIterator_& operator ++();
1871     //! increments the iterator
1872     MatConstIterator_ operator ++(int);
1873     //! returns the current iterator position
1874     Point pos() const;
1875 };
1876
1877
1878
1879 //////////////////////////////////// MatIterator_ ////////////////////////////////////
1880
1881 /*!
1882  Matrix read-write iterator
1883 */
1884 template<typename _Tp>
1885 class MatIterator_ : public MatConstIterator_<_Tp>
1886 {
1887 public:
1888     typedef _Tp* pointer;
1889     typedef _Tp& reference;
1890
1891 #ifndef OPENCV_NOSTL
1892     typedef std::random_access_iterator_tag iterator_category;
1893 #endif
1894
1895     //! the default constructor
1896     MatIterator_();
1897     //! constructor that sets the iterator to the beginning of the matrix
1898     MatIterator_(Mat_<_Tp>* _m);
1899     //! constructor that sets the iterator to the specified element of the matrix
1900     MatIterator_(Mat_<_Tp>* _m, int _row, int _col=0);
1901     //! constructor that sets the iterator to the specified element of the matrix
1902     MatIterator_(const Mat_<_Tp>* _m, Point _pt);
1903     //! constructor that sets the iterator to the specified element of the matrix
1904     MatIterator_(const Mat_<_Tp>* _m, const int* _idx);
1905     //! copy constructor
1906     MatIterator_(const MatIterator_& it);
1907     //! copy operator
1908     MatIterator_& operator = (const MatIterator_<_Tp>& it );
1909
1910     //! returns the current matrix element
1911     _Tp& operator *() const;
1912     //! returns the i-th matrix element, relative to the current
1913     _Tp& operator [](ptrdiff_t i) const;
1914
1915     //! shifts the iterator forward by the specified number of elements
1916     MatIterator_& operator += (ptrdiff_t ofs);
1917     //! shifts the iterator backward by the specified number of elements
1918     MatIterator_& operator -= (ptrdiff_t ofs);
1919     //! decrements the iterator
1920     MatIterator_& operator --();
1921     //! decrements the iterator
1922     MatIterator_ operator --(int);
1923     //! increments the iterator
1924     MatIterator_& operator ++();
1925     //! increments the iterator
1926     MatIterator_ operator ++(int);
1927 };
1928
1929
1930
1931 /////////////////////////////// SparseMatConstIterator ///////////////////////////////
1932
1933 /*!
1934  Read-Only Sparse Matrix Iterator.
1935  Here is how to use the iterator to compute the sum of floating-point sparse matrix elements:
1936
1937  \code
1938  SparseMatConstIterator it = m.begin(), it_end = m.end();
1939  double s = 0;
1940  CV_Assert( m.type() == CV_32F );
1941  for( ; it != it_end; ++it )
1942     s += it.value<float>();
1943  \endcode
1944 */
1945 class CV_EXPORTS SparseMatConstIterator
1946 {
1947 public:
1948     //! the default constructor
1949     SparseMatConstIterator();
1950     //! the full constructor setting the iterator to the first sparse matrix element
1951     SparseMatConstIterator(const SparseMat* _m);
1952     //! the copy constructor
1953     SparseMatConstIterator(const SparseMatConstIterator& it);
1954
1955     //! the assignment operator
1956     SparseMatConstIterator& operator = (const SparseMatConstIterator& it);
1957
1958     //! template method returning the current matrix element
1959     template<typename _Tp> const _Tp& value() const;
1960     //! returns the current node of the sparse matrix. it.node->idx is the current element index
1961     const SparseMat::Node* node() const;
1962
1963     //! moves iterator to the previous element
1964     SparseMatConstIterator& operator --();
1965     //! moves iterator to the previous element
1966     SparseMatConstIterator operator --(int);
1967     //! moves iterator to the next element
1968     SparseMatConstIterator& operator ++();
1969     //! moves iterator to the next element
1970     SparseMatConstIterator operator ++(int);
1971
1972     //! moves iterator to the element after the last element
1973     void seekEnd();
1974
1975     const SparseMat* m;
1976     size_t hashidx;
1977     uchar* ptr;
1978 };
1979
1980
1981
1982 ////////////////////////////////// SparseMatIterator /////////////////////////////////
1983
1984 /*!
1985  Read-write Sparse Matrix Iterator
1986
1987  The class is similar to cv::SparseMatConstIterator,
1988  but can be used for in-place modification of the matrix elements.
1989 */
1990 class CV_EXPORTS SparseMatIterator : public SparseMatConstIterator
1991 {
1992 public:
1993     //! the default constructor
1994     SparseMatIterator();
1995     //! the full constructor setting the iterator to the first sparse matrix element
1996     SparseMatIterator(SparseMat* _m);
1997     //! the full constructor setting the iterator to the specified sparse matrix element
1998     SparseMatIterator(SparseMat* _m, const int* idx);
1999     //! the copy constructor
2000     SparseMatIterator(const SparseMatIterator& it);
2001
2002     //! the assignment operator
2003     SparseMatIterator& operator = (const SparseMatIterator& it);
2004     //! returns read-write reference to the current sparse matrix element
2005     template<typename _Tp> _Tp& value() const;
2006     //! returns pointer to the current sparse matrix node. it.node->idx is the index of the current element (do not modify it!)
2007     SparseMat::Node* node() const;
2008
2009     //! moves iterator to the next element
2010     SparseMatIterator& operator ++();
2011     //! moves iterator to the next element
2012     SparseMatIterator operator ++(int);
2013 };
2014
2015
2016
2017 /////////////////////////////// SparseMatConstIterator_ //////////////////////////////
2018
2019 /*!
2020  Template Read-Only Sparse Matrix Iterator Class.
2021
2022  This is the derived from SparseMatConstIterator class that
2023  introduces more convenient operator *() for accessing the current element.
2024 */
2025 template<typename _Tp> class SparseMatConstIterator_ : public SparseMatConstIterator
2026 {
2027 public:
2028
2029 #ifndef OPENCV_NOSTL
2030     typedef std::forward_iterator_tag iterator_category;
2031 #endif
2032
2033     //! the default constructor
2034     SparseMatConstIterator_();
2035     //! the full constructor setting the iterator to the first sparse matrix element
2036     SparseMatConstIterator_(const SparseMat_<_Tp>* _m);
2037     SparseMatConstIterator_(const SparseMat* _m);
2038     //! the copy constructor
2039     SparseMatConstIterator_(const SparseMatConstIterator_& it);
2040
2041     //! the assignment operator
2042     SparseMatConstIterator_& operator = (const SparseMatConstIterator_& it);
2043     //! the element access operator
2044     const _Tp& operator *() const;
2045
2046     //! moves iterator to the next element
2047     SparseMatConstIterator_& operator ++();
2048     //! moves iterator to the next element
2049     SparseMatConstIterator_ operator ++(int);
2050 };
2051
2052
2053
2054 ///////////////////////////////// SparseMatIterator_ /////////////////////////////////
2055
2056 /*!
2057  Template Read-Write Sparse Matrix Iterator Class.
2058
2059  This is the derived from cv::SparseMatConstIterator_ class that
2060  introduces more convenient operator *() for accessing the current element.
2061 */
2062 template<typename _Tp> class SparseMatIterator_ : public SparseMatConstIterator_<_Tp>
2063 {
2064 public:
2065
2066 #ifndef OPENCV_NOSTL
2067     typedef std::forward_iterator_tag iterator_category;
2068 #endif
2069
2070     //! the default constructor
2071     SparseMatIterator_();
2072     //! the full constructor setting the iterator to the first sparse matrix element
2073     SparseMatIterator_(SparseMat_<_Tp>* _m);
2074     SparseMatIterator_(SparseMat* _m);
2075     //! the copy constructor
2076     SparseMatIterator_(const SparseMatIterator_& it);
2077
2078     //! the assignment operator
2079     SparseMatIterator_& operator = (const SparseMatIterator_& it);
2080     //! returns the reference to the current element
2081     _Tp& operator *() const;
2082
2083     //! moves the iterator to the next element
2084     SparseMatIterator_& operator ++();
2085     //! moves the iterator to the next element
2086     SparseMatIterator_ operator ++(int);
2087 };
2088
2089
2090
2091 /////////////////////////////////// NAryMatIterator //////////////////////////////////
2092
2093 /*!
2094  n-Dimensional Dense Matrix Iterator Class.
2095
2096  The class cv::NAryMatIterator is used for iterating over one or more n-dimensional dense arrays (cv::Mat's).
2097
2098  The iterator is completely different from cv::Mat_ and cv::SparseMat_ iterators.
2099  It iterates through the slices (or planes), not the elements, where "slice" is a continuous part of the arrays.
2100
2101  Here is the example on how the iterator can be used to normalize 3D histogram:
2102
2103  \code
2104  void normalizeColorHist(Mat& hist)
2105  {
2106  #if 1
2107      // intialize iterator (the style is different from STL).
2108      // after initialization the iterator will contain
2109      // the number of slices or planes
2110      // the iterator will go through
2111      Mat* arrays[] = { &hist, 0 };
2112      Mat planes[1];
2113      NAryMatIterator it(arrays, planes);
2114      double s = 0;
2115      // iterate through the matrix. on each iteration
2116      // it.planes[i] (of type Mat) will be set to the current plane of
2117      // i-th n-dim matrix passed to the iterator constructor.
2118      for(int p = 0; p < it.nplanes; p++, ++it)
2119         s += sum(it.planes[0])[0];
2120      it = NAryMatIterator(hist);
2121      s = 1./s;
2122      for(int p = 0; p < it.nplanes; p++, ++it)
2123         it.planes[0] *= s;
2124  #elif 1
2125      // this is a shorter implementation of the above
2126      // using built-in operations on Mat
2127      double s = sum(hist)[0];
2128      hist.convertTo(hist, hist.type(), 1./s, 0);
2129  #else
2130      // and this is even shorter one
2131      // (assuming that the histogram elements are non-negative)
2132      normalize(hist, hist, 1, 0, NORM_L1);
2133  #endif
2134  }
2135  \endcode
2136
2137  You can iterate through several matrices simultaneously as long as they have the same geometry
2138  (dimensionality and all the dimension sizes are the same), which is useful for binary
2139  and n-ary operations on such matrices. Just pass those matrices to cv::MatNDIterator.
2140  Then, during the iteration it.planes[0], it.planes[1], ... will
2141  be the slices of the corresponding matrices
2142 */
2143 class CV_EXPORTS NAryMatIterator
2144 {
2145 public:
2146     //! the default constructor
2147     NAryMatIterator();
2148     //! the full constructor taking arbitrary number of n-dim matrices
2149     NAryMatIterator(const Mat** arrays, uchar** ptrs, int narrays=-1);
2150     //! the full constructor taking arbitrary number of n-dim matrices
2151     NAryMatIterator(const Mat** arrays, Mat* planes, int narrays=-1);
2152     //! the separate iterator initialization method
2153     void init(const Mat** arrays, Mat* planes, uchar** ptrs, int narrays=-1);
2154
2155     //! proceeds to the next plane of every iterated matrix
2156     NAryMatIterator& operator ++();
2157     //! proceeds to the next plane of every iterated matrix (postfix increment operator)
2158     NAryMatIterator operator ++(int);
2159
2160     //! the iterated arrays
2161     const Mat** arrays;
2162     //! the current planes
2163     Mat* planes;
2164     //! data pointers
2165     uchar** ptrs;
2166     //! the number of arrays
2167     int narrays;
2168     //! the number of hyper-planes that the iterator steps through
2169     size_t nplanes;
2170     //! the size of each segment (in elements)
2171     size_t size;
2172 protected:
2173     int iterdepth;
2174     size_t idx;
2175 };
2176
2177
2178
2179 ///////////////////////////////// Matrix Expressions /////////////////////////////////
2180
2181 class CV_EXPORTS MatOp
2182 {
2183 public:
2184     MatOp();
2185     virtual ~MatOp();
2186
2187     virtual bool elementWise(const MatExpr& expr) const;
2188     virtual void assign(const MatExpr& expr, Mat& m, int type=-1) const = 0;
2189     virtual void roi(const MatExpr& expr, const Range& rowRange,
2190                      const Range& colRange, MatExpr& res) const;
2191     virtual void diag(const MatExpr& expr, int d, MatExpr& res) const;
2192     virtual void augAssignAdd(const MatExpr& expr, Mat& m) const;
2193     virtual void augAssignSubtract(const MatExpr& expr, Mat& m) const;
2194     virtual void augAssignMultiply(const MatExpr& expr, Mat& m) const;
2195     virtual void augAssignDivide(const MatExpr& expr, Mat& m) const;
2196     virtual void augAssignAnd(const MatExpr& expr, Mat& m) const;
2197     virtual void augAssignOr(const MatExpr& expr, Mat& m) const;
2198     virtual void augAssignXor(const MatExpr& expr, Mat& m) const;
2199
2200     virtual void add(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
2201     virtual void add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const;
2202
2203     virtual void subtract(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
2204     virtual void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const;
2205
2206     virtual void multiply(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const;
2207     virtual void multiply(const MatExpr& expr1, double s, MatExpr& res) const;
2208
2209     virtual void divide(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res, double scale=1) const;
2210     virtual void divide(double s, const MatExpr& expr, MatExpr& res) const;
2211
2212     virtual void abs(const MatExpr& expr, MatExpr& res) const;
2213
2214     virtual void transpose(const MatExpr& expr, MatExpr& res) const;
2215     virtual void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
2216     virtual void invert(const MatExpr& expr, int method, MatExpr& res) const;
2217
2218     virtual Size size(const MatExpr& expr) const;
2219     virtual int type(const MatExpr& expr) const;
2220 };
2221
2222
2223 class CV_EXPORTS MatExpr
2224 {
2225 public:
2226     MatExpr();
2227     explicit MatExpr(const Mat& m);
2228
2229     MatExpr(const MatOp* _op, int _flags, const Mat& _a = Mat(), const Mat& _b = Mat(),
2230             const Mat& _c = Mat(), double _alpha = 1, double _beta = 1, const Scalar& _s = Scalar());
2231
2232     operator Mat() const;
2233     template<typename _Tp> operator Mat_<_Tp>() const;
2234
2235     Size size() const;
2236     int type() const;
2237
2238     MatExpr row(int y) const;
2239     MatExpr col(int x) const;
2240     MatExpr diag(int d = 0) const;
2241     MatExpr operator()( const Range& rowRange, const Range& colRange ) const;
2242     MatExpr operator()( const Rect& roi ) const;
2243
2244     MatExpr t() const;
2245     MatExpr inv(int method = DECOMP_LU) const;
2246     MatExpr mul(const MatExpr& e, double scale=1) const;
2247     MatExpr mul(const Mat& m, double scale=1) const;
2248
2249     Mat cross(const Mat& m) const;
2250     double dot(const Mat& m) const;
2251
2252     const MatOp* op;
2253     int flags;
2254
2255     Mat a, b, c;
2256     double alpha, beta;
2257     Scalar s;
2258 };
2259
2260
2261 CV_EXPORTS MatExpr operator + (const Mat& a, const Mat& b);
2262 CV_EXPORTS MatExpr operator + (const Mat& a, const Scalar& s);
2263 CV_EXPORTS MatExpr operator + (const Scalar& s, const Mat& a);
2264 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Mat& m);
2265 CV_EXPORTS MatExpr operator + (const Mat& m, const MatExpr& e);
2266 CV_EXPORTS MatExpr operator + (const MatExpr& e, const Scalar& s);
2267 CV_EXPORTS MatExpr operator + (const Scalar& s, const MatExpr& e);
2268 CV_EXPORTS MatExpr operator + (const MatExpr& e1, const MatExpr& e2);
2269
2270 CV_EXPORTS MatExpr operator - (const Mat& a, const Mat& b);
2271 CV_EXPORTS MatExpr operator - (const Mat& a, const Scalar& s);
2272 CV_EXPORTS MatExpr operator - (const Scalar& s, const Mat& a);
2273 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Mat& m);
2274 CV_EXPORTS MatExpr operator - (const Mat& m, const MatExpr& e);
2275 CV_EXPORTS MatExpr operator - (const MatExpr& e, const Scalar& s);
2276 CV_EXPORTS MatExpr operator - (const Scalar& s, const MatExpr& e);
2277 CV_EXPORTS MatExpr operator - (const MatExpr& e1, const MatExpr& e2);
2278
2279 CV_EXPORTS MatExpr operator - (const Mat& m);
2280 CV_EXPORTS MatExpr operator - (const MatExpr& e);
2281
2282 CV_EXPORTS MatExpr operator * (const Mat& a, const Mat& b);
2283 CV_EXPORTS MatExpr operator * (const Mat& a, double s);
2284 CV_EXPORTS MatExpr operator * (double s, const Mat& a);
2285 CV_EXPORTS MatExpr operator * (const MatExpr& e, const Mat& m);
2286 CV_EXPORTS MatExpr operator * (const Mat& m, const MatExpr& e);
2287 CV_EXPORTS MatExpr operator * (const MatExpr& e, double s);
2288 CV_EXPORTS MatExpr operator * (double s, const MatExpr& e);
2289 CV_EXPORTS MatExpr operator * (const MatExpr& e1, const MatExpr& e2);
2290
2291 CV_EXPORTS MatExpr operator / (const Mat& a, const Mat& b);
2292 CV_EXPORTS MatExpr operator / (const Mat& a, double s);
2293 CV_EXPORTS MatExpr operator / (double s, const Mat& a);
2294 CV_EXPORTS MatExpr operator / (const MatExpr& e, const Mat& m);
2295 CV_EXPORTS MatExpr operator / (const Mat& m, const MatExpr& e);
2296 CV_EXPORTS MatExpr operator / (const MatExpr& e, double s);
2297 CV_EXPORTS MatExpr operator / (double s, const MatExpr& e);
2298 CV_EXPORTS MatExpr operator / (const MatExpr& e1, const MatExpr& e2);
2299
2300 CV_EXPORTS MatExpr operator < (const Mat& a, const Mat& b);
2301 CV_EXPORTS MatExpr operator < (const Mat& a, double s);
2302 CV_EXPORTS MatExpr operator < (double s, const Mat& a);
2303
2304 CV_EXPORTS MatExpr operator <= (const Mat& a, const Mat& b);
2305 CV_EXPORTS MatExpr operator <= (const Mat& a, double s);
2306 CV_EXPORTS MatExpr operator <= (double s, const Mat& a);
2307
2308 CV_EXPORTS MatExpr operator == (const Mat& a, const Mat& b);
2309 CV_EXPORTS MatExpr operator == (const Mat& a, double s);
2310 CV_EXPORTS MatExpr operator == (double s, const Mat& a);
2311
2312 CV_EXPORTS MatExpr operator != (const Mat& a, const Mat& b);
2313 CV_EXPORTS MatExpr operator != (const Mat& a, double s);
2314 CV_EXPORTS MatExpr operator != (double s, const Mat& a);
2315
2316 CV_EXPORTS MatExpr operator >= (const Mat& a, const Mat& b);
2317 CV_EXPORTS MatExpr operator >= (const Mat& a, double s);
2318 CV_EXPORTS MatExpr operator >= (double s, const Mat& a);
2319
2320 CV_EXPORTS MatExpr operator > (const Mat& a, const Mat& b);
2321 CV_EXPORTS MatExpr operator > (const Mat& a, double s);
2322 CV_EXPORTS MatExpr operator > (double s, const Mat& a);
2323
2324 CV_EXPORTS MatExpr operator & (const Mat& a, const Mat& b);
2325 CV_EXPORTS MatExpr operator & (const Mat& a, const Scalar& s);
2326 CV_EXPORTS MatExpr operator & (const Scalar& s, const Mat& a);
2327
2328 CV_EXPORTS MatExpr operator | (const Mat& a, const Mat& b);
2329 CV_EXPORTS MatExpr operator | (const Mat& a, const Scalar& s);
2330 CV_EXPORTS MatExpr operator | (const Scalar& s, const Mat& a);
2331
2332 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Mat& b);
2333 CV_EXPORTS MatExpr operator ^ (const Mat& a, const Scalar& s);
2334 CV_EXPORTS MatExpr operator ^ (const Scalar& s, const Mat& a);
2335
2336 CV_EXPORTS MatExpr operator ~(const Mat& m);
2337
2338 CV_EXPORTS MatExpr min(const Mat& a, const Mat& b);
2339 CV_EXPORTS MatExpr min(const Mat& a, double s);
2340 CV_EXPORTS MatExpr min(double s, const Mat& a);
2341
2342 CV_EXPORTS MatExpr max(const Mat& a, const Mat& b);
2343 CV_EXPORTS MatExpr max(const Mat& a, double s);
2344 CV_EXPORTS MatExpr max(double s, const Mat& a);
2345
2346 CV_EXPORTS MatExpr abs(const Mat& m);
2347 CV_EXPORTS MatExpr abs(const MatExpr& e);
2348
2349 } // cv
2350
2351 #include "opencv2/core/mat.inl.hpp"
2352
2353 #endif // __OPENCV_CORE_MAT_HPP__