]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cxcore_basic_structures.tex
1ebd07aa4e9dbb43684465592e15061c8724a47e
[opencv.git] / opencv / doc / cxcore_basic_structures.tex
1 \section{Basic Structures}
2
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 %                                                                                    %
5 %                                         C                                          %
6 %                                                                                    %
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8
9 \ifCPy
10 \label{CvPoint}\cvclass{CvPoint}
11 2D point with integer coordinates (usually zero-based).
12
13 \ifC % {
14 \begin{lstlisting}
15 typedef struct CvPoint
16 {
17     int x; 
18     int y; 
19 }
20 CvPoint;
21 \end{lstlisting}
22
23 \begin{description}
24 \cvarg{x}{x-coordinate}
25 \cvarg{y}{y-coordinate} 
26 \end{description}
27
28 \begin{lstlisting}
29 /* Constructor */
30 inline CvPoint cvPoint( int x, int y );
31
32 /* Conversion from CvPoint2D32f */
33 inline CvPoint cvPointFrom32f( CvPoint2D32f point );
34 \end{lstlisting}
35 \else % }{
36 2D point, represented as a tuple \texttt{(x, y)}, where x and y are integers.
37 \fi % }
38
39 \label{CvPoint2D32f}\cvclass{CvPoint2D32f}
40 2D point with floating-point coordinates
41
42 \ifC % {
43 \begin{lstlisting}
44 typedef struct CvPoint2D32f
45 {
46     float x;
47     float y; 
48 }
49 CvPoint2D32f;
50 \end{lstlisting}
51
52 \begin{description}
53 \cvarg{x}{x-coordinate}
54 \cvarg{y}{y-coordinate}
55 \end{description}
56
57 \begin{lstlisting}
58 /* Constructor */
59 inline CvPoint2D32f cvPoint2D32f( double x, double y );
60
61 /* Conversion from CvPoint */
62 inline CvPoint2D32f cvPointTo32f( CvPoint point );
63 \end{lstlisting}
64 \else % }{
65 2D point, represented as a tuple \texttt{(x, y)}, where x and y are floats.
66 \fi % }
67
68
69 \label{CvPoint3D32f}\cvclass{CvPoint3D32f}
70 3D point with floating-point coordinates
71
72 \ifC % {
73 \begin{lstlisting}
74 typedef struct CvPoint3D32f
75 {
76     float x; 
77     float y; 
78     float z; 
79 }
80 CvPoint3D32f;
81 \end{lstlisting}
82
83 \begin{description}
84 \cvarg{x}{x-coordinate}
85 \cvarg{y}{y-coordinate}
86 \cvarg{z}{z-coordinate}
87 \end{description}
88
89 \begin{lstlisting}
90 /* Constructor */
91 inline CvPoint3D32f cvPoint3D32f( double x, double y, double z );
92 \end{lstlisting}
93 \else % }{
94 3D point, represented as a tuple \texttt{(x, y, z)}, where x, y and z are floats.
95 \fi % }
96
97 \label{CvPoint2D64f}\cvclass{CvPoint2D64f}
98 2D point with double precision floating-point coordinates
99
100 \ifC % {
101 \begin{lstlisting}
102 typedef struct CvPoint2D64f
103 {
104     double x; 
105     double y; 
106 }
107 CvPoint2D64f;
108 \end{lstlisting}
109
110 \begin{description}
111 \cvarg{x}{x-coordinate}
112 \cvarg{y}{y-coordinate}
113 \end{description}
114
115 \begin{lstlisting}
116 /* Constructor */
117 inline CvPoint2D64f cvPoint2D64f( double x, double y );
118
119 /* Conversion from CvPoint */
120 inline CvPoint2D64f cvPointTo64f( CvPoint point );
121 \end{lstlisting}
122 \else % }{
123 2D point, represented as a tuple \texttt{(x, y)}, where x and y are floats.
124 \fi % }
125
126 \label{CvPoint3D64f}\cvclass{CvPoint3D64f}
127 3D point with double precision floating-point coordinates
128
129 \ifC % {
130 \begin{lstlisting}
131 typedef struct CvPoint3D64f
132 {
133     double x; 
134     double y; 
135     double z; 
136 }
137 CvPoint3D64f;
138 \end{lstlisting}
139
140 \begin{description}
141 \cvarg{x}{x-coordinate}
142 \cvarg{y}{y-coordinate}
143 \cvarg{z}{z-coordinate}
144 \end{description}
145
146 \begin{lstlisting}
147 /* Constructor */
148 inline CvPoint3D64f cvPoint3D64f( double x, double y, double z );
149 \end{lstlisting}
150 \else % }{
151 3D point, represented as a tuple \texttt{(x, y, z)}, where x, y and z are floats.
152 \fi % }
153
154 \label{CvSize}\cvclass{CvSize}
155 Pixel-accurate size of a rectangle.
156
157 \ifC % {
158 \begin{lstlisting}
159 typedef struct CvSize
160 {
161     int width; 
162     int height; 
163 }
164 CvSize;
165 \end{lstlisting}
166
167 \begin{description}
168 \cvarg{width}{Width of the rectangle}
169 \cvarg{height}{Height of the rectangle}
170 \end{description}
171
172 \begin{lstlisting}
173 /* Constructor */
174 inline CvSize cvSize( int width, int height );
175 \end{lstlisting}
176 \else % }{
177 Size of a rectangle, represented as a tuple \texttt{(width, height)}, where width and height are integers.
178 \fi % }
179
180 \label{CvSize2D32f}\cvclass{CvSize2D32f}
181 Sub-pixel accurate size of a rectangle.
182
183 \ifC % {
184 \begin{lstlisting}
185 typedef struct CvSize2D32f
186 {
187     float width; 
188     float height; 
189 }
190 CvSize2D32f;
191 \end{lstlisting}
192
193 \begin{description}
194 \cvarg{width}{Width of the rectangle}
195 \cvarg{height}{Height of the rectangle}
196 \end{description}
197
198 \begin{lstlisting}
199 /* Constructor */
200 inline CvSize2D32f cvSize2D32f( double width, double height );
201 \end{lstlisting}
202 \else % }{
203 Size of a rectangle, represented as a tuple \texttt{(width, height)}, where width and height are floats.
204 \fi % }
205
206 \label{CvRect}\cvclass{CvRect}
207 Offset (usually the top-left corner) and size of a rectangle.
208
209 \ifC % {
210 \begin{lstlisting}
211 typedef struct CvRect
212 {
213     int x; 
214     int y; 
215     int width; 
216     int height; 
217 }
218 CvRect;
219 \end{lstlisting}
220
221 \begin{description}
222 \cvarg{x}{x-coordinate of the top-left corner}
223 \cvarg{y}{y-coordinate of the top-left corner (bottom-left for Windows bitmaps)}
224 \cvarg{width}{Width of the rectangle}
225 \cvarg{height}{Height of the rectangle}
226 \end{description}
227
228 \begin{lstlisting}
229 /* Constructor */
230 inline CvRect cvRect( int x, int y, int width, int height );
231 \end{lstlisting}
232 \else % }{
233 Rectangle, represented as a tuple \texttt{(x, y, width, height)}, where all are integers.
234 \fi % }
235
236 \label{CvScalar}\cvclass{CvScalar}
237 A container for 1-,2-,3- or 4-tuples of doubles.
238
239 \ifC % {
240 \begin{lstlisting}
241 typedef struct CvScalar
242 {
243     double val[4];
244 }
245 CvScalar;
246 \end{lstlisting}
247
248 \begin{lstlisting}
249 /* Constructor: 
250 initializes val[0] with val0, val[1] with val1, etc. 
251 */
252 inline CvScalar cvScalar( double val0, double val1=0,
253                           double val2=0, double val3=0 );
254 /* Constructor: 
255 initializes all of val[0]...val[3] with val0123 
256 */
257 inline CvScalar cvScalarAll( double val0123 );
258
259 /* Constructor: 
260 initializes val[0] with val0, and all of val[1]...val[3] with zeros 
261 */
262 inline CvScalar cvRealScalar( double val0 );
263 \end{lstlisting}
264 \else % }{
265
266 CvScalar is always represented as a 4-tuple.
267
268 \begin{lstlisting}
269 >>> import cv
270 >>> cv.Scalar(1, 2, 3, 4)
271 (1.0, 2.0, 3.0, 4.0)
272 >>> cv.ScalarAll(7)
273 (7.0, 7.0, 7.0, 7.0)
274 >>> cv.RealScalar(7)
275 (7.0, 0.0, 0.0, 0.0)
276 >>> cv.RGB(17, 110, 255)
277 (255.0, 110.0, 17.0, 0.0)
278 \end{lstlisting}
279 \fi % }
280
281 \label{CvTermCriteria}\cvclass{CvTermCriteria}
282 Termination criteria for iterative algorithms.
283
284 \ifC % {
285 \begin{lstlisting}
286 #define CV_TERMCRIT_ITER    1
287 #define CV_TERMCRIT_NUMBER  CV_TERMCRIT_ITER
288 #define CV_TERMCRIT_EPS     2
289
290 typedef struct CvTermCriteria
291 {
292     int    type;
293     int    max_iter; 
294     double epsilon; 
295 }
296 CvTermCriteria;
297 \end{lstlisting}
298
299 \begin{description}
300 \cvarg{type}{A combination of CV\_TERMCRIT\_ITER and CV\_TERMCRIT\_EPS}
301 \cvarg{max\_iter}{Maximum number of iterations}
302 \cvarg{epsilon}{Required accuracy}
303 \end{description}
304
305 \begin{lstlisting}
306 /* Constructor */
307 inline CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon );
308
309 /* Check and transform a CvTermCriteria so that 
310    type=CV_TERMCRIT_ITER+CV_TERMCRIT_EPS
311    and both max_iter and epsilon are valid */
312 CvTermCriteria cvCheckTermCriteria( CvTermCriteria criteria,
313                                     double default_eps,
314                                     int default_max_iters );
315 \end{lstlisting}
316 \else % }{
317 Represented by a tuple \texttt{(type, max\_iter, epsilon)}.
318
319 \begin{description}
320 \cvarg{type}{\texttt{CV\_TERMCRIT\_ITER}, \texttt{CV\_TERMCRIT\_EPS} or \texttt{CV\_TERMCRIT\_ITER | CV\_TERMCRIT\_EPS}}
321 \cvarg{max\_iter}{Maximum number of iterations}
322 \cvarg{epsilon}{Required accuracy}
323 \end{description}
324
325 \begin{lstlisting}
326 (cv.CV_TERMCRIT_ITER, 10, 0)                         # terminate after 10 iterations
327 (cv.CV_TERMCRIT_EPS, 0, 0.01)                        # terminate when epsilon reaches 0.01
328 (cv.CV_TERMCRIT_ITER | cv.CV_TERMCRIT_EPS, 10, 0.01) # terminate as soon as either condition is met
329 \end{lstlisting}
330 \fi % }
331
332 \label{CvMat}\cvclass{CvMat}
333
334 \ifC % {
335 A multi-channel matrix.
336
337 \begin{lstlisting}
338 typedef struct CvMat
339 {
340     int type; 
341     int step; 
342
343     int* refcount; 
344
345     union
346     {
347         uchar* ptr;
348         short* s;
349         int* i;
350         float* fl;
351         double* db;
352     } data; 
353
354 #ifdef __cplusplus
355     union
356     {
357         int rows;
358         int height;
359     };
360
361     union
362     {
363         int cols;
364         int width;
365     };
366 #else
367     int rows; 
368     int cols; 
369 #endif
370
371 } CvMat;
372 \end{lstlisting}
373
374 \begin{description}
375 \cvarg{type}{A CvMat signature (CV\_MAT\_MAGIC\_VAL) containing the type of elements and flags}
376 \cvarg{step}{Full row length in bytes}
377 \cvarg{refcount}{Underlying data reference counter}
378 \cvarg{data}{Pointers to the actual matrix data}
379 \cvarg{rows}{Number of rows}
380 \cvarg{cols}{Number of columns}
381 \end{description}
382
383 Matrices are stored row by row. All of the rows are aligned by 4 bytes.
384 \else % }{
385 A multi-channel 2D matrix.  Created by
386 \cross{CreateMat},
387 \cross{LoadImageM},
388 \cross{CreateMatHeader},
389 \cross{fromarray}.
390
391 \begin{description}
392 \cvarg{type}{A CvMat signature containing the type of elements and flags, int}
393 \cvarg{step}{Full row length in bytes, int}
394 \cvarg{rows}{Number of rows, int}
395 \cvarg{cols}{Number of columns, int}
396 \cvarg{tostring() -> str}{Returns the contents of the CvMat as a single string.}
397 \end{description}
398
399
400 \fi % }
401
402
403 \label{CvMatND}\cvclass{CvMatND}
404 Multi-dimensional dense multi-channel array.
405
406 \ifC
407 \begin{lstlisting}
408 typedef struct CvMatND
409 {
410     int type; 
411     int dims;
412
413     int* refcount; 
414
415     union
416     {
417         uchar* ptr;
418         short* s;
419         int* i;
420         float* fl;
421         double* db;
422     } data; 
423
424     struct
425     {
426         int size;
427         int step;
428     }
429     dim[CV_MAX_DIM];
430
431 } CvMatND;
432 \end{lstlisting}
433
434 \begin{description}
435 \cvarg{type}{A CvMatND signature (CV\_MATND\_MAGIC\_VAL), combining the type of elements and flags}
436 \cvarg{dims}{The number of array dimensions}
437 \cvarg{refcount}{Underlying data reference counter}
438 \cvarg{data}{Pointers to the actual matrix data}
439 \cvarg{dim}{For each dimension, the pair (number of elements, distance between elements in bytes)}
440 \end{description}
441 \fi
442
443 \ifPy
444 \begin{description}
445 \cvarg{type}{A CvMatND signature combining the type of elements and flags, int}
446 \cvarg{tostring() -> str}{Returns the contents of the CvMatND as a single string.}
447 \end{description}
448 \fi
449
450 \ifC
451 \label{CvSparseMat}\cvclass{CvSparseMat}
452 Multi-dimensional sparse multi-channel array.
453
454 \begin{lstlisting}
455 typedef struct CvSparseMat
456 {
457     int type;
458     int dims; 
459     int* refcount; 
460     struct CvSet* heap; 
461     void** hashtable; 
462     int hashsize;
463     int total; 
464     int valoffset; 
465     int idxoffset; 
466     int size[CV_MAX_DIM]; 
467
468 } CvSparseMat;
469 \end{lstlisting}
470
471 \begin{description}
472 \cvarg{type}{A CvSparseMat signature (CV\_SPARSE\_MAT\_MAGIC\_VAL), combining the type of elements and flags.}
473 \cvarg{dims}{Number of dimensions}
474 \cvarg{refcount}{Underlying reference counter. Not used.}
475 \cvarg{heap}{A pool of hash table nodes}
476 \cvarg{hashtable}{The hash table. Each entry is a list of nodes.}
477 \cvarg{hashsize}{Size of the hash table}
478 \cvarg{total}{Total number of sparse array nodes}
479 \cvarg{valoffset}{The value offset of the array nodes, in bytes}
480 \cvarg{idxoffset}{The index offset of the array nodes, in bytes}
481 \cvarg{size}{Array of dimension sizes}
482 \end{description}
483
484 \fi
485
486 \label{IplImage}\cvclass{IplImage}
487 \ifC
488 IPL image header
489
490 \begin{lstlisting}
491 typedef struct _IplImage
492 {
493     int  nSize;         
494     int  ID;            
495     int  nChannels;     
496     int  alphaChannel;  
497     int  depth;         
498     char colorModel[4]; 
499     char channelSeq[4]; 
500     int  dataOrder;     
501     int  origin;        
502     int  align;         
503     int  width;         
504     int  height;        
505     struct _IplROI *roi; 
506     struct _IplImage *maskROI; 
507     void  *imageId;     
508     struct _IplTileInfo *tileInfo; 
509     int  imageSize;                             
510     char *imageData;  
511     int  widthStep;   
512     int  BorderMode[4]; 
513     int  BorderConst[4]; 
514     char *imageDataOrigin; 
515 }
516 IplImage;
517 \end{lstlisting}
518
519 \begin{description}
520 \cvarg{nSize}{\texttt{sizeof(IplImage)}}
521 \cvarg{ID}{Version, always equals 0}
522 \cvarg{nChannels}{Number of channels. Most OpenCV functions support 1-4 channels.}
523 \cvarg{alphaChannel}{Ignored by OpenCV}
524 \cvarg{depth}{Pixel depth in bits. The supported depths are:
525 \begin{description}
526 \cvarg{IPL\_DEPTH\_8U}{Unsigned 8-bit integer}
527 \cvarg{IPL\_DEPTH\_8S}{Signed 8-bit integer}
528 \cvarg{IPL\_DEPTH\_16U}{Unsigned 16-bit integer}
529 \cvarg{IPL\_DEPTH\_16S}{Signed 16-bit integer}
530 \cvarg{IPL\_DEPTH\_32S}{Signed 32-bit integer}
531 \cvarg{IPL\_DEPTH\_32F}{Single-precision floating point}
532 \cvarg{IPL\_DEPTH\_64F}{Double-precision floating point}
533 \end{description}}
534 \cvarg{colorModel}{Ignored by OpenCV. The OpenCV function \cross{CvtColor} requires the source and destination color spaces as parameters.}
535 \cvarg{channelSeq}{Ignored by OpenCV}
536 \cvarg{dataOrder}{0 = \texttt{IPL\_DATA\_ORDER\_PIXEL} - interleaved color channels, 1 - separate color channels. \cross{CreateImage} only creates images with interleaved channels. For example, the usual layout of a color image is: $ b_{00} g_{00} r_{00} b_{10} g_{10} r_{10} ...$}
537 \cvarg{origin}{0 - top-left origin, 1 - bottom-left origin (Windows bitmap style)}
538 \cvarg{align}{Alignment of image rows (4 or 8). OpenCV ignores this and uses widthStep instead.}
539 \cvarg{width}{Image width in pixels}
540 \cvarg{height}{Image height in pixels}
541 \cvarg{roi}{Region Of Interest (ROI). If not NULL, only this image region will be processed.}
542 \cvarg{maskROI}{Must be NULL in OpenCV}
543 \cvarg{imageId}{Must be NULL in OpenCV}
544 \cvarg{tileInfo}{Must be NULL in OpenCV}
545 \cvarg{imageSize}{Image data size in bytes. For interleaved data, this equals $\texttt{image->height} \cdot \texttt{image->widthStep}$ }
546 \cvarg{imageData}{A pointer to the aligned image data}
547 \cvarg{widthStep}{The size of an aligned image row, in bytes}
548 \cvarg{BorderMode}{Border completion mode, ignored by OpenCV}
549 \cvarg{BorderConst}{Border completion mode, ignored by OpenCV}
550 \cvarg{imageDataOrigin}{A pointer to the origin of the image data (not necessarily aligned). This is used for image deallocation.}
551 \end{description}
552
553 The \cross{IplImage} structure was inherited from the Intel Image Processing Library, in which the format is native. OpenCV only supports a subset of possible \cross{IplImage} formats, as outlined in the parameter list above.
554
555 In addition to the above restrictions, OpenCV handles ROIs differently. OpenCV functions require that the image size or ROI size of all source and destination images match exactly. On the other hand, the Intel Image Processing Library processes the area of intersection between the source and destination images (or ROIs), allowing them to vary independently. 
556 \fi
557
558 \ifPy
559
560 The \cross{IplImage} object was inherited from the Intel Image Processing
561 Library, in which the format is native. OpenCV only supports a subset
562 of possible \cross{IplImage} formats.
563
564 \begin{description}
565 \cvarg{nChannels}{Number of channels, int.}
566 \cvarg{width}{Image width in pixels}
567 \cvarg{height}{Image height in pixels}
568 \cvarg{depth}{Pixel depth in bits. The supported depths are:
569 \begin{description}
570 \cvarg{IPL\_DEPTH\_8U}{Unsigned 8-bit integer}
571 \cvarg{IPL\_DEPTH\_8S}{Signed 8-bit integer}
572 \cvarg{IPL\_DEPTH\_16U}{Unsigned 16-bit integer}
573 \cvarg{IPL\_DEPTH\_16S}{Signed 16-bit integer}
574 \cvarg{IPL\_DEPTH\_32S}{Signed 32-bit integer}
575 \cvarg{IPL\_DEPTH\_32F}{Single-precision floating point}
576 \cvarg{IPL\_DEPTH\_64F}{Double-precision floating point}
577 \end{description}}
578 \cvarg{origin}{0 - top-left origin, 1 - bottom-left origin (Windows bitmap style)}
579 \cvarg{tostring() -> str}{Returns the contents of the CvMatND as a single string.}
580 \end{description}
581 \fi
582
583 \label{CvArr}\cvclass{CvArr}
584 Arbitrary array
585
586 \ifC
587 \begin{lstlisting}
588 typedef void CvArr;
589 \end{lstlisting}
590
591 The metatype \texttt{CvArr} is used \textit{only} as a function parameter to specify that the function accepts arrays of multiple types, such as IplImage*, CvMat* or even CvSeq* sometimes. The particular array type is determined at runtime by analyzing the first 4 bytes of the header.
592 \fi
593
594 \ifPy
595 \texttt{CvArr} is used \textit{only} as a function parameter to specify that the parameter can be:
596 \begin{itemize}
597 \item{an \cross{IplImage}}
598 \item{a \cross{CvMat}}
599 \item{any other type that exports the \href{http://docs.scipy.org/doc/numpy/reference/arrays.interface.html}{array interface}}
600 \end{itemize}
601 \fi
602
603 \fi
604
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 %                                                                                    %
607 %                                        C++                                         %
608 %                                                                                    %
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610
611 \ifCpp
612 \subsection{DataType}\label{DataType}
613 Template "traits" class for other OpenCV primitive data types
614
615 \begin{lstlisting}
616 template<typename _Tp> class DataType
617 {
618     // value_type is always a synonym for _Tp.
619     typedef _Tp value_type;
620     
621     // intermediate type used for operations on _Tp.
622     // it is int for uchar, signed char, unsigned short, signed short and int,
623     // float for float, double for double, ...
624     typedef <...> work_type;
625     // in the case of multi-channel data it is the data type of each channel
626     typedef <...> channel_type;
627     enum
628     {
629         // CV_8U ... CV_64F
630         depth = DataDepth<channel_type>::value,
631         // 1 ... 
632         channels = <...>,
633         // '1u', '4i', '3f', '2d' etc.
634         fmt=<...>,
635         // CV_8UC3, CV_32FC2 ...
636         type = CV_MAKETYPE(depth, channels)
637     };
638 };
639 \end{lstlisting}
640
641 The template class \texttt{DataType} is descriptive class for OpenCV primitive data types and other types that comply with the following definition. A primitive OpenCV data type is one of \texttt{unsigned char, bool ($\sim$unsigned char), signed char, unsigned short, signed short, int, float, double} or a tuple of values of one of these types, where all the values in the tuple have the same type. If you are familiar with OpenCV \cross{CvMat}'s type notation, CV\_8U ... CV\_32FC3, CV\_64FC2 etc., then a primitive type can be defined as a type for which you can give a unique identifier in a form \texttt{CV\_<bit-depth>{U|S|F}C<number\_of\_channels>}. A universal OpenCV structure able to store a single instance of such primitive data type is \cross{Vec}. Multiple instances of such a type can be stored to a \texttt{std::vector}, \texttt{Mat}, \texttt{Mat\_}, \texttt{MatND}, \texttt{MatND\_}, \texttt{SparseMat}, \texttt{SparseMat\_} or any other container that is able to store \cross{Vec} instances.
642  
643 The class \texttt{DataType} is basically used to provide some description of such primitive data types without adding any fields or methods to the corresponding classes (and it is actually impossible to add anything to primitive C/C++ data types). This technique is known in C++ as class traits. It's not \texttt{DataType} itself that is used, but its specialized versions, such as:
644
645 \begin{lstlisting}
646 template<> class DataType<uchar>
647 {
648     typedef uchar value_type;
649     typedef int work_type;
650     typedef uchar channel_type;
651     enum { channel_type = CV_8U, channels = 1, fmt='u', type = CV_8U };
652 };
653 ...
654 template<typename _Tp> DataType<std::complex<_Tp> >
655 {
656     typedef std::complex<_Tp> value_type;
657     typedef std::complex<_Tp> work_type;
658     typedef _Tp channel_type;
659     // DataDepth is another helper trait class
660     enum { depth = DataDepth<_Tp>::value, channels=2,
661         fmt=(channels-1)*256+DataDepth<_Tp>::fmt,
662         type=CV_MAKETYPE(depth, channels) };
663 };
664 ...
665 \end{lstlisting}
666
667 The main purpose of the classes is to convert compile-time type information to OpenCV-compatible data type identifier, for example:
668
669 \begin{lstlisting}
670 // allocates 30x40 floating-point matrix
671 Mat A(30, 40, DataType<float>::type);
672
673 Mat B = Mat_<std::complex<double> >(3, 3);
674 // the statement below will print 6, 2 /* i.e. depth == CV_64F, channels == 2 */ 
675 cout << B.depth() << ", " << B.channels() << endl; 
676 \end{lstlisting}
677
678 that is, such traits are used to tell OpenCV which data type you are working with, even if such a type is not native to OpenCV (the matrix \texttt{B} intialization above compiles because OpenCV defines the proper specialized template class \texttt{DataType<complex<\_Tp> >}). Also, this mechanism is useful (and used in OpenCV this way) for generic algorithms implementations.
679
680 \subsection{Point\_}
681 Template class for 2D points
682
683 \begin{lstlisting}
684 template<typename _Tp> class Point_
685 {
686 public:
687     typedef _Tp value_type;
688     
689     Point_();
690     Point_(_Tp _x, _Tp _y);
691     Point_(const Point_& pt);
692     Point_(const CvPoint& pt);
693     Point_(const CvPoint2D32f& pt);
694     Point_(const Size_<_Tp>& sz);
695     Point_(const Vec<_Tp, 2>& v);
696     Point_& operator = (const Point_& pt);
697     template<typename _Tp2> operator Point_<_Tp2>() const;
698     operator CvPoint() const;
699     operator CvPoint2D32f() const;
700     operator Vec<_Tp, 2>() const;
701
702     // computes dot-product (this->x*pt.x + this->y*pt.y)
703     _Tp dot(const Point_& pt) const;
704     // computes dot-product using double-precision arithmetics
705     double ddot(const Point_& pt) const;
706     // returns true if the point is inside the rectangle "r".
707     bool inside(const Rect_<_Tp>& r) const;
708     
709     _Tp x, y;
710 };
711 \end{lstlisting}
712
713 The class represents a 2D point, specified by its coordinates $x$ and $y$.
714 Instance of the class is interchangeable with C structures \texttt{CvPoint} and \texttt{CvPoint2D32f}. There is also cast operator to convert point coordinates to the specified type. The conversion from floating-point coordinates to integer coordinates is done by rounding; in general case the conversion uses \hyperref[saturatecast]{saturate\_cast} operation on each of the coordinates. Besides the class members listed in the declaration above, the following operations on points are implemented:
715
716 \begin{lstlisting}
717     pt1 = pt2 + pt3;
718     pt1 = pt2 - pt3;
719     pt1 = pt2 * a;
720     pt1 = a * pt2;
721     pt1 += pt2;
722     pt1 -= pt2;
723     pt1 *= a;
724     double value = norm(pt); // L2 norm
725     pt1 == pt2;
726     pt1 != pt2;
727 \end{lstlisting}
728
729 For user convenience, the following type aliases are defined:
730 \begin{lstlisting}
731 typedef Point_<int> Point2i;
732 typedef Point2i Point;
733 typedef Point_<float> Point2f;
734 typedef Point_<double> Point2d;
735 \end{lstlisting}
736
737 Here is a short example:
738 \begin{lstlisting}
739 Point2f a(0.3f, 0.f), b(0.f, 0.4f);
740 Point pt = (a + b)*10.f;
741 cout << pt.x << ", " << pt.y << endl; 
742 \end{lstlisting}
743
744 \subsection{Point3\_}
745
746 Template class for 3D points
747
748 \begin{lstlisting}
749
750 template<typename _Tp> class Point3_
751 {
752 public:
753     typedef _Tp value_type;
754     
755     Point3_();
756     Point3_(_Tp _x, _Tp _y, _Tp _z);
757     Point3_(const Point3_& pt);
758     explicit Point3_(const Point_<_Tp>& pt);
759     Point3_(const CvPoint3D32f& pt);
760     Point3_(const Vec<_Tp, 3>& v);
761     Point3_& operator = (const Point3_& pt);
762     template<typename _Tp2> operator Point3_<_Tp2>() const;
763     operator CvPoint3D32f() const;
764     operator Vec<_Tp, 3>() const;
765
766     _Tp dot(const Point3_& pt) const;
767     double ddot(const Point3_& pt) const;
768     
769     _Tp x, y, z;
770 };
771 \end{lstlisting}
772
773 The class represents a 3D point, specified by its coordinates $x$, $y$ and $z$.
774 Instance of the class is interchangeable with C structure \texttt{CvPoint2D32f}. Similarly to \texttt{Point\_}, the 3D points' coordinates can be converted to another type, and the vector arithmetic and comparison operations are also supported.
775
776 The following type aliases are available:
777
778 \begin{lstlisting}
779 typedef Point3_<int> Point3i;
780 typedef Point3_<float> Point3f;
781 typedef Point3_<double> Point3d;
782 \end{lstlisting}
783
784 \subsection{Size\_}
785
786 Template class for specfying image or rectangle size.
787
788 \begin{lstlisting}
789 template<typename _Tp> class Size_
790 {
791 public:
792     typedef _Tp value_type;
793     
794     Size_();
795     Size_(_Tp _width, _Tp _height);
796     Size_(const Size_& sz);
797     Size_(const CvSize& sz);
798     Size_(const CvSize2D32f& sz);
799     Size_(const Point_<_Tp>& pt);
800     Size_& operator = (const Size_& sz);
801     _Tp area() const;
802
803     operator Size_<int>() const;
804     operator Size_<float>() const;
805     operator Size_<double>() const;
806     operator CvSize() const;
807     operator CvSize2D32f() const;
808
809     _Tp width, height;
810 };
811 \end{lstlisting}
812
813 The class \texttt{Size\_} is similar to \texttt{Point\_}, except that the two members are called \texttt{width} and \texttt{height} instead of \texttt{x} and \texttt{y}. The structure can be converted to and from the old OpenCV structures \cross{CvSize} and \cross{CvSize2D32f}. The same set of arithmetic and comparison operations as for \texttt{Point\_} is available. 
814
815 OpenCV defines the following type aliases:
816
817 \begin{lstlisting}
818 typedef Size_<int> Size2i;
819 typedef Size2i Size;
820 typedef Size_<float> Size2f;
821 \end{lstlisting}
822
823 \subsection{Rect\_}
824
825 Template class for 2D rectangles
826
827 \begin{lstlisting}
828 template<typename _Tp> class Rect_
829 {
830 public:
831     typedef _Tp value_type;
832     
833     Rect_();
834     Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
835     Rect_(const Rect_& r);
836     Rect_(const CvRect& r);
837     // (x, y) <- org, (width, height) <- sz
838     Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
839     // (x, y) <- min(pt1, pt2), (width, height) <- max(pt1, pt2) - (x, y)
840     Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
841     Rect_& operator = ( const Rect_& r );
842     // returns Point_<_Tp>(x, y)
843     Point_<_Tp> tl() const;
844     // returns Point_<_Tp>(x+width, y+height)
845     Point_<_Tp> br() const;
846     
847     // returns Size_<_Tp>(width, height)
848     Size_<_Tp> size() const;
849     // returns width*height
850     _Tp area() const;
851
852     operator Rect_<int>() const;
853     operator Rect_<float>() const;
854     operator Rect_<double>() const;
855     operator CvRect() const;
856
857     // x <= pt.x && pt.x < x + width &&
858     // y <= pt.y && pt.y < y + height ? true : false
859     bool contains(const Point_<_Tp>& pt) const;
860
861     _Tp x, y, width, height;
862 };
863 \end{lstlisting}
864
865 The rectangle is described by the coordinates of the top-left corner (which is the default interpretation of \texttt{Rect\_::x} and \texttt{Rect\_::y} in OpenCV; though, in your algorithms you may count \texttt{x} and \texttt{y} from the bottom-left corner), the rectangle width and height.
866
867 Another assumption OpenCV usually makes is that the top and left boundary of the rectangle are inclusive, while the right and bottom boundaries are not, for example, the method \texttt{Rect\_::contains} returns true if
868 \begin{eqnarray*}
869       x \leq pt.x < x+width,\\
870       y \leq pt.y < y+height
871 \end{eqnarray*}
872 And virtually every loop over an image \cross{ROI} in OpenCV (where ROI is specified by \texttt{Rect\_<int>}) is implemented as:
873 \begin{lstlisting}
874 for(int y = roi.y; y < roi.y + rect.height; y++)
875     for(int x = roi.x; x < roi.x + rect.width; x++)
876     {
877         // ...
878     }
879 \end{lstlisting}
880
881 In addition to the class members, the following operations on rectangles are implemented:
882 \begin{itemize}
883     \item $\texttt{rect} = \texttt{rect} \pm \texttt{point}$ (shifting rectangle by a certain offset)
884     \item $\texttt{rect} = \texttt{rect} \pm \texttt{size}$ (expanding or shrinking rectangle by a certain amount)
885     \item \texttt{rect += point, rect -= point, rect += size, rect -= size} (augmenting operations)
886     \item \texttt{rect = rect1 \& rect2} (rectangle intersection)
887     \item \texttt{rect = rect1 | rect2} (minimum area rectangle containing \texttt{rect2} and \texttt{rect3})
888     \item \texttt{rect \&= rect1, rect |= rect1} (and the corresponding augmenting operations)
889     \item \texttt{rect == rect1, rect != rect1} (rectangle comparison)
890 \end{itemize}
891
892 Example. Here is how the partial ordering on rectangles can be established (rect1 $\subseteq$ rect2):
893 \begin{lstlisting}
894 template<typename _Tp> inline bool
895 operator <= (const Rect_<_Tp>& r1, const Rect_<_Tp>& r2)
896 {
897     return (r1 & r2) == r1;
898 }
899 \end{lstlisting}
900
901 For user convenience, the following type alias is available:
902 \begin{lstlisting}
903 typedef Rect_<int> Rect;
904 \end{lstlisting}
905
906 \subsection{RotatedRect}\label{RotatedRect}
907 Possibly rotated rectangle
908
909 \begin{lstlisting}
910 class RotatedRect
911 {
912 public:
913     // constructors
914     RotatedRect();
915     RotatedRect(const Point2f& _center, const Size2f& _size, float _angle);
916     RotatedRect(const CvBox2D& box);
917     
918     // returns minimal up-right rectangle that contains the rotated rectangle
919     Rect boundingRect() const;
920     // backward conversion to CvBox2D
921     operator CvBox2D() const;
922     
923     // mass center of the rectangle
924     Point2f center;
925     // size
926     Size2f size;
927     // rotation angle in degrees
928     float angle;
929 };
930 \end{lstlisting}
931
932 The class \texttt{RotatedRect} replaces the old \cross{CvBox2D} and fully compatible with it.
933
934 \subsection{TermCriteria}\label{TermCriteria}
935
936 Termination criteria for iterative algorithms
937
938 \begin{lstlisting}
939 class TermCriteria
940 {
941 public:
942     enum { COUNT=1, MAX_ITER=COUNT, EPS=2 };
943
944     // constructors
945     TermCriteria();
946     // type can be MAX_ITER, EPS or MAX_ITER+EPS.
947     // type = MAX_ITER means that only the number of iterations does matter;
948     // type = EPS means that only the required precision (epsilon) does matter
949     //    (though, most algorithms put some limit on the number of iterations anyway)
950     // type = MAX_ITER + EPS means that algorithm stops when
951     // either the specified number of iterations is made,
952     // or when the specified accuracy is achieved - whatever happens first.
953     TermCriteria(int _type, int _maxCount, double _epsilon);
954     TermCriteria(const CvTermCriteria& criteria);
955     operator CvTermCriteria() const;
956
957     int type;
958     int maxCount;
959     double epsilon;
960 };
961 \end{lstlisting}
962
963 The class \texttt{TermCriteria} replaces the old \cross{CvTermCriteria} and fully compatible with it.
964
965
966 \subsection{Vec}\label{Vec}
967 Template class for short numerical vectors
968
969 \begin{lstlisting}
970 template<typename _Tp, int cn> class Vec
971 {
972 public:
973     typedef _Tp value_type;
974     enum { depth = DataDepth<_Tp>::value, channels = cn,
975            type = CV_MAKETYPE(depth, channels) };
976     
977     // default constructor: all elements are set to 0
978     Vec();
979     // constructors taking up to 10 first elements as parameters
980     Vec(_Tp v0);
981     Vec(_Tp v0, _Tp v1);
982     Vec(_Tp v0, _Tp v1, _Tp v2);
983     ...
984     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4,
985         _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9);
986     Vec(const Vec<_Tp, cn>& v);
987     // constructs vector with all the components set to alpha.
988     static Vec all(_Tp alpha);
989     
990     // two variants of dot-product
991     _Tp dot(const Vec& v) const;
992     double ddot(const Vec& v) const;
993     
994     // cross-product; valid only when cn == 3.
995     Vec cross(const Vec& v) const;
996     
997     // element type conversion
998     template<typename T2> operator Vec<T2, cn>() const;
999     
1000     // conversion to/from CvScalar (valid only when cn==4)
1001     operator CvScalar() const;
1002     
1003     // element access
1004     _Tp operator [](int i) const;
1005     _Tp& operator[](int i);
1006
1007     _Tp val[cn];
1008 };
1009 \end{lstlisting}
1010
1011 The class is the most universal representation of short numerical vectors or tuples. It is possible to convert \texttt{Vec<T,2>} to/from \texttt{Point\_}, \texttt{Vec<T,3>} to/from \texttt{Point3\_}, and \texttt{Vec<T,4>} to \cross{CvScalar}~. The elements of \texttt{Vec} are accessed using \texttt{operator[]}. All the expected vector operations are implemented too:
1012
1013 \begin{itemize}
1014     \item \texttt{v1 = $v2 \pm v3$, v1 = v2 * $\alpha$, v1 = $\alpha$ * v2} (plus the corresponding augmenting operations; note that these operations apply \hyperref[saturatecast]{saturate\_cast.3C.3E} to the each computed vector component)
1015     \item \texttt{v1 == v2, v1 != v2}
1016     \item \texttt{double n = norm(v1); // $L_2$-norm}
1017 \end{itemize}
1018
1019 For user convenience, the following type aliases are introduced:
1020 \begin{lstlisting}
1021 typedef Vec<uchar, 2> Vec2b;
1022 typedef Vec<uchar, 3> Vec3b;
1023 typedef Vec<uchar, 4> Vec4b;
1024
1025 typedef Vec<short, 2> Vec2s;
1026 typedef Vec<short, 3> Vec3s;
1027 typedef Vec<short, 4> Vec4s;
1028
1029 typedef Vec<int, 2> Vec2i;
1030 typedef Vec<int, 3> Vec3i;
1031 typedef Vec<int, 4> Vec4i;
1032
1033 typedef Vec<float, 2> Vec2f;
1034 typedef Vec<float, 3> Vec3f;
1035 typedef Vec<float, 4> Vec4f;
1036 typedef Vec<float, 6> Vec6f;
1037
1038 typedef Vec<double, 2> Vec2d;
1039 typedef Vec<double, 3> Vec3d;
1040 typedef Vec<double, 4> Vec4d;
1041 typedef Vec<double, 6> Vec6d;
1042 \end{lstlisting}
1043
1044 The class \texttt{Vec} can be used for declaring various numerical objects, e.g. \texttt{Vec<double,9>} can be used to store a 3x3 double-precision matrix. It is also very useful for declaring and processing multi-channel arrays, see \texttt{Mat\_} description.
1045
1046 \subsection{Scalar\_}
1047 4-element vector
1048
1049 \begin{lstlisting}
1050 template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>
1051 {
1052 public:
1053     Scalar_();
1054     Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
1055     Scalar_(const CvScalar& s);
1056     Scalar_(_Tp v0);
1057     static Scalar_<_Tp> all(_Tp v0);
1058     operator CvScalar() const;
1059
1060     template<typename T2> operator Scalar_<T2>() const;
1061
1062     Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const;
1063     template<typename T2> void convertTo(T2* buf, int channels, int unroll_to=0) const;
1064 };
1065
1066 typedef Scalar_<double> Scalar;
1067 \end{lstlisting}
1068
1069 The template class \texttt{Scalar\_} and it's double-precision instantiation \texttt{Scalar} represent 4-element vector. Being derived from \texttt{Vec<\_Tp, 4>}, they can be used as typical 4-element vectors, but in addition they can be converted to/from \texttt{CvScalar}. The type \texttt{Scalar} is widely used in OpenCV for passing pixel values and it is a drop-in replacement for \cross{CvScalar} that was used for the same purpose in the earlier versions of OpenCV.
1070
1071 \subsection{Range}\label{Range}
1072 Specifies a continuous subsequence (a.k.a. slice) of a sequence.
1073
1074 \begin{lstlisting}
1075 class Range
1076 {
1077 public:
1078     Range();
1079     Range(int _start, int _end);
1080     Range(const CvSlice& slice);
1081     int size() const;
1082     bool empty() const;
1083     static Range all();
1084     operator CvSlice() const;
1085
1086     int start, end;
1087 };
1088 \end{lstlisting}
1089
1090 The class is used to specify a row or column span in a matrix (\cross{Mat}), and for many other purposes. \texttt{Range(a,b)} is basically the same as \texttt{a:b} in Matlab or \texttt{a..b} in Python. As in Python, \texttt{start} is inclusive left boundary of the range, and \texttt{end} is exclusive right boundary of the range. Such a half-opened interval is usually denoted as $[start,end)$.
1091
1092 The static method \texttt{Range::all()} returns some special variable that means "the whole sequence" or "the whole range", just like "\texttt{:}" in Matlab or "\texttt{...}" in Python. All the methods and functions in OpenCV that take \texttt{Range} support this special \texttt{Range::all()} value, but of course, in the case of your own custom processing you will probably have to check and handle it explicitly:
1093 \begin{lstlisting}
1094 void my_function(..., const Range& r, ....)
1095 {
1096     if(r == Range::all()) {
1097         // process all the data
1098     }
1099     else {
1100         // process [r.start, r.end)
1101     } 
1102 }
1103 \end{lstlisting}
1104
1105 \subsection{Ptr}\label{Ptr}
1106
1107 A template class for smart reference-counting pointers
1108
1109 \begin{lstlisting}
1110 template<typename _Tp> class Ptr
1111 {
1112 public:
1113     // default constructor
1114     Ptr();
1115     // constructor that wraps the object pointer
1116     Ptr(_Tp* _obj);
1117     // destructor: calls release()
1118     ~Ptr();
1119     // copy constructor; increments ptr's reference counter
1120     Ptr(const Ptr& ptr);
1121     // assignment operator; decrements own reference counter
1122     // (with release()) and increments ptr's reference counter 
1123     Ptr& operator = (const Ptr& ptr);
1124     // increments reference counter
1125     void addref();
1126     // decrements reference counter; when it becomes 0,
1127     // delete_obj() is called
1128     void release();
1129     // user-specified custom object deletion operation.
1130     // by default, "delete obj;" is called
1131     void delete_obj();
1132     // returns true if obj == 0;
1133     bool empty() const;
1134
1135     // provide access to the object fields and methods
1136     _Tp* operator -> ();
1137     const _Tp* operator -> () const;
1138
1139     // return the underlying object pointer;
1140     // thanks to the methods, the Ptr<_Tp> can be
1141     // used instead of _Tp*
1142     operator _Tp* ();
1143     operator const _Tp*() const;
1144 protected:
1145     // the incapsulated object pointer
1146     _Tp* obj;
1147     // the associated reference counter
1148     int* refcount;
1149 };
1150 \end{lstlisting}
1151
1152 The class \texttt{Ptr<\_Tp>} is a template class that wraps pointers of the corresponding type. It is similar to \texttt{shared\_ptr} that is a part of Boost library (\url{http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/shared_ptr.htm}) and also a part of the
1153 \href{http://en.wikipedia.org/wiki/C++0x}{C++0x} standard. 
1154
1155 By using this class you can get the following capabilities:
1156
1157 \begin{itemize}
1158     \item default constructor, copy constructor and assignment operator for an arbitrary C++ class or a C structure. For some objects, like files, windows, mutexes, sockets etc, copy constructor or assignment operator are difficult to define. For some other objects, like complex classifiers in OpenCV, copy constructors are absent and not easy to implement. Finally, some of complex OpenCV and your own data structures may have been written in C. However, copy constructors and default constructors can simplify programming a lot; besides, they are often required (e.g. by STL containers). By wrapping a pointer to such a complex object \texttt{TObj} to \texttt{Ptr<TObj>} you will automatically get all of the necessary constructors and the assignment operator.
1159     \item all the above-mentioned operations running very fast, regardless of the data size, i.e. as "O(1)" operations. Indeed, while some structures, like \texttt{std::vector} provide a copy constructor and an assignment operator, the operations may take considerable time if the data structures are big. But if the structures are put into \texttt{Ptr<>}, the overhead becomes small and independent of the data size.
1160     \item automatic destruction, even for C structures. See the example below with \texttt{FILE*}.  
1161     \item heterogeneous collections of objects. The standard STL and most other C++ and OpenCV containers can only store objects of the same type and the same size. The classical solution to store objects of different types in the same container is to store pointers to the base class \texttt{base\_class\_t*} instead, but when you loose the automatic memory management. Again, by using \texttt{Ptr<base\_class\_t>()} instead of the raw pointers, you can solve the problem.
1162 \end{itemize}    
1163
1164 The class \texttt{Ptr} treats the wrapped object as a black box, the reference counter is allocated and managed separately. The only thing the pointer class needs to know about the object is how to deallocate it. This knowledge is incapsulated in \texttt{Ptr::delete\_obj()} method, which is called when the reference counter becomes 0. If the object is a C++ class instance, no additional coding is needed, because the default implementation of this method calls \texttt{delete obj;}.
1165 However, if the object is deallocated in a different way, then the specialized method should be created. For example, if you want to wrap \texttt{FILE}, the \texttt{delete\_obj} may be implemented as following:
1166
1167 \begin{lstlisting}
1168 template<> inline void Ptr<FILE>::delete_obj()
1169 {
1170     fclose(obj); // no need to clear the pointer afterwards,
1171                  // it is done externally.
1172 }
1173 ...
1174
1175 // now use it:
1176 Ptr<FILE> f(fopen("myfile.txt", "r"));
1177 if(f.empty())
1178     throw ...;
1179 fprintf(f, ....);
1180 ...
1181 // the file will be closed automatically by the Ptr<FILE> destructor.
1182 \end{lstlisting}  
1183
1184 \textbf{Note}: The reference increment/decrement operations are implemented as atomic operations, and therefore it is normally safe to use the classes in multi-threaded applications. The same is true for \cross{Mat} and other C++ OpenCV classes that operate on the reference counters.
1185
1186 \subsection{Mat}\label{Mat}
1187
1188 OpenCV C++ matrix class.
1189
1190 \begin{lstlisting}
1191 class Mat
1192 {
1193 public:
1194     // constructors
1195     Mat();
1196     // constructs matrix of the specified size and type
1197     // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
1198     Mat(int _rows, int _cols, int _type);
1199     // constucts matrix and fills it with the specified value _s.
1200     Mat(int _rows, int _cols, int _type, const Scalar& _s);
1201     Mat(Size _size, int _type);
1202     // copy constructor
1203     Mat(const Mat& m);
1204     // constructor for matrix headers pointing to user-allocated data
1205     Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
1206     Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);
1207     // creates a matrix header for a part of the bigger matrix
1208     Mat(const Mat& m, const Range& rowRange, const Range& colRange);
1209     Mat(const Mat& m, const Rect& roi);
1210     // converts old-style CvMat to the new matrix; the data is not copied by default
1211     Mat(const CvMat* m, bool copyData=false);
1212     // converts old-style IplImage to the new matrix; the data is not copied by default
1213     Mat(const IplImage* img, bool copyData=false);
1214     // builds matrix from std::vector with or without copying the data
1215     template<typename _Tp> Mat(const vector<_Tp>& vec, bool copyData=false);
1216     // helper constructor to compile matrix expressions
1217     Mat(const MatExpr_Base& expr);
1218     // destructor - calls release()
1219     ~Mat();
1220     // assignment operators
1221     Mat& operator = (const Mat& m);
1222     Mat& operator = (const MatExpr_Base& expr);
1223
1224     ...
1225     // returns a new matrix header for the specified row
1226     Mat row(int y) const;
1227     // returns a new matrix header for the specified column
1228     Mat col(int x) const;
1229     // ... for the specified row span
1230     Mat rowRange(int startrow, int endrow) const;
1231     Mat rowRange(const Range& r) const;
1232     // ... for the specified column span
1233     Mat colRange(int startcol, int endcol) const;
1234     Mat colRange(const Range& r) const;
1235     // ... for the specified diagonal
1236     // (d=0 - the main diagonal,
1237     //  >0 - a diagonal from the lower half,
1238     //  <0 - a diagonal from the upper half)
1239     Mat diag(int d=0) const;
1240     // constructs a square diagonal matrix which main diagonal is vector "d"
1241     static Mat diag(const Mat& d);
1242
1243     // returns deep copy of the matrix, i.e. the data is copied
1244     Mat clone() const;
1245     // copies the matrix content to "m".
1246     // It calls m.create(this->size(), this->type()).
1247     void copyTo( Mat& m ) const;
1248     // copies those matrix elements to "m" that are marked with non-zero mask elements.
1249     void copyTo( Mat& m, const Mat& mask ) const;
1250     // converts matrix to another datatype with optional scalng. See cvConvertScale.
1251     void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;
1252
1253     ...
1254     // sets every matrix element to s
1255     Mat& operator = (const Scalar& s);
1256     // sets some of the matrix elements to s, according to the mask
1257     Mat& setTo(const Scalar& s, const Mat& mask=Mat());
1258     // creates alternative matrix header for the same data, with different
1259     // number of channels and/or different number of rows. see cvReshape.
1260     Mat reshape(int _cn, int _rows=0) const;
1261
1262     // matrix transposition by means of matrix expressions
1263     MatExpr_<...> t() const;
1264     // matrix inversion by means of matrix expressions
1265     MatExpr_<...> inv(int method=DECOMP_LU) const;
1266     // per-element matrix multiplication by means of matrix expressions
1267     MatExpr_<...> mul(const Mat& m, double scale=1) const;
1268     MatExpr_<...> mul(const MatExpr_<...>& m, double scale=1) const;
1269
1270     // computes cross-product of 2 3D vectors
1271     Mat cross(const Mat& m) const;
1272     // computes dot-product
1273     double dot(const Mat& m) const;
1274
1275     // Matlab-style matrix initialization. see the description
1276     static MatExpr_Initializer zeros(int rows, int cols, int type);
1277     static MatExpr_Initializer zeros(Size size, int type);
1278     static MatExpr_Initializer ones(int rows, int cols, int type);
1279     static MatExpr_Initializer ones(Size size, int type);
1280     static MatExpr_Initializer eye(int rows, int cols, int type);
1281     static MatExpr_Initializer eye(Size size, int type);
1282     
1283     // allocates new matrix data unless the matrix already has specified size and type.
1284     // previous data is unreferenced if needed.
1285     void create(int _rows, int _cols, int _type);
1286     void create(Size _size, int _type);
1287     // increases the reference counter; use with care to avoid memleaks
1288     void addref();
1289     // decreases reference counter;
1290     // deallocate the data when reference counter reaches 0.
1291     void release();
1292
1293     // locates matrix header within a parent matrix. See below
1294     void locateROI( Size& wholeSize, Point& ofs ) const;
1295     // moves/resizes the current matrix ROI inside the parent matrix.
1296     Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
1297     // extracts a rectangular sub-matrix
1298     // (this is a generalized form of row, rowRange etc.)
1299     Mat operator()( Range rowRange, Range colRange ) const;
1300     Mat operator()( const Rect& roi ) const;
1301
1302     // converts header to CvMat; no data is copied
1303     operator CvMat() const;
1304     // converts header to IplImage; no data is copied
1305     operator IplImage() const;
1306     
1307     // returns true iff the matrix data is continuous
1308     // (i.e. when there are no gaps between successive rows).
1309     // similar to CV_IS_MAT_CONT(cvmat->type)
1310     bool isContinuous() const;
1311     // returns element size in bytes,
1312     // similar to CV_ELEM_SIZE(cvmat->type)
1313     size_t elemSize() const;
1314     // returns the size of element channel in bytes.
1315     size_t elemSize1() const;
1316     // returns element type, similar to CV_MAT_TYPE(cvmat->type)
1317     int type() const;
1318     // returns element type, similar to CV_MAT_DEPTH(cvmat->type)
1319     int depth() const;
1320     // returns element type, similar to CV_MAT_CN(cvmat->type)
1321     int channels() const;
1322     // returns step/elemSize1()
1323     size_t step1() const;
1324     // returns matrix size:
1325     // width == number of columns, height == number of rows
1326     Size size() const;
1327     // returns true if matrix data is NULL
1328     bool empty() const;
1329
1330     // returns pointer to y-th row
1331     uchar* ptr(int y=0);
1332     const uchar* ptr(int y=0) const;
1333
1334     // template version of the above method
1335     template<typename _Tp> _Tp* ptr(int y=0);
1336     template<typename _Tp> const _Tp* ptr(int y=0) const;
1337     
1338     // template methods for read-write or read-only element access.
1339     // note that _Tp must match the actual matrix type -
1340     // the functions do not do any on-fly type conversion
1341     template<typename _Tp> _Tp& at(int y, int x);
1342     template<typename _Tp> _Tp& at(Point pt);
1343     template<typename _Tp> const _Tp& at(int y, int x) const;
1344     template<typename _Tp> const _Tp& at(Point pt) const;
1345     
1346     // template methods for iteration over matrix elements.
1347     // the iterators take care of skipping gaps in the end of rows (if any)
1348     template<typename _Tp> MatIterator_<_Tp> begin();
1349     template<typename _Tp> MatIterator_<_Tp> end();
1350     template<typename _Tp> MatConstIterator_<_Tp> begin() const;
1351     template<typename _Tp> MatConstIterator_<_Tp> end() const;
1352
1353     enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG };
1354
1355     // includes several bit-fields:
1356     //  * the magic signature
1357     //  * continuity flag
1358     //  * depth
1359     //  * number of channels
1360     int flags;
1361     // the number of rows and columns
1362     int rows, cols;
1363     // a distance between successive rows in bytes; includes the gap if any
1364     size_t step;
1365     // pointer to the data
1366     uchar* data;
1367
1368     // pointer to the reference counter;
1369     // when matrix points to user-allocated data, the pointer is NULL
1370     int* refcount;
1371     
1372     // helper fields used in locateROI and adjustROI
1373     uchar* datastart;
1374     uchar* dataend;
1375 };
1376 \end{lstlisting}
1377
1378 The class \texttt{Mat} represents a 2D numerical array that can act as a matrix (and further it's referred to as a matrix), image, optical flow map etc. It is very similar to \cross{CvMat} type from earlier versions of OpenCV, and similarly to \texttt{CvMat}, the matrix can be multi-channel, but it also fully supports \cross{ROI} mechanism, just like \cross{IplImage}.
1379
1380 There are many different ways to create \texttt{Mat} object. Here are the some popular ones:
1381 \begin{itemize}
1382 \item using \texttt{create(nrows, ncols, type)} method or
1383     the similar constructor \texttt{Mat(nrows, ncols, type[, fill\_value])} constructor.
1384     A new matrix of the specified size and specifed type will be allocated.
1385     \texttt{type} has the same meaning as in \cvCppCross{cvCreateMat} method,
1386     e.g. \texttt{CV\_8UC1} means 8-bit single-channel matrix,
1387     \texttt{CV\_32FC2} means 2-channel (i.e. complex) floating-point matrix etc:
1388         
1389 \begin{lstlisting}
1390 // make 7x7 complex matrix filled with 1+3j.
1391 cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
1392 // and now turn M to 100x60 15-channel 8-bit matrix.
1393 // The old content will be deallocated
1394 M.create(100,60,CV_8UC(15));
1395 \end{lstlisting}
1396         
1397     As noted in the introduction of this chapter, \texttt{create()}
1398     will only allocate a new matrix when the current matrix dimensionality
1399     or type are different from the specified.
1400         
1401 \item by using a copy constructor or assignment operator, where on the right side it can
1402       be a matrix or expression, see below. Again, as noted in the introduction,
1403       matrix assignment is O(1) operation because it only copies the header
1404       and increases the reference counter. \texttt{Mat::clone()} method can be used to get a full
1405       (a.k.a. deep) copy of the matrix when you need it.
1406           
1407 \item by constructing a header for a part of another matrix. It can be a single row, single column,
1408       several rows, several columns, rectangular region in the matrix (called a minor in algebra) or
1409       a diagonal. Such operations are also O(1), because the new header will reference the same data.
1410       You can actually modify a part of the matrix using this feature, e.g.
1411           
1412 \begin{lstlisting}
1413 // add 5-th row, multiplied by 3 to the 3rd row
1414 M.row(3) = M.row(3) + M.row(5)*3;
1415
1416 // now copy 7-th column to the 1-st column
1417 // M.col(1) = M.col(7); // this will not work
1418 Mat M1 = M.col(1);
1419 M.col(7).copyTo(M1);
1420
1421 // create new 320x240 image
1422 cv::Mat img(Size(320,240),CV_8UC3);
1423 // select a roi
1424 cv::Mat roi(img, Rect(10,10,100,100));
1425 // fill the ROI with (0,255,0) (which is green in RGB space);
1426 // the original 320x240 image will be modified
1427 roi = Scalar(0,255,0);
1428 \end{lstlisting}
1429
1430       Thanks to the additional \texttt{datastart} and \texttt{dataend} members, it is possible to
1431       compute the relative sub-matrix position in the main \emph{"container"} matrix using \texttt{locateROI()}:
1432       
1433 \begin{lstlisting}
1434 Mat A = Mat::eye(10, 10, CV_32S);
1435 // extracts A columns, 1 (inclusive) to 3 (exclusive).
1436 Mat B = A(Range::all(), Range(1, 3));
1437 // extracts B rows, 5 (inclusive) to 9 (exclusive).
1438 // that is, C ~ A(Range(5, 9), Range(1, 3))
1439 Mat C = B(Range(5, 9), Range::all());
1440 Size size; Point ofs;
1441 C.locateROI(size, ofs);
1442 // size will be (width=10,height=10) and the ofs will be (x=1, y=5)
1443 \end{lstlisting}
1444           
1445       As in the case of whole matrices, if you need a deep copy, use \texttt{clone()} method
1446       of the extracted sub-matrices.
1447           
1448 \item by making a header for user-allocated-data. It can be useful for
1449     \begin{enumerate}
1450         \item processing "foreign" data using OpenCV (e.g. when you implement
1451         a DirectShow filter or a processing module for gstreamer etc.), e.g.
1452             
1453 \begin{lstlisting}
1454 void process_video_frame(const unsigned char* pixels,
1455                          int width, int height, int step)
1456 {
1457     cv::Mat img(height, width, CV_8UC3, pixels, step);
1458     cv::GaussianBlur(img, img, cv::Size(7,7), 1.5, 1.5);
1459 }
1460 \end{lstlisting}
1461             
1462         \item for quick initialization of small matrices and/or super-fast element access
1463 \begin{lstlisting}
1464 double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};
1465 cv::Mat M = cv::Mat(3, 3, CV_64F, m).inv();
1466 \end{lstlisting}
1467         \end{enumerate}
1468         
1469         partial yet very common cases of this "user-allocated data" case are conversions
1470         from \cross{CvMat} and \cross{IplImage} to \texttt{Mat}. For this purpose there are special constructors
1471         taking pointers to \texttt{CvMat} or \texttt{IplImage} and the optional
1472         flag indicating whether to copy the data or not.
1473         
1474         Backward conversion from \texttt{Mat} to \texttt{CvMat} or \texttt{IplImage} is provided via cast operators
1475         \texttt{Mat::operator CvMat() const} an \texttt{Mat::operator IplImage()}.
1476         The operators do \emph{not} copy the data.
1477         
1478 \begin{lstlisting}
1479 IplImage* img = cvLoadImage("greatwave.jpg", 1);
1480 Mat mtx(img); // convert IplImage* -> cv::Mat
1481 CvMat oldmat = mtx; // convert cv::Mat -> CvMat
1482 CV_Assert(oldmat.cols == img->width && oldmat.rows == img->height &&
1483     oldmat.data.ptr == (uchar*)img->imageData && oldmat.step == img->widthStep);
1484 \end{lstlisting}
1485         
1486 \item by using MATLAB-style matrix initializers, \texttt{zeros(), ones(), eye()}, e.g.:
1487
1488 \begin{lstlisting}
1489 // create a double-precision identity martix and add it to M.
1490 M += Mat::eye(M.rows, M.cols, CV_64F);
1491 \end{lstlisting}
1492
1493 \item by using comma-separated initializer:
1494 \begin{lstlisting}
1495 // create 3x3 double-precision identity matrix
1496 Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
1497 \end{lstlisting}
1498
1499 here we first call constructor of \texttt{Mat\_} class (that we describe further) with the proper matrix, and then we just put \texttt{<<} operator followed by comma-separated values that can be constants, variables, expressions etc. Also, note the extra parentheses that are needed to avoid compiler errors.
1500         
1501 \end{itemize}
1502
1503 Once matrix is created, it will be automatically managed by using reference-counting mechanism (unless the matrix header is built on top of user-allocated data, in which case you should handle the data by yourself).
1504 The matrix data will be deallocated when no one points to it; if you want to release the data pointed by a matrix header before the matrix destructor is called, use \texttt{Mat::release()}.
1505
1506 The next important thing to learn about the matrix class is element access. Here is how the matrix is stored. The elements are stored in row-major order (row by row). The \texttt{Mat::data} member points to the first element of the first row, \texttt{Mat::rows} contains the number of matrix rows and \texttt{Mat::cols} -- the number of matrix columns. There is yet another member, called \texttt{Mat::step} that is used to actually compute address of a matrix element. The \texttt{Mat::step} is needed because the matrix can be a part of another matrix or because there can some padding space in the end of each row for a proper alignment.
1507 %\includegraphics[width=1.0\textwidth]{pics/roi.png}
1508
1509 Given these parameters, address of the matrix element $M_{ij}$ is computed as following:
1510
1511
1512 \texttt{addr($M_{ij}$)=M.data + M.step*i + j*M.elemSize()}
1513
1514
1515 if you know the matrix element type, e.g. it is \texttt{float}, then you can use \texttt{at<>()} method:
1516
1517
1518 \texttt{addr($M_{ij}$)=\&M.at<float>(i,j)}
1519
1520 (where \& is used to convert the reference returned by \texttt{at} to a pointer).
1521 if you need to process a whole row of matrix, the most efficient way is to get the pointer to the row first, and then just use plain C operator \texttt{[]}:
1522
1523 \begin{lstlisting}
1524 // compute sum of positive matrix elements
1525 // (assuming that M is double-precision matrix)
1526 double sum=0;
1527 for(int i = 0; i < M.rows; i++)
1528 {
1529     const double* Mi = M.ptr<double>(i);
1530     for(int j = 0; j < M.cols; j++)
1531         sum += std::max(Mi[j], 0.);
1532 }
1533 \end{lstlisting}
1534
1535 Some operations, like the above one, do not actually depend on the matrix shape, they just process elements of a matrix one by one (or elements from multiple matrices that are sitting in the same place, e.g. matrix addition). Such operations are called element-wise and it makes sense to check whether all the input/output matrices are continuous, i.e. have no gaps in the end of each row, and if yes, process them as a single long row:
1536
1537 \begin{lstlisting}
1538 // compute sum of positive matrix elements, optimized variant
1539 double sum=0;
1540 int cols = M.cols, rows = M.rows;
1541 if(M.isContinuous())
1542 {
1543     cols *= rows;
1544     rows = 1;
1545 }
1546 for(int i = 0; i < rows; i++)
1547 {
1548     const double* Mi = M.ptr<double>(i);
1549     for(int j = 0; j < cols; j++)
1550         sum += std::max(Mi[j], 0.);
1551 }
1552 \end{lstlisting}
1553 in the case of continuous matrix the outer loop body will be executed just once, so the overhead will be smaller, which will be especially noticeable in the case of small matrices.
1554
1555 Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows:
1556 \begin{lstlisting}
1557 // compute sum of positive matrix elements, iterator-based variant
1558 double sum=0;
1559 MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>();
1560 for(; it != it_end; ++it)
1561     sum += std::max(*it, 0.);
1562 \end{lstlisting}
1563
1564 The matrix iterators are random-access iterators, so they can be passed to any STL algorithm, including \texttt{std::sort()}.
1565
1566 \subsection{Matrix Expressions}
1567
1568 This is a list of implemented matrix operations that can be combined in arbitrary complex expressions
1569 (here \emph{A}, \emph{B} stand for matrices (\texttt{Mat}), \emph{s} for a scalar (\texttt{Scalar}),
1570 \emph{$\alpha$} for a real-valued scalar (\texttt{double})):
1571
1572 \begin{itemize}
1573     \item addition, subtraction, negation: $\texttt{A}\pm \texttt{B},\;\texttt{A}\pm \texttt{s},\;\texttt{s}\pm \texttt{A},\;-\texttt{A}$
1574     \item scaling: \texttt{A*$\alpha$, A/$\alpha$}
1575     \item per-element multiplication and division: \texttt{A.mul(B), A/B, $\alpha$/A}
1576     \item matrix multiplication: \texttt{A*B}
1577     \item transposition: \texttt{A.t() $\sim A^t$}
1578     \item matrix inversion and pseudo-inversion, solving linear systems and least-squares problems:
1579         \texttt{A.inv([method]) $\sim A^{-1}$}, \texttt{A.inv([method])*B $\sim X:\,AX=B$}
1580     \item comparison: $\texttt{A}\gtreqqless \texttt{B},\;\texttt{A} \ne \texttt{B},\;\texttt{A}\gtreqqless \alpha,\; \texttt{A} \ne \alpha$.
1581           The result of comparison is 8-bit single channel mask, which elements are set to 255
1582           (if the particular element or pair of elements satisfy the condition) and 0 otherwise.
1583     \item bitwise logical operations: \verb"A & B, A & s, A | B, A | s, A ^ B, A ^ s, ~A"
1584     \item element-wise minimum and maximum: \texttt{min(A, B), min(A, $\alpha$), max(A, B), max(A, $\alpha$)}
1585     \item element-wise absolute value: \texttt{abs(A)}
1586     \item cross-product, dot-product: \texttt{A.cross(B), A.dot(B)}
1587     \item any function of matrix or matrices and scalars that returns a matrix or a scalar, such as
1588           \cvCppCross{norm}, \cvCppCross{mean}, \cvCppCross{sum}, \cvCppCross{countNonZero}, \cvCppCross{trace},
1589           \cvCppCross{determinant}, \cvCppCross{repeat} etc.
1590     \item matrix initializers (\texttt{eye(), zeros(), ones()}), matrix comma-separated initializers,
1591           matrix constructors and operators that extract sub-matrices (see \cross{Mat} description).
1592     \item \verb"Mat_<destination_type>()" constructors to cast the result to the proper type.
1593 \end{itemize}
1594 Note, however, that comma-separated initializers and probably some other operations may require additional explicit \texttt{Mat()} or \verb"Mat_<T>()" constuctor calls to resolve possible ambiguity.
1595
1596 \subsection{Mat\_}\label{MatT}
1597 Template matrix class derived from \cross{Mat}
1598
1599 \begin{lstlisting}
1600 template<typename _Tp> class Mat_ : public Mat
1601 {
1602 public:
1603     typedef _Tp value_type;
1604     typedef typename DataType<_Tp>::channel_type channel_type;
1605     typedef MatIterator_<_Tp> iterator;
1606     typedef MatConstIterator_<_Tp> const_iterator;
1607
1608     Mat_();
1609     // equivalent to Mat(_rows, _cols, DataType<_Tp>::type)
1610     Mat_(int _rows, int _cols);
1611     // other forms of the above constructor
1612     Mat_(int _rows, int _cols, const _Tp& value);
1613     explicit Mat_(Size _size);
1614     Mat_(Size _size, const _Tp& value);
1615     // copy/conversion contructor. If m is of different type, it's converted
1616     Mat_(const Mat& m);
1617     // copy constructor
1618     Mat_(const Mat_& m);
1619     // construct a matrix on top of user-allocated data.
1620     // step is in bytes(!!!), regardless of the type
1621     Mat_(int _rows, int _cols, _Tp* _data, size_t _step=AUTO_STEP);
1622     // minor selection
1623     Mat_(const Mat_& m, const Range& rowRange, const Range& colRange);
1624     Mat_(const Mat_& m, const Rect& roi);
1625     // to support complex matrix expressions
1626     Mat_(const MatExpr_Base& expr);
1627     // makes a matrix out of Vec or std::vector. The matrix will have a single column
1628     template<int n> explicit Mat_(const Vec<_Tp, n>& vec);
1629     Mat_(const vector<_Tp>& vec, bool copyData=false);
1630
1631     Mat_& operator = (const Mat& m);
1632     Mat_& operator = (const Mat_& m);
1633     // set all the elements to s.
1634     Mat_& operator = (const _Tp& s);
1635
1636     // iterators; they are smart enough to skip gaps in the end of rows
1637     iterator begin();
1638     iterator end();
1639     const_iterator begin() const;
1640     const_iterator end() const;
1641
1642     // equivalent to Mat::create(_rows, _cols, DataType<_Tp>::type)
1643     void create(int _rows, int _cols);
1644     void create(Size _size);
1645     // cross-product
1646     Mat_ cross(const Mat_& m) const;
1647     // to support complex matrix expressions
1648     Mat_& operator = (const MatExpr_Base& expr);
1649     // data type conversion
1650     template<typename T2> operator Mat_<T2>() const;
1651     // overridden forms of Mat::row() etc.
1652     Mat_ row(int y) const;
1653     Mat_ col(int x) const;
1654     Mat_ diag(int d=0) const;
1655     Mat_ clone() const;
1656
1657     // transposition, inversion, per-element multiplication
1658     MatExpr_<...> t() const;
1659     MatExpr_<...> inv(int method=DECOMP_LU) const;
1660
1661     MatExpr_<...> mul(const Mat_& m, double scale=1) const;
1662     MatExpr_<...> mul(const MatExpr_<...>& m, double scale=1) const;
1663
1664     // overridden forms of Mat::elemSize() etc.
1665     size_t elemSize() const;
1666     size_t elemSize1() const;
1667     int type() const;
1668     int depth() const;
1669     int channels() const;
1670     size_t step1() const;
1671     // returns step()/sizeof(_Tp)
1672     size_t stepT() const;
1673
1674     // overridden forms of Mat::zeros() etc. Data type is omitted, of course
1675     static MatExpr_Initializer zeros(int rows, int cols);
1676     static MatExpr_Initializer zeros(Size size);
1677     static MatExpr_Initializer ones(int rows, int cols);
1678     static MatExpr_Initializer ones(Size size);
1679     static MatExpr_Initializer eye(int rows, int cols);
1680     static MatExpr_Initializer eye(Size size);
1681
1682     // some more overriden methods
1683     Mat_ reshape(int _rows) const;
1684     Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright );
1685     Mat_ operator()( const Range& rowRange, const Range& colRange ) const;
1686     Mat_ operator()( const Rect& roi ) const;
1687
1688     // more convenient forms of row and element access operators 
1689     _Tp* operator [](int y);
1690     const _Tp* operator [](int y) const;
1691
1692     _Tp& operator ()(int row, int col);
1693     const _Tp& operator ()(int row, int col) const;
1694     _Tp& operator ()(Point pt);
1695     const _Tp& operator ()(Point pt) const;
1696
1697     // to support matrix expressions
1698     operator MatExpr_<Mat_, Mat_>() const;
1699     
1700     // conversion to vector.
1701     operator vector<_Tp>() const;
1702 };
1703 \end{lstlisting}
1704
1705 The class \texttt{Mat\_<\_Tp>} is a "thin" template wrapper on top of \texttt{Mat} class. It does not have any extra data fields, nor it or \texttt{Mat} have any virtual methods and thus references or pointers to these two classes can be freely converted one to another. But do it with care, e.g.:
1706
1707 \begin{lstlisting}
1708 // create 100x100 8-bit matrix
1709 Mat M(100,100,CV_8U);
1710 // this will compile fine. no any data conversion will be done.
1711 Mat_<float>& M1 = (Mat_<float>&)M;
1712 // the program will likely crash at the statement below
1713 M1(99,99) = 1.f;
1714 \end{lstlisting}
1715
1716 While \texttt{Mat} is sufficient in most cases, \texttt{Mat\_} can be more convenient if you use a lot of element access operations and if you know matrix type at compile time. Note that \texttt{Mat::at<\_Tp>(int y, int x)} and \texttt{Mat\_<\_Tp>::operator ()(int y, int x)} do absolutely the same and run at the same speed, but the latter is certainly shorter:
1717
1718 \begin{lstlisting}
1719 Mat_<double> M(20,20);
1720 for(int i = 0; i < M.rows; i++)
1721     for(int j = 0; j < M.cols; j++)
1722         M(i,j) = 1./(i+j+1);
1723 Mat E, V;
1724 eigen(M,E,V);
1725 cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0);
1726 \end{lstlisting}
1727
1728 \emph{How to use \texttt{Mat\_} for multi-channel images/matrices?}
1729
1730 This is simple - just pass \texttt{Vec} as \texttt{Mat\_} parameter:
1731 \begin{lstlisting}
1732 // allocate 320x240 color image and fill it with green (in RGB space)
1733 Mat_<Vec3b> img(240, 320, Vec3b(0,255,0));
1734 // now draw a diagonal white line
1735 for(int i = 0; i < 100; i++)
1736     img(i,i)=Vec3b(255,255,255);
1737 // and now scramble the 2nd (red) channel of each pixel
1738 for(int i = 0; i < img.rows; i++)
1739     for(int j = 0; j < img.cols; j++)
1740         img(i,j)[2] ^= (uchar)(i ^ j);
1741 \end{lstlisting}
1742
1743 \subsection{MatND}\label{MatND}
1744 n-dimensional dense array
1745
1746 \begin{lstlisting}
1747 class MatND
1748 {
1749 public:
1750     // default constructor
1751     MatND();
1752     // constructs array with specific size and data type
1753     MatND(int _ndims, const int* _sizes, int _type);
1754     // constructs array and fills it with the specified value
1755     MatND(int _ndims, const int* _sizes, int _type, const Scalar& _s);
1756     // copy constructor. only the header is copied.
1757     MatND(const MatND& m);
1758     // sub-array selection. only the header is copied
1759     MatND(const MatND& m, const Range* ranges);
1760     // converts old-style nd array to MatND; optionally, copies the data
1761     MatND(const CvMatND* m, bool copyData=false);
1762     ~MatND();
1763     MatND& operator = (const MatND& m);
1764
1765     // creates a complete copy of the matrix (all the data is copied)
1766     MatND clone() const;
1767     // sub-array selection; only the header is copied
1768     MatND operator()(const Range* ranges) const;
1769
1770     // copies the data to another matrix.
1771     // Calls m.create(this->size(), this->type()) prior to
1772     // copying the data
1773     void copyTo( MatND& m ) const;
1774     // copies only the selected elements to another matrix.
1775     void copyTo( MatND& m, const MatND& mask ) const;
1776     // converts data to the specified data type.
1777     // calls m.create(this->size(), rtype) prior to the conversion
1778     void convertTo( MatND& m, int rtype, double alpha=1, double beta=0 ) const;
1779
1780     // assigns "s" to each array element. 
1781     MatND& operator = (const Scalar& s);
1782     // assigns "s" to the selected elements of array
1783     // (or to all the elements if mask==MatND())
1784     MatND& setTo(const Scalar& s, const MatND& mask=MatND());
1785     // modifies geometry of array without copying the data
1786     MatND reshape(int _newcn, int _newndims=0, const int* _newsz=0) const;
1787
1788     // allocates a new buffer for the data unless the current one already
1789     // has the specified size and type.
1790     void create(int _ndims, const int* _sizes, int _type);
1791     // manually increment reference counter (use with care !!!)
1792     void addref();
1793     // decrements the reference counter. Dealloctes the data when
1794     // the reference counter reaches zero.
1795     void release();
1796
1797     // converts the matrix to 2D Mat or to the old-style CvMatND.
1798     // In either case the data is not copied.
1799     operator Mat() const;
1800     operator CvMatND() const;
1801     // returns true if the array data is stored continuously 
1802     bool isContinuous() const;
1803     // returns size of each element in bytes
1804     size_t elemSize() const;
1805     // returns size of each element channel in bytes
1806     size_t elemSize1() const;
1807     // returns OpenCV data type id (CV_8UC1, ... CV_64FC4,...)
1808     int type() const;
1809     // returns depth (CV_8U ... CV_64F)
1810     int depth() const;
1811     // returns the number of channels
1812     int channels() const;
1813     // step1() ~ step()/elemSize1()
1814     size_t step1(int i) const;
1815
1816     // return pointer to the element (versions for 1D, 2D, 3D and generic nD cases)
1817     uchar* ptr(int i0);
1818     const uchar* ptr(int i0) const;
1819     uchar* ptr(int i0, int i1);
1820     const uchar* ptr(int i0, int i1) const;
1821     uchar* ptr(int i0, int i1, int i2);
1822     const uchar* ptr(int i0, int i1, int i2) const;
1823     uchar* ptr(const int* idx);
1824     const uchar* ptr(const int* idx) const;
1825
1826     // convenient template methods for element access.
1827     // note that _Tp must match the actual matrix type -
1828     // the functions do not do any on-fly type conversion
1829     template<typename _Tp> _Tp& at(int i0);
1830     template<typename _Tp> const _Tp& at(int i0) const;
1831     template<typename _Tp> _Tp& at(int i0, int i1);
1832     template<typename _Tp> const _Tp& at(int i0, int i1) const;
1833     template<typename _Tp> _Tp& at(int i0, int i1, int i2);
1834     template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
1835     template<typename _Tp> _Tp& at(const int* idx);
1836     template<typename _Tp> const _Tp& at(const int* idx) const;
1837
1838     enum { MAGIC_VAL=0x42FE0000, AUTO_STEP=-1,
1839         CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, MAX_DIM=CV_MAX_DIM };
1840
1841     // combines data type, continuity flag, signature (magic value) 
1842     int flags;
1843     // the array dimensionality
1844     int dims;
1845
1846     // data reference counter
1847     int* refcount;
1848     // pointer to the data
1849     uchar* data;
1850     // and its actual beginning and end
1851     uchar* datastart;
1852     uchar* dataend;
1853
1854     // step and size for each dimension, MAX_DIM at max
1855     int size[MAX_DIM];
1856     size_t step[MAX_DIM];
1857 };
1858 \end{lstlisting}
1859
1860 The class \texttt{MatND} describes n-dimensional dense numerical single-channel or multi-channel array. This is a convenient representation for multi-dimensional histograms (when they are not very sparse, otherwise \texttt{SparseMat} will do better), voxel volumes, stacked motion fields etc. The data layout of matrix $M$ is defined by the array of \texttt{M.step[]}, so that the address of element $(i_0,...,i_{M.dims-1})$, where $0\leq i_k<M.size[k]$ is computed as:
1861 \[
1862 addr(M_{i_0,...,i_{M.dims-1}}) = M.data + M.step[0]*i_0 + M.step[1]*i_1 + ... + M.step[M.dims-1]*i_{M.dims-1}
1863 \]
1864 which is more general form of the respective formula for \cross{Mat}, wherein $\texttt{size[0]}\sim\texttt{rows}$,
1865 $\texttt{size[1]}\sim\texttt{cols}$, \texttt{step[0]} was simply called \texttt{step}, and \texttt{step[1]} was not stored at all but computed as \texttt{Mat::elemSize()}.
1866
1867 In other aspects \texttt{MatND} is also very similar to \texttt{Mat}, with the following limitations and differences:
1868 \begin{itemize}
1869     \item much less operations are implemented for \texttt{MatND}
1870     \item currently, algebraic expressions with \texttt{MatND}'s are not supported
1871     \item the \texttt{MatND} iterator is completely different from \texttt{Mat} and \texttt{Mat\_} iterators. The latter are per-element iterators, while the former is per-slice iterator, see below.
1872 \end{itemize}
1873
1874 Here is how you can use \texttt{MatND} to compute NxNxN histogram of color 8bpp image (i.e. each channel value ranges from 0..255 and we quantize it to 0..N-1):
1875
1876 \begin{lstlisting}
1877 void computeColorHist(const Mat& image, MatND& hist, int N)
1878 {
1879     const int histSize[] = {N, N, N};
1880     
1881     // make sure that the histogram has proper size and type
1882     hist.create(3, histSize, CV_32F);
1883     
1884     // and clear it
1885     hist = Scalar(0);
1886     
1887     // the loop below assumes that the image
1888     // is 8-bit 3-channel, so let's check it.
1889     CV_Assert(image.type() == CV_8UC3);
1890     MatConstIterator_<Vec3b> it = image.begin<Vec3b>(),
1891                              it_end = image.end<Vec3b>();    
1892     for( ; it != it_end; ++it )
1893     {
1894         const Vec3b& pix = *it;
1895         
1896         // we could have incremented the cells by 1.f/(image.rows*image.cols)
1897         // instead of 1.f to make the histogram normalized.
1898         hist.at<float>(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f;
1899     }
1900 }
1901 \end{lstlisting}
1902
1903 And here is how you can iterate through \texttt{MatND} elements:
1904
1905 \begin{lstlisting}
1906 void normalizeColorHist(MatND& hist)
1907 {
1908 #if 1    
1909     // intialize iterator (the style is different from STL).
1910     // after initialization the iterator will contain
1911     // the number of slices or planes
1912     // the iterator will go through
1913     MatNDIterator it(hist);
1914     double s = 0;
1915     // iterate through the matrix. on each iteration
1916     // it.planes[*] (of type Mat) will be set to the current plane.
1917     for(int p = 0; p < it.nplanes; p++, ++it)
1918         s += sum(it.planes[0])[0];
1919     it = MatNDIterator(hist);
1920     s = 1./s;
1921     for(int p = 0; p < it.nplanes; p++, ++it)
1922         it.planes[0] *= s;
1923 #elif 1
1924     // this is a shorter implementation of the above
1925     // using built-in operations on MatND
1926     double s = sum(hist)[0];
1927     hist.convertTo(hist, hist.type(), 1./s, 0);
1928 #else
1929     // and this is even shorter one
1930     // (assuming that the histogram elements are non-negative)
1931     normalize(hist, hist, 1, 0, NORM_L1);
1932 #endif
1933 }
1934 \end{lstlisting}
1935
1936 You can iterate though several matrices simultaneously as long as they have the same geometry (dimensionality and all the dimension sizes are the same), which is useful for binary and n-ary operations on such matrices. Just pass those matrices to \texttt{MatNDIterator}. Then, during the iteration \texttt{it.planes[0]}, \texttt{it.planes[1]}, ... will be the slices of the corresponding matrices.
1937
1938 \subsection{MatND\_}
1939 Template class for n-dimensional dense array derived from \cross{MatND}.
1940
1941 \begin{lstlisting}
1942 template<typename _Tp> class MatND_ : public MatND
1943 {
1944 public:
1945     typedef _Tp value_type;
1946     typedef typename DataType<_Tp>::channel_type channel_type;
1947
1948     // constructors, the same as in MatND, only the type is omitted
1949     MatND_();
1950     MatND_(int dims, const int* _sizes);
1951     MatND_(int dims, const int* _sizes, const _Tp& _s);
1952     MatND_(const MatND& m);
1953     MatND_(const MatND_& m);
1954     MatND_(const MatND_& m, const Range* ranges);
1955     MatND_(const CvMatND* m, bool copyData=false);
1956     MatND_& operator = (const MatND& m);
1957     MatND_& operator = (const MatND_& m);
1958     // different initialization function
1959     // where we take _Tp instead of Scalar
1960     MatND_& operator = (const _Tp& s);
1961
1962     // no special destructor is needed; use the one from MatND
1963
1964     void create(int dims, const int* _sizes);
1965     template<typename T2> operator MatND_<T2>() const;
1966     MatND_ clone() const;
1967     MatND_ operator()(const Range* ranges) const;
1968
1969     size_t elemSize() const;
1970     size_t elemSize1() const;
1971     int type() const;
1972     int depth() const;
1973     int channels() const;
1974     // step[i]/elemSize()
1975     size_t stepT(int i) const;
1976     size_t step1(int i) const;
1977
1978     // shorter alternatives for MatND::at<_Tp>.
1979     _Tp& operator ()(const int* idx);
1980     const _Tp& operator ()(const int* idx) const;
1981     _Tp& operator ()(int idx0);
1982     const _Tp& operator ()(int idx0) const;
1983     _Tp& operator ()(int idx0, int idx1);
1984     const _Tp& operator ()(int idx0, int idx1) const;
1985     _Tp& operator ()(int idx0, int idx1, int idx2);
1986     const _Tp& operator ()(int idx0, int idx1, int idx2) const;
1987     _Tp& operator ()(int idx0, int idx1, int idx2);
1988     const _Tp& operator ()(int idx0, int idx1, int idx2) const;
1989 };
1990 \end{lstlisting}
1991
1992 \texttt{MatND\_} relates to \texttt{MatND}  almost like \texttt{Mat\_} to \texttt{Mat} - it provides a bit more convenient element access operations and adds no extra members of virtual methods to the base class, thus references/pointers to \texttt{MatND\_} and \texttt{MatND} can be easily converted one to another, e.g.
1993
1994 \begin{lstlisting}
1995 // alternative variant of the above histogram accumulation loop
1996 ...
1997 CV_Assert(hist.type() == CV_32FC1);
1998 MatND_<float>& _hist = (MatND_<float>&)hist;
1999 for( ; it != it_end; ++it )
2000 {
2001     const Vec3b& pix = *it;
2002     _hist(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f;
2003 }
2004 ...
2005 \end{lstlisting}
2006
2007 \subsection{SparseMat}\label{SparseMat}
2008 Sparse n-dimensional array.
2009
2010 \begin{lstlisting}
2011 class SparseMat
2012 {
2013 public:
2014     typedef SparseMatIterator iterator;
2015     typedef SparseMatConstIterator const_iterator;
2016
2017     // internal structure - sparse matrix header
2018     struct Hdr
2019     {
2020         ...
2021     };
2022
2023     // sparse matrix node - element of a hash table
2024     struct Node
2025     {
2026         size_t hashval;
2027         size_t next;
2028         int idx[CV_MAX_DIM];
2029     };
2030
2031     ////////// constructors and destructor //////////
2032     // default constructor
2033     SparseMat();
2034     // creates matrix of the specified size and type
2035     SparseMat(int dims, const int* _sizes, int _type);
2036     // copy constructor
2037     SparseMat(const SparseMat& m);
2038     // converts dense 2d matrix to the sparse form,
2039     // if try1d is true and matrix is a single-column matrix (Nx1),
2040     // then the sparse matrix will be 1-dimensional.
2041     SparseMat(const Mat& m, bool try1d=false);
2042     // converts dense n-d matrix to the sparse form
2043     SparseMat(const MatND& m);
2044     // converts old-style sparse matrix to the new-style.
2045     // all the data is copied, so that "m" can be safely
2046     // deleted after the conversion
2047     SparseMat(const CvSparseMat* m);
2048     // destructor
2049     ~SparseMat();
2050     
2051     ///////// assignment operations /////////// 
2052     
2053     // this is O(1) operation; no data is copied
2054     SparseMat& operator = (const SparseMat& m);
2055     // (equivalent to the corresponding constructor with try1d=false)
2056     SparseMat& operator = (const Mat& m);
2057     SparseMat& operator = (const MatND& m);
2058
2059     // creates full copy of the matrix
2060     SparseMat clone() const;
2061     
2062     // copy all the data to the destination matrix.
2063     // the destination will be reallocated if needed.
2064     void copyTo( SparseMat& m ) const;
2065     // converts 1D or 2D sparse matrix to dense 2D matrix.
2066     // If the sparse matrix is 1D, then the result will
2067     // be a single-column matrix.
2068     void copyTo( Mat& m ) const;
2069     // converts arbitrary sparse matrix to dense matrix.
2070     // watch out the memory!
2071     void copyTo( MatND& m ) const;
2072     // multiplies all the matrix elements by the specified scalar
2073     void convertTo( SparseMat& m, int rtype, double alpha=1 ) const;
2074     // converts sparse matrix to dense matrix with optional type conversion and scaling.
2075     // When rtype=-1, the destination element type will be the same
2076     // as the sparse matrix element type.
2077     // Otherwise rtype will specify the depth and
2078     // the number of channels will remain the same is in the sparse matrix
2079     void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;
2080     void convertTo( MatND& m, int rtype, double alpha=1, double beta=0 ) const;
2081
2082     // not used now
2083     void assignTo( SparseMat& m, int type=-1 ) const;
2084
2085     // reallocates sparse matrix. If it was already of the proper size and type,
2086     // it is simply cleared with clear(), otherwise,
2087     // the old matrix is released (using release()) and the new one is allocated.
2088     void create(int dims, const int* _sizes, int _type);
2089     // sets all the matrix elements to 0, which means clearing the hash table.
2090     void clear();
2091     // manually increases reference counter to the header.
2092     void addref();
2093     // decreses the header reference counter, when it reaches 0,
2094     // the header and all the underlying data are deallocated.
2095     void release();
2096
2097     // converts sparse matrix to the old-style representation.
2098     // all the elements are copied.
2099     operator CvSparseMat*() const;
2100     // size of each element in bytes
2101     // (the matrix nodes will be bigger because of
2102     //  element indices and other SparseMat::Node elements).
2103     size_t elemSize() const;
2104     // elemSize()/channels()
2105     size_t elemSize1() const;
2106     
2107     // the same is in Mat and MatND
2108     int type() const;
2109     int depth() const;
2110     int channels() const;
2111     
2112     // returns the array of sizes and 0 if the matrix is not allocated
2113     const int* size() const;
2114     // returns i-th size (or 0)
2115     int size(int i) const;
2116     // returns the matrix dimensionality
2117     int dims() const;
2118     // returns the number of non-zero elements
2119     size_t nzcount() const;
2120     
2121     // compute element hash value from the element indices:
2122     // 1D case
2123     size_t hash(int i0) const;
2124     // 2D case
2125     size_t hash(int i0, int i1) const;
2126     // 3D case
2127     size_t hash(int i0, int i1, int i2) const;
2128     // n-D case
2129     size_t hash(const int* idx) const;
2130     
2131     // low-level element-acccess functions,
2132     // special variants for 1D, 2D, 3D cases and the generic one for n-D case.
2133     //
2134     // return pointer to the matrix element.
2135     //  if the element is there (it's non-zero), the pointer to it is returned
2136     //  if it's not there and createMissing=false, NULL pointer is returned
2137     //  if it's not there and createMissing=true, then the new element
2138     //    is created and initialized with 0. Pointer to it is returned
2139     //  If the optional hashval pointer is not NULL, the element hash value is
2140     //  not computed, but *hashval is taken instead.
2141     uchar* ptr(int i0, bool createMissing, size_t* hashval=0);
2142     uchar* ptr(int i0, int i1, bool createMissing, size_t* hashval=0);
2143     uchar* ptr(int i0, int i1, int i2, bool createMissing, size_t* hashval=0);
2144     uchar* ptr(const int* idx, bool createMissing, size_t* hashval=0);
2145
2146     // higher-level element access functions:
2147     // ref<_Tp>(i0,...[,hashval]) - equivalent to *(_Tp*)ptr(i0,...true[,hashval]).
2148     //    always return valid reference to the element.
2149     //    If it's did not exist, it is created.
2150     // find<_Tp>(i0,...[,hashval]) - equivalent to (_const Tp*)ptr(i0,...false[,hashval]).
2151     //    return pointer to the element or NULL pointer if the element is not there.
2152     // value<_Tp>(i0,...[,hashval]) - equivalent to
2153     //    { const _Tp* p = find<_Tp>(i0,...[,hashval]); return p ? *p : _Tp(); }
2154     //    that is, 0 is returned when the element is not there.
2155     // note that _Tp must match the actual matrix type -
2156     // the functions do not do any on-fly type conversion
2157     
2158     // 1D case
2159     template<typename _Tp> _Tp& ref(int i0, size_t* hashval=0);   
2160     template<typename _Tp> _Tp value(int i0, size_t* hashval=0) const;
2161     template<typename _Tp> const _Tp* find(int i0, size_t* hashval=0) const;
2162
2163     // 2D case
2164     template<typename _Tp> _Tp& ref(int i0, int i1, size_t* hashval=0);   
2165     template<typename _Tp> _Tp value(int i0, int i1, size_t* hashval=0) const;
2166     template<typename _Tp> const _Tp* find(int i0, int i1, size_t* hashval=0) const;
2167     
2168     // 3D case
2169     template<typename _Tp> _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
2170     template<typename _Tp> _Tp value(int i0, int i1, int i2, size_t* hashval=0) const;
2171     template<typename _Tp> const _Tp* find(int i0, int i1, int i2, size_t* hashval=0) const;
2172
2173     // n-D case
2174     template<typename _Tp> _Tp& ref(const int* idx, size_t* hashval=0);
2175     template<typename _Tp> _Tp value(const int* idx, size_t* hashval=0) const;
2176     template<typename _Tp> const _Tp* find(const int* idx, size_t* hashval=0) const;
2177
2178     // erase the specified matrix element.
2179     // When there is no such element, the methods do nothing
2180     void erase(int i0, int i1, size_t* hashval=0);
2181     void erase(int i0, int i1, int i2, size_t* hashval=0);
2182     void erase(const int* idx, size_t* hashval=0);
2183
2184     // return the matrix iterators,
2185     //   pointing to the first sparse matrix element,
2186     SparseMatIterator begin();
2187     SparseMatConstIterator begin() const;
2188     //   ... or to the point after the last sparse matrix element
2189     SparseMatIterator end();
2190     SparseMatConstIterator end() const;
2191     
2192     // and the template forms of the above methods.
2193     // _Tp must match the actual matrix type.
2194     template<typename _Tp> SparseMatIterator_<_Tp> begin();
2195     template<typename _Tp> SparseMatConstIterator_<_Tp> begin() const;
2196     template<typename _Tp> SparseMatIterator_<_Tp> end();
2197     template<typename _Tp> SparseMatConstIterator_<_Tp> end() const;
2198
2199     // return value stored in the sparse martix node
2200     template<typename _Tp> _Tp& value(Node* n);
2201     template<typename _Tp> const _Tp& value(const Node* n) const;
2202     
2203     ////////////// some internal-use methods ///////////////
2204     ...
2205
2206     // pointer to the sparse matrix header
2207     Hdr* hdr;
2208 };
2209 \end{lstlisting}
2210
2211 The class \texttt{SparseMat} represents multi-dimensional sparse numerical arrays. Such a sparse array can store elements of any type that \cross{Mat} and \cross{MatND} can store. "Sparse" means that only non-zero elements are stored (though, as a result of operations on a sparse matrix, some of its stored elements can actually become 0. It's up to the user to detect such elements and delete them using \texttt{SparseMat::erase}). The non-zero elements are stored in a hash table that grows when it's filled enough, so that the search time is O(1) in average (regardless of whether element is there or not). Elements can be accessed using the following methods:
2212
2213 \begin{enumerate}
2214     \item query operations (\texttt{SparseMat::ptr} and the higher-level \texttt{SparseMat::ref}, \texttt{SparseMat::value} and \texttt{SparseMat::find}), e.g.:
2215     \begin{lstlisting}
2216     const int dims = 5;
2217     int size[] = {10, 10, 10, 10, 10};
2218     SparseMat sparse_mat(dims, size, CV_32F);
2219     for(int i = 0; i < 1000; i++)
2220     {
2221         int idx[dims];
2222         for(int k = 0; k < dims; k++)
2223             idx[k] = rand()%sparse_mat.size(k);
2224         sparse_mat.ref<float>(idx) += 1.f;
2225     }
2226     \end{lstlisting}
2227     \item sparse matrix iterators. Like \cross{Mat} iterators and unlike \cross{MatND} iterators, the sparse matrix iterators are STL-style, that is, the iteration loop is familiar to C++ users:
2228     \begin{lstlisting}
2229     // prints elements of a sparse floating-point matrix
2230     // and the sum of elements.
2231     SparseMatConstIterator_<float>
2232         it = sparse_mat.begin<float>(),
2233         it_end = sparse_mat.end<float>();
2234     double s = 0;
2235     int dims = sparse_mat.dims();
2236     for(; it != it_end; ++it)
2237     {
2238         // print element indices and the element value
2239         const Node* n = it.node();
2240         printf("(")
2241         for(int i = 0; i < dims; i++)
2242             printf("%3d%c", n->idx[i], i < dims-1 ? ',' : ')');
2243         printf(": %f\n", *it);    
2244         s += *it;
2245     }
2246     printf("Element sum is %g\n", s);
2247     \end{lstlisting}
2248     If you run this loop, you will notice that elements are enumerated in no any logical order (lexicographical etc.), they come in the same order as they stored in the hash table, i.e. semi-randomly. You may collect pointers to the nodes and sort them to get the proper ordering. Note, however, that pointers to the nodes may become invalid when you add more elements to the matrix; this is because of possible buffer reallocation.
2249     \item a combination of the above 2 methods when you need to process 2 or more sparse matrices simultaneously, e.g. this is how you can compute unnormalized cross-correlation of the 2 floating-point sparse matrices:
2250     \begin{lstlisting}
2251     double cross_corr(const SparseMat& a, const SparseMat& b)
2252     {
2253         const SparseMat *_a = &a, *_b = &b;
2254         // if b contains less elements than a,
2255         // it's faster to iterate through b
2256         if(_a->nzcount() > _b->nzcount())
2257             std::swap(_a, _b);
2258         SparseMatConstIterator_<float> it = _a->begin<float>(),
2259                                        it_end = _a->end<float>();
2260         double ccorr = 0;
2261         for(; it != it_end; ++it)
2262         {
2263             // take the next element from the first matrix
2264             float avalue = *it;
2265             const Node* anode = it.node();
2266             // and try to find element with the same index in the second matrix.
2267             // since the hash value depends only on the element index,
2268             // we reuse hashvalue stored in the node
2269             float bvalue = _b->value<float>(anode->idx,&anode->hashval);
2270             ccorr += avalue*bvalue;
2271         }
2272         return ccorr;
2273     }
2274     \end{lstlisting}
2275 \end{enumerate}
2276
2277 \subsection{SparseMat\_}
2278 Template sparse n-dimensional array class derived from \cross{SparseMat}
2279
2280 \begin{lstlisting}
2281 template<typename _Tp> class SparseMat_ : public SparseMat
2282 {
2283 public:
2284     typedef SparseMatIterator_<_Tp> iterator;
2285     typedef SparseMatConstIterator_<_Tp> const_iterator;
2286
2287     // constructors;
2288     // the created matrix will have data type = DataType<_Tp>::type
2289     SparseMat_();
2290     SparseMat_(int dims, const int* _sizes);
2291     SparseMat_(const SparseMat& m);
2292     SparseMat_(const SparseMat_& m);
2293     SparseMat_(const Mat& m);
2294     SparseMat_(const MatND& m);
2295     SparseMat_(const CvSparseMat* m);
2296     // assignment operators; data type conversion is done when necessary
2297     SparseMat_& operator = (const SparseMat& m);
2298     SparseMat_& operator = (const SparseMat_& m);
2299     SparseMat_& operator = (const Mat& m);
2300     SparseMat_& operator = (const MatND& m);
2301
2302     // equivalent to the correspoding parent class methods
2303     SparseMat_ clone() const;
2304     void create(int dims, const int* _sizes);
2305     operator CvSparseMat*() const;
2306
2307     // overriden methods that do extra checks for the data type
2308     int type() const;
2309     int depth() const;
2310     int channels() const;
2311     
2312     // more convenient element access operations.
2313     // ref() is retained (but <_Tp> specification is not need anymore);
2314     // operator () is equivalent to SparseMat::value<_Tp>
2315     _Tp& ref(int i0, size_t* hashval=0);
2316     _Tp operator()(int i0, size_t* hashval=0) const;
2317     _Tp& ref(int i0, int i1, size_t* hashval=0);
2318     _Tp operator()(int i0, int i1, size_t* hashval=0) const;
2319     _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
2320     _Tp operator()(int i0, int i1, int i2, size_t* hashval=0) const;
2321     _Tp& ref(const int* idx, size_t* hashval=0);
2322     _Tp operator()(const int* idx, size_t* hashval=0) const;
2323
2324     // iterators
2325     SparseMatIterator_<_Tp> begin();
2326     SparseMatConstIterator_<_Tp> begin() const;
2327     SparseMatIterator_<_Tp> end();
2328     SparseMatConstIterator_<_Tp> end() const;
2329 };
2330 \end{lstlisting}
2331
2332 \texttt{SparseMat\_} is a thin wrapper on top of \cross{SparseMat}, made in the same way as \texttt{Mat\_} and \texttt{MatND\_}.
2333 It simplifies notation of some operations, and that's it.
2334 \begin{lstlisting}
2335 int sz[] = {10, 20, 30};
2336 SparseMat_<double> M(3, sz);
2337 ...
2338 M.ref(1, 2, 3) = M(4, 5, 6) + M(7, 8, 9);
2339 \end{lstlisting}
2340 \fi