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