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