1 \section{Basic Structures}
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10 \label{CvPoint}\cvclass{CvPoint}
11 2D point with integer coordinates (usually zero-based).
15 typedef struct CvPoint
24 \cvarg{x}{x-coordinate}
25 \cvarg{y}{y-coordinate}
30 inline CvPoint cvPoint( int x, int y );
32 /* Conversion from CvPoint2D32f */
33 inline CvPoint cvPointFrom32f( CvPoint2D32f point );
36 2D point, represented as a tuple \texttt{(x, y)}, where x and y are integers.
39 \label{CvPoint2D32f}\cvclass{CvPoint2D32f}
40 2D point with floating-point coordinates
44 typedef struct CvPoint2D32f
53 \cvarg{x}{x-coordinate}
54 \cvarg{y}{y-coordinate}
59 inline CvPoint2D32f cvPoint2D32f( double x, double y );
61 /* Conversion from CvPoint */
62 inline CvPoint2D32f cvPointTo32f( CvPoint point );
65 2D point, represented as a tuple \texttt{(x, y)}, where x and y are floats.
69 \label{CvPoint3D32f}\cvclass{CvPoint3D32f}
70 3D point with floating-point coordinates
74 typedef struct CvPoint3D32f
84 \cvarg{x}{x-coordinate}
85 \cvarg{y}{y-coordinate}
86 \cvarg{z}{z-coordinate}
91 inline CvPoint3D32f cvPoint3D32f( double x, double y, double z );
94 3D point, represented as a tuple \texttt{(x, y, z)}, where x, y and z are floats.
97 \label{CvPoint2D64f}\cvclass{CvPoint2D64f}
98 2D point with double precision floating-point coordinates
102 typedef struct CvPoint2D64f
111 \cvarg{x}{x-coordinate}
112 \cvarg{y}{y-coordinate}
117 inline CvPoint2D64f cvPoint2D64f( double x, double y );
119 /* Conversion from CvPoint */
120 inline CvPoint2D64f cvPointTo64f( CvPoint point );
123 2D point, represented as a tuple \texttt{(x, y)}, where x and y are floats.
126 \label{CvPoint3D64f}\cvclass{CvPoint3D64f}
127 3D point with double precision floating-point coordinates
131 typedef struct CvPoint3D64f
141 \cvarg{x}{x-coordinate}
142 \cvarg{y}{y-coordinate}
143 \cvarg{z}{z-coordinate}
148 inline CvPoint3D64f cvPoint3D64f( double x, double y, double z );
151 3D point, represented as a tuple \texttt{(x, y, z)}, where x, y and z are floats.
154 \label{CvSize}\cvclass{CvSize}
155 Pixel-accurate size of a rectangle.
159 typedef struct CvSize
168 \cvarg{width}{Width of the rectangle}
169 \cvarg{height}{Height of the rectangle}
174 inline CvSize cvSize( int width, int height );
177 Size of a rectangle, represented as a tuple \texttt{(width, height)}, where width and height are integers.
180 \label{CvSize2D32f}\cvclass{CvSize2D32f}
181 Sub-pixel accurate size of a rectangle.
185 typedef struct CvSize2D32f
194 \cvarg{width}{Width of the rectangle}
195 \cvarg{height}{Height of the rectangle}
200 inline CvSize2D32f cvSize2D32f( double width, double height );
203 Size of a rectangle, represented as a tuple \texttt{(width, height)}, where width and height are floats.
206 \label{CvRect}\cvclass{CvRect}
207 Offset (usually the top-left corner) and size of a rectangle.
211 typedef struct CvRect
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}
230 inline CvRect cvRect( int x, int y, int width, int height );
233 Rectangle, represented as a tuple \texttt{(x, y, width, height)}, where all are integers.
236 \label{CvScalar}\cvclass{CvScalar}
237 A container for 1-,2-,3- or 4-tuples of doubles.
241 typedef struct CvScalar
250 initializes val[0] with val0, val[1] with val1, etc.
252 inline CvScalar cvScalar( double val0, double val1=0,
253 double val2=0, double val3=0 );
255 initializes all of val[0]...val[3] with val0123
257 inline CvScalar cvScalarAll( double val0123 );
260 initializes val[0] with val0, and all of val[1]...val[3] with zeros
262 inline CvScalar cvRealScalar( double val0 );
266 CvScalar is always represented as a 4-tuple.
270 >>> cv.Scalar(1, 2, 3, 4)
276 >>> cv.RGB(17, 110, 255)
277 (255.0, 110.0, 17.0, 0.0)
281 \label{CvTermCriteria}\cvclass{CvTermCriteria}
282 Termination criteria for iterative algorithms.
286 #define CV_TERMCRIT_ITER 1
287 #define CV_TERMCRIT_NUMBER CV_TERMCRIT_ITER
288 #define CV_TERMCRIT_EPS 2
290 typedef struct CvTermCriteria
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}
307 inline CvTermCriteria cvTermCriteria( int type, int max_iter, double epsilon );
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,
314 int default_max_iters );
317 Represented by a tuple \texttt{(type, max\_iter, epsilon)}.
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}
327 \label{CvMat}\cvclass{CvMat}
330 A multi-channel matrix.
370 \cvarg{type}{A CvMat signature (CV\_MAT\_MAGIC\_VAL) containing the type of elements and flags}
371 \cvarg{step}{Full row length in bytes}
372 \cvarg{refcount}{Underlying data reference counter}
373 \cvarg{data}{Pointers to the actual matrix data}
374 \cvarg{rows}{Number of rows}
375 \cvarg{cols}{Number of columns}
378 Matrices are stored row by row. All of the rows are aligned by 4 bytes.
380 A multi-channel 2D matrix. Created by
383 \cross{CreateMatHeader},
387 \cvarg{type}{A CvMat signature containing the type of elements and flags, int}
388 \cvarg{step}{Full row length in bytes, int}
389 \cvarg{rows}{Number of rows, int}
390 \cvarg{cols}{Number of columns, int}
391 \cvarg{tostring() -> str}{Returns the contents of the CvMat as a single string.}
398 \label{CvMatND}\cvclass{CvMatND}
399 Multi-dimensional dense multi-channel array.
403 typedef struct CvMatND
430 \cvarg{type}{A CvMatND signature (CV\_MATND\_MAGIC\_VAL), combining the type of elements and flags}
431 \cvarg{dims}{The number of array dimensions}
432 \cvarg{refcount}{Underlying data reference counter}
433 \cvarg{data}{Pointers to the actual matrix data}
434 \cvarg{dim}{For each dimension, the pair (number of elements, distance between elements in bytes)}
440 \cvarg{type}{A CvMatND signature combining the type of elements and flags, int}
441 \cvarg{tostring() -> str}{Returns the contents of the CvMatND as a single string.}
446 \label{CvSparseMat}\cvclass{CvSparseMat}
447 Multi-dimensional sparse multi-channel array.
450 typedef struct CvSparseMat
461 int size[CV_MAX_DIM];
467 \cvarg{type}{A CvSparseMat signature (CV\_SPARSE\_MAT\_MAGIC\_VAL), combining the type of elements and flags.}
468 \cvarg{dims}{Number of dimensions}
469 \cvarg{refcount}{Underlying reference counter. Not used.}
470 \cvarg{heap}{A pool of hash table nodes}
471 \cvarg{hashtable}{The hash table. Each entry is a list of nodes.}
472 \cvarg{hashsize}{Size of the hash table}
473 \cvarg{total}{Total number of sparse array nodes}
474 \cvarg{valoffset}{The value offset of the array nodes, in bytes}
475 \cvarg{idxoffset}{The index offset of the array nodes, in bytes}
476 \cvarg{size}{Array of dimension sizes}
481 \label{IplImage}\cvclass{IplImage}
486 typedef struct _IplImage
501 struct _IplImage *maskROI;
503 struct _IplTileInfo *tileInfo;
509 char *imageDataOrigin;
515 \cvarg{nSize}{\texttt{sizeof(IplImage)}}
516 \cvarg{ID}{Version, always equals 0}
517 \cvarg{nChannels}{Number of channels. Most OpenCV functions support 1-4 channels.}
518 \cvarg{alphaChannel}{Ignored by OpenCV}
519 \cvarg{depth}{Pixel depth in bits. The supported depths are:
521 \cvarg{IPL\_DEPTH\_8U}{Unsigned 8-bit integer}
522 \cvarg{IPL\_DEPTH\_8S}{Signed 8-bit integer}
523 \cvarg{IPL\_DEPTH\_16U}{Unsigned 16-bit integer}
524 \cvarg{IPL\_DEPTH\_16S}{Signed 16-bit integer}
525 \cvarg{IPL\_DEPTH\_32S}{Signed 32-bit integer}
526 \cvarg{IPL\_DEPTH\_32F}{Single-precision floating point}
527 \cvarg{IPL\_DEPTH\_64F}{Double-precision floating point}
529 \cvarg{colorModel}{Ignored by OpenCV. The OpenCV function \cross{CvtColor} requires the source and destination color spaces as parameters.}
530 \cvarg{channelSeq}{Ignored by OpenCV}
531 \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} ...$}
532 \cvarg{origin}{0 - top-left origin, 1 - bottom-left origin (Windows bitmap style)}
533 \cvarg{align}{Alignment of image rows (4 or 8). OpenCV ignores this and uses widthStep instead.}
534 \cvarg{width}{Image width in pixels}
535 \cvarg{height}{Image height in pixels}
536 \cvarg{roi}{Region Of Interest (ROI). If not NULL, only this image region will be processed.}
537 \cvarg{maskROI}{Must be NULL in OpenCV}
538 \cvarg{imageId}{Must be NULL in OpenCV}
539 \cvarg{tileInfo}{Must be NULL in OpenCV}
540 \cvarg{imageSize}{Image data size in bytes. For interleaved data, this equals $\texttt{image->height} \cdot \texttt{image->widthStep}$ }
541 \cvarg{imageData}{A pointer to the aligned image data}
542 \cvarg{widthStep}{The size of an aligned image row, in bytes}
543 \cvarg{BorderMode}{Border completion mode, ignored by OpenCV}
544 \cvarg{BorderConst}{Border completion mode, ignored by OpenCV}
545 \cvarg{imageDataOrigin}{A pointer to the origin of the image data (not necessarily aligned). This is used for image deallocation.}
548 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.
550 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.
555 The \cross{IplImage} object was inherited from the Intel Image Processing
556 Library, in which the format is native. OpenCV only supports a subset
557 of possible \cross{IplImage} formats.
560 \cvarg{nChannels}{Number of channels, int.}
561 \cvarg{width}{Image width in pixels}
562 \cvarg{height}{Image height in pixels}
563 \cvarg{depth}{Pixel depth in bits. The supported depths are:
565 \cvarg{IPL\_DEPTH\_8U}{Unsigned 8-bit integer}
566 \cvarg{IPL\_DEPTH\_8S}{Signed 8-bit integer}
567 \cvarg{IPL\_DEPTH\_16U}{Unsigned 16-bit integer}
568 \cvarg{IPL\_DEPTH\_16S}{Signed 16-bit integer}
569 \cvarg{IPL\_DEPTH\_32S}{Signed 32-bit integer}
570 \cvarg{IPL\_DEPTH\_32F}{Single-precision floating point}
571 \cvarg{IPL\_DEPTH\_64F}{Double-precision floating point}
573 \cvarg{origin}{0 - top-left origin, 1 - bottom-left origin (Windows bitmap style)}
574 \cvarg{tostring() -> str}{Returns the contents of the CvMatND as a single string.}
578 \label{CvArr}\cvclass{CvArr}
586 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.
590 \texttt{CvArr} is used \textit{only} as a function parameter to specify that the parameter can be:
592 \item{an \cross{IplImage}}
593 \item{a \cross{CvMat}}
594 \item{any other type that exports the \href{http://docs.scipy.org/doc/numpy/reference/arrays.interface.html}{array interface}}
600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 \subsection{DataType}\label{DataType}
608 Template "traits" class for other OpenCV primitive data types
611 template<typename _Tp> class DataType
613 // value_type is always a synonym for _Tp.
614 typedef _Tp value_type;
616 // intermediate type used for operations on _Tp.
617 // it is int for uchar, signed char, unsigned short, signed short and int,
618 // float for float, double for double, ...
619 typedef <...> work_type;
620 // in the case of multi-channel data it is the data type of each channel
621 typedef <...> channel_type;
625 depth = DataDepth<channel_type>::value,
628 // '1u', '4i', '3f', '2d' etc.
630 // CV_8UC3, CV_32FC2 ...
631 type = CV_MAKETYPE(depth, channels)
636 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 \verb*"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.
638 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:
641 template<> class DataType<uchar>
643 typedef uchar value_type;
644 typedef int work_type;
645 typedef uchar channel_type;
646 enum { channel_type = CV_8U, channels = 1, fmt='u', type = CV_8U };
649 template<typename _Tp> DataType<std::complex<_Tp> >
651 typedef std::complex<_Tp> value_type;
652 typedef std::complex<_Tp> work_type;
653 typedef _Tp channel_type;
654 // DataDepth is another helper trait class
655 enum { depth = DataDepth<_Tp>::value, channels=2,
656 fmt=(channels-1)*256+DataDepth<_Tp>::fmt,
657 type=CV_MAKETYPE(depth, channels) };
662 The main purpose of the classes is to convert compile-time type information to OpenCV-compatible data type identifier, for example:
665 // allocates 30x40 floating-point matrix
666 Mat A(30, 40, DataType<float>::type);
668 Mat B = Mat_<std::complex<double> >(3, 3);
669 // the statement below will print 6, 2 /* i.e. depth == CV_64F, channels == 2 */
670 cout << B.depth() << ", " << B.channels() << endl;
673 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.
676 Template class for 2D points
679 template<typename _Tp> class Point_
682 typedef _Tp value_type;
685 Point_(_Tp _x, _Tp _y);
686 Point_(const Point_& pt);
687 Point_(const CvPoint& pt);
688 Point_(const CvPoint2D32f& pt);
689 Point_(const Size_<_Tp>& sz);
690 Point_(const Vec<_Tp, 2>& v);
691 Point_& operator = (const Point_& pt);
692 template<typename _Tp2> operator Point_<_Tp2>() const;
693 operator CvPoint() const;
694 operator CvPoint2D32f() const;
695 operator Vec<_Tp, 2>() const;
697 // computes dot-product (this->x*pt.x + this->y*pt.y)
698 _Tp dot(const Point_& pt) const;
699 // computes dot-product using double-precision arithmetics
700 double ddot(const Point_& pt) const;
701 // returns true if the point is inside the rectangle "r".
702 bool inside(const Rect_<_Tp>& r) const;
708 The class represents a 2D point, specified by its coordinates $x$ and $y$.
709 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:
719 double value = norm(pt); // L2 norm
724 For user convenience, the following type aliases are defined:
726 typedef Point_<int> Point2i;
727 typedef Point2i Point;
728 typedef Point_<float> Point2f;
729 typedef Point_<double> Point2d;
732 Here is a short example:
734 Point2f a(0.3f, 0.f), b(0.f, 0.4f);
735 Point pt = (a + b)*10.f;
736 cout << pt.x << ", " << pt.y << endl;
739 \subsection{Point3\_}
741 Template class for 3D points
745 template<typename _Tp> class Point3_
748 typedef _Tp value_type;
751 Point3_(_Tp _x, _Tp _y, _Tp _z);
752 Point3_(const Point3_& pt);
753 explicit Point3_(const Point_<_Tp>& pt);
754 Point3_(const CvPoint3D32f& pt);
755 Point3_(const Vec<_Tp, 3>& v);
756 Point3_& operator = (const Point3_& pt);
757 template<typename _Tp2> operator Point3_<_Tp2>() const;
758 operator CvPoint3D32f() const;
759 operator Vec<_Tp, 3>() const;
761 _Tp dot(const Point3_& pt) const;
762 double ddot(const Point3_& pt) const;
768 The class represents a 3D point, specified by its coordinates $x$, $y$ and $z$.
769 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.
771 The following type aliases are available:
774 typedef Point3_<int> Point3i;
775 typedef Point3_<float> Point3f;
776 typedef Point3_<double> Point3d;
781 Template class for specfying image or rectangle size.
784 template<typename _Tp> class Size_
787 typedef _Tp value_type;
790 Size_(_Tp _width, _Tp _height);
791 Size_(const Size_& sz);
792 Size_(const CvSize& sz);
793 Size_(const CvSize2D32f& sz);
794 Size_(const Point_<_Tp>& pt);
795 Size_& operator = (const Size_& sz);
798 operator Size_<int>() const;
799 operator Size_<float>() const;
800 operator Size_<double>() const;
801 operator CvSize() const;
802 operator CvSize2D32f() const;
808 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.
810 OpenCV defines the following type aliases:
813 typedef Size_<int> Size2i;
815 typedef Size_<float> Size2f;
820 Template class for 2D rectangles
823 template<typename _Tp> class Rect_
826 typedef _Tp value_type;
829 Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
830 Rect_(const Rect_& r);
831 Rect_(const CvRect& r);
832 // (x, y) <- org, (width, height) <- sz
833 Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
834 // (x, y) <- min(pt1, pt2), (width, height) <- max(pt1, pt2) - (x, y)
835 Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
836 Rect_& operator = ( const Rect_& r );
837 // returns Point_<_Tp>(x, y)
838 Point_<_Tp> tl() const;
839 // returns Point_<_Tp>(x+width, y+height)
840 Point_<_Tp> br() const;
842 // returns Size_<_Tp>(width, height)
843 Size_<_Tp> size() const;
844 // returns width*height
847 operator Rect_<int>() const;
848 operator Rect_<float>() const;
849 operator Rect_<double>() const;
850 operator CvRect() const;
852 // x <= pt.x && pt.x < x + width &&
853 // y <= pt.y && pt.y < y + height ? true : false
854 bool contains(const Point_<_Tp>& pt) const;
856 _Tp x, y, width, height;
860 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.
862 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
864 x \leq pt.x < x+width,\\
865 y \leq pt.y < y+height
867 And virtually every loop over an image \cross{ROI} in OpenCV (where ROI is specified by \texttt{Rect\_<int>}) is implemented as:
869 for(int y = roi.y; y < roi.y + rect.height; y++)
870 for(int x = roi.x; x < roi.x + rect.width; x++)
876 In addition to the class members, the following operations on rectangles are implemented:
878 \item $\texttt{rect} = \texttt{rect} \pm \texttt{point}$ (shifting rectangle by a certain offset)
879 \item $\texttt{rect} = \texttt{rect} \pm \texttt{size}$ (expanding or shrinking rectangle by a certain amount)
880 \item \texttt{rect += point, rect -= point, rect += size, rect -= size} (augmenting operations)
881 \item \texttt{rect = rect1 \& rect2} (rectangle intersection)
882 \item \texttt{rect = rect1 | rect2} (minimum area rectangle containing \texttt{rect2} and \texttt{rect3})
883 \item \texttt{rect \&= rect1, rect |= rect1} (and the corresponding augmenting operations)
884 \item \texttt{rect == rect1, rect != rect1} (rectangle comparison)
887 Example. Here is how the partial ordering on rectangles can be established (rect1 $\subseteq$ rect2):
889 template<typename _Tp> inline bool
890 operator <= (const Rect_<_Tp>& r1, const Rect_<_Tp>& r2)
892 return (r1 & r2) == r1;
896 For user convenience, the following type alias is available:
898 typedef Rect_<int> Rect;
901 \subsection{RotatedRect}\label{RotatedRect}
902 Possibly rotated rectangle
910 RotatedRect(const Point2f& _center, const Size2f& _size, float _angle);
911 RotatedRect(const CvBox2D& box);
913 // returns minimal up-right rectangle that contains the rotated rectangle
914 Rect boundingRect() const;
915 // backward conversion to CvBox2D
916 operator CvBox2D() const;
918 // mass center of the rectangle
922 // rotation angle in degrees
927 The class \texttt{RotatedRect} replaces the old \cross{CvBox2D} and fully compatible with it.
929 \subsection{TermCriteria}\label{TermCriteria}
931 Termination criteria for iterative algorithms
937 enum { COUNT=1, MAX_ITER=COUNT, EPS=2 };
941 // type can be MAX_ITER, EPS or MAX_ITER+EPS.
942 // type = MAX_ITER means that only the number of iterations does matter;
943 // type = EPS means that only the required precision (epsilon) does matter
944 // (though, most algorithms put some limit on the number of iterations anyway)
945 // type = MAX_ITER + EPS means that algorithm stops when
946 // either the specified number of iterations is made,
947 // or when the specified accuracy is achieved - whatever happens first.
948 TermCriteria(int _type, int _maxCount, double _epsilon);
949 TermCriteria(const CvTermCriteria& criteria);
950 operator CvTermCriteria() const;
958 The class \texttt{TermCriteria} replaces the old \cross{CvTermCriteria} and fully compatible with it.
961 \subsection{Vec}\label{Vec}
962 Template class for short numerical vectors
965 template<typename _Tp, int cn> class Vec
968 typedef _Tp value_type;
969 enum { depth = DataDepth<_Tp>::value, channels = cn,
970 type = CV_MAKETYPE(depth, channels) };
972 // default constructor: all elements are set to 0
974 // constructors taking up to 10 first elements as parameters
977 Vec(_Tp v0, _Tp v1, _Tp v2);
979 Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4,
980 _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9);
981 Vec(const Vec<_Tp, cn>& v);
982 // constructs vector with all the components set to alpha.
983 static Vec all(_Tp alpha);
985 // two variants of dot-product
986 _Tp dot(const Vec& v) const;
987 double ddot(const Vec& v) const;
989 // cross-product; valid only when cn == 3.
990 Vec cross(const Vec& v) const;
992 // element type conversion
993 template<typename T2> operator Vec<T2, cn>() const;
995 // conversion to/from CvScalar (valid only when cn==4)
996 operator CvScalar() const;
999 _Tp operator [](int i) const;
1000 _Tp& operator[](int i);
1006 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:
1009 \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)
1010 \item \texttt{v1 == v2, v1 != v2}
1011 \item \texttt{double n = norm(v1); // $L_2$-norm}
1014 For user convenience, the following type aliases are introduced:
1016 typedef Vec<uchar, 2> Vec2b;
1017 typedef Vec<uchar, 3> Vec3b;
1018 typedef Vec<uchar, 4> Vec4b;
1020 typedef Vec<short, 2> Vec2s;
1021 typedef Vec<short, 3> Vec3s;
1022 typedef Vec<short, 4> Vec4s;
1024 typedef Vec<int, 2> Vec2i;
1025 typedef Vec<int, 3> Vec3i;
1026 typedef Vec<int, 4> Vec4i;
1028 typedef Vec<float, 2> Vec2f;
1029 typedef Vec<float, 3> Vec3f;
1030 typedef Vec<float, 4> Vec4f;
1031 typedef Vec<float, 6> Vec6f;
1033 typedef Vec<double, 2> Vec2d;
1034 typedef Vec<double, 3> Vec3d;
1035 typedef Vec<double, 4> Vec4d;
1036 typedef Vec<double, 6> Vec6d;
1039 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.
1041 \subsection{Scalar\_}
1045 template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>
1049 Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
1050 Scalar_(const CvScalar& s);
1052 static Scalar_<_Tp> all(_Tp v0);
1053 operator CvScalar() const;
1055 template<typename T2> operator Scalar_<T2>() const;
1057 Scalar_<_Tp> mul(const Scalar_<_Tp>& t, double scale=1 ) const;
1058 template<typename T2> void convertTo(T2* buf, int channels, int unroll_to=0) const;
1061 typedef Scalar_<double> Scalar;
1064 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.
1066 \subsection{Range}\label{Range}
1067 Specifies a continuous subsequence (a.k.a. slice) of a sequence.
1074 Range(int _start, int _end);
1075 Range(const CvSlice& slice);
1079 operator CvSlice() const;
1085 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)$.
1087 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:
1089 void my_function(..., const Range& r, ....)
1091 if(r == Range::all()) {
1092 // process all the data
1095 // process [r.start, r.end)
1100 \subsection{Ptr}\label{Ptr}
1102 A template class for smart reference-counting pointers
1105 template<typename _Tp> class Ptr
1108 // default constructor
1110 // constructor that wraps the object pointer
1112 // destructor: calls release()
1114 // copy constructor; increments ptr's reference counter
1115 Ptr(const Ptr& ptr);
1116 // assignment operator; decrements own reference counter
1117 // (with release()) and increments ptr's reference counter
1118 Ptr& operator = (const Ptr& ptr);
1119 // increments reference counter
1121 // decrements reference counter; when it becomes 0,
1122 // delete_obj() is called
1124 // user-specified custom object deletion operation.
1125 // by default, "delete obj;" is called
1127 // returns true if obj == 0;
1130 // provide access to the object fields and methods
1131 _Tp* operator -> ();
1132 const _Tp* operator -> () const;
1134 // return the underlying object pointer;
1135 // thanks to the methods, the Ptr<_Tp> can be
1136 // used instead of _Tp*
1138 operator const _Tp*() const;
1140 // the incapsulated object pointer
1142 // the associated reference counter
1147 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
1148 \href{http://en.wikipedia.org/wiki/C++0x}{C++0x} standard.
1150 By using this class you can get the following capabilities:
1153 \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.
1154 \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.
1155 \item automatic destruction, even for C structures. See the example below with \texttt{FILE*}.
1156 \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.
1159 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;}.
1160 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:
1163 template<> inline void Ptr<FILE>::delete_obj()
1165 fclose(obj); // no need to clear the pointer afterwards,
1166 // it is done externally.
1171 Ptr<FILE> f(fopen("myfile.txt", "r"));
1176 // the file will be closed automatically by the Ptr<FILE> destructor.
1179 \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.
1181 \subsection{Mat}\label{Mat}
1183 OpenCV C++ matrix class.
1191 // constructs matrix of the specified size and type
1192 // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
1193 Mat(int _rows, int _cols, int _type);
1194 // constucts matrix and fills it with the specified value _s.
1195 Mat(int _rows, int _cols, int _type, const Scalar& _s);
1196 Mat(Size _size, int _type);
1199 // constructor for matrix headers pointing to user-allocated data
1200 Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
1201 Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);
1202 // creates a matrix header for a part of the bigger matrix
1203 Mat(const Mat& m, const Range& rowRange, const Range& colRange);
1204 Mat(const Mat& m, const Rect& roi);
1205 // converts old-style CvMat to the new matrix; the data is not copied by default
1206 Mat(const CvMat* m, bool copyData=false);
1207 // converts old-style IplImage to the new matrix; the data is not copied by default
1208 Mat(const IplImage* img, bool copyData=false);
1209 // builds matrix from std::vector with or without copying the data
1210 template<typename _Tp> Mat(const vector<_Tp>& vec, bool copyData=false);
1211 // helper constructor to compile matrix expressions
1212 Mat(const MatExpr_Base& expr);
1213 // destructor - calls release()
1215 // assignment operators
1216 Mat& operator = (const Mat& m);
1217 Mat& operator = (const MatExpr_Base& expr);
1220 // returns a new matrix header for the specified row
1221 Mat row(int y) const;
1222 // returns a new matrix header for the specified column
1223 Mat col(int x) const;
1224 // ... for the specified row span
1225 Mat rowRange(int startrow, int endrow) const;
1226 Mat rowRange(const Range& r) const;
1227 // ... for the specified column span
1228 Mat colRange(int startcol, int endcol) const;
1229 Mat colRange(const Range& r) const;
1230 // ... for the specified diagonal
1231 // (d=0 - the main diagonal,
1232 // >0 - a diagonal from the lower half,
1233 // <0 - a diagonal from the upper half)
1234 Mat diag(int d=0) const;
1235 // constructs a square diagonal matrix which main diagonal is vector "d"
1236 static Mat diag(const Mat& d);
1238 // returns deep copy of the matrix, i.e. the data is copied
1240 // copies the matrix content to "m".
1241 // It calls m.create(this->size(), this->type()).
1242 void copyTo( Mat& m ) const;
1243 // copies those matrix elements to "m" that are marked with non-zero mask elements.
1244 void copyTo( Mat& m, const Mat& mask ) const;
1245 // converts matrix to another datatype with optional scalng. See cvConvertScale.
1246 void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;
1249 // sets every matrix element to s
1250 Mat& operator = (const Scalar& s);
1251 // sets some of the matrix elements to s, according to the mask
1252 Mat& setTo(const Scalar& s, const Mat& mask=Mat());
1253 // creates alternative matrix header for the same data, with different
1254 // number of channels and/or different number of rows. see cvReshape.
1255 Mat reshape(int _cn, int _rows=0) const;
1257 // matrix transposition by means of matrix expressions
1258 MatExpr_<...> t() const;
1259 // matrix inversion by means of matrix expressions
1260 MatExpr_<...> inv(int method=DECOMP_LU) const;
1261 // per-element matrix multiplication by means of matrix expressions
1262 MatExpr_<...> mul(const Mat& m, double scale=1) const;
1263 MatExpr_<...> mul(const MatExpr_<...>& m, double scale=1) const;
1265 // computes cross-product of 2 3D vectors
1266 Mat cross(const Mat& m) const;
1267 // computes dot-product
1268 double dot(const Mat& m) const;
1270 // Matlab-style matrix initialization. see the description
1271 static MatExpr_Initializer zeros(int rows, int cols, int type);
1272 static MatExpr_Initializer zeros(Size size, int type);
1273 static MatExpr_Initializer ones(int rows, int cols, int type);
1274 static MatExpr_Initializer ones(Size size, int type);
1275 static MatExpr_Initializer eye(int rows, int cols, int type);
1276 static MatExpr_Initializer eye(Size size, int type);
1278 // allocates new matrix data unless the matrix already has specified size and type.
1279 // previous data is unreferenced if needed.
1280 void create(int _rows, int _cols, int _type);
1281 void create(Size _size, int _type);
1282 // increases the reference counter; use with care to avoid memleaks
1284 // decreases reference counter;
1285 // deallocate the data when reference counter reaches 0.
1288 // locates matrix header within a parent matrix. See below
1289 void locateROI( Size& wholeSize, Point& ofs ) const;
1290 // moves/resizes the current matrix ROI inside the parent matrix.
1291 Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
1292 // extracts a rectangular sub-matrix
1293 // (this is a generalized form of row, rowRange etc.)
1294 Mat operator()( Range rowRange, Range colRange ) const;
1295 Mat operator()( const Rect& roi ) const;
1297 // converts header to CvMat; no data is copied
1298 operator CvMat() const;
1299 // converts header to IplImage; no data is copied
1300 operator IplImage() const;
1302 // returns true iff the matrix data is continuous
1303 // (i.e. when there are no gaps between successive rows).
1304 // similar to CV_IS_MAT_CONT(cvmat->type)
1305 bool isContinuous() const;
1306 // returns element size in bytes,
1307 // similar to CV_ELEM_SIZE(cvmat->type)
1308 size_t elemSize() const;
1309 // returns the size of element channel in bytes.
1310 size_t elemSize1() const;
1311 // returns element type, similar to CV_MAT_TYPE(cvmat->type)
1313 // returns element type, similar to CV_MAT_DEPTH(cvmat->type)
1315 // returns element type, similar to CV_MAT_CN(cvmat->type)
1316 int channels() const;
1317 // returns step/elemSize1()
1318 size_t step1() const;
1319 // returns matrix size:
1320 // width == number of columns, height == number of rows
1322 // returns true if matrix data is NULL
1325 // returns pointer to y-th row
1326 uchar* ptr(int y=0);
1327 const uchar* ptr(int y=0) const;
1329 // template version of the above method
1330 template<typename _Tp> _Tp* ptr(int y=0);
1331 template<typename _Tp> const _Tp* ptr(int y=0) const;
1333 // template methods for read-write or read-only element access.
1334 // note that _Tp must match the actual matrix type -
1335 // the functions do not do any on-fly type conversion
1336 template<typename _Tp> _Tp& at(int y, int x);
1337 template<typename _Tp> _Tp& at(Point pt);
1338 template<typename _Tp> const _Tp& at(int y, int x) const;
1339 template<typename _Tp> const _Tp& at(Point pt) const;
1341 // template methods for iteration over matrix elements.
1342 // the iterators take care of skipping gaps in the end of rows (if any)
1343 template<typename _Tp> MatIterator_<_Tp> begin();
1344 template<typename _Tp> MatIterator_<_Tp> end();
1345 template<typename _Tp> MatConstIterator_<_Tp> begin() const;
1346 template<typename _Tp> MatConstIterator_<_Tp> end() const;
1348 enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG };
1350 // includes several bit-fields:
1351 // * the magic signature
1352 // * continuity flag
1354 // * number of channels
1356 // the number of rows and columns
1358 // a distance between successive rows in bytes; includes the gap if any
1360 // pointer to the data
1363 // pointer to the reference counter;
1364 // when matrix points to user-allocated data, the pointer is NULL
1367 // helper fields used in locateROI and adjustROI
1373 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}.
1375 There are many different ways to create \texttt{Mat} object. Here are the some popular ones:
1377 \item using \texttt{create(nrows, ncols, type)} method or
1378 the similar constructor \texttt{Mat(nrows, ncols, type[, fill\_value])} constructor.
1379 A new matrix of the specified size and specifed type will be allocated.
1380 \texttt{type} has the same meaning as in \cvCppCross{cvCreateMat} method,
1381 e.g. \texttt{CV\_8UC1} means 8-bit single-channel matrix,
1382 \texttt{CV\_32FC2} means 2-channel (i.e. complex) floating-point matrix etc:
1385 // make 7x7 complex matrix filled with 1+3j.
1386 cv::Mat M(7,7,CV_32FC2,Scalar(1,3));
1387 // and now turn M to 100x60 15-channel 8-bit matrix.
1388 // The old content will be deallocated
1389 M.create(100,60,CV_8UC(15));
1392 As noted in the introduction of this chapter, \texttt{create()}
1393 will only allocate a new matrix when the current matrix dimensionality
1394 or type are different from the specified.
1396 \item by using a copy constructor or assignment operator, where on the right side it can
1397 be a matrix or expression, see below. Again, as noted in the introduction,
1398 matrix assignment is O(1) operation because it only copies the header
1399 and increases the reference counter. \texttt{Mat::clone()} method can be used to get a full
1400 (a.k.a. deep) copy of the matrix when you need it.
1402 \item by constructing a header for a part of another matrix. It can be a single row, single column,
1403 several rows, several columns, rectangular region in the matrix (called a minor in algebra) or
1404 a diagonal. Such operations are also O(1), because the new header will reference the same data.
1405 You can actually modify a part of the matrix using this feature, e.g.
1408 // add 5-th row, multiplied by 3 to the 3rd row
1409 M.row(3) = M.row(3) + M.row(5)*3;
1411 // now copy 7-th column to the 1-st column
1412 // M.col(1) = M.col(7); // this will not work
1414 M.col(7).copyTo(M1);
1416 // create new 320x240 image
1417 cv::Mat img(Size(320,240),CV_8UC3);
1419 cv::Mat roi(img, Rect(10,10,100,100));
1420 // fill the ROI with (0,255,0) (which is green in RGB space);
1421 // the original 320x240 image will be modified
1422 roi = Scalar(0,255,0);
1425 Thanks to the additional \texttt{datastart} and \texttt{dataend} members, it is possible to
1426 compute the relative sub-matrix position in the main \emph{"container"} matrix using \texttt{locateROI()}:
1429 Mat A = Mat::eye(10, 10, CV_32S);
1430 // extracts A columns, 1 (inclusive) to 3 (exclusive).
1431 Mat B = A(Range::all(), Range(1, 3));
1432 // extracts B rows, 5 (inclusive) to 9 (exclusive).
1433 // that is, C ~ A(Range(5, 9), Range(1, 3))
1434 Mat C = B(Range(5, 9), Range::all());
1435 Size size; Point ofs;
1436 C.locateROI(size, ofs);
1437 // size will be (width=10,height=10) and the ofs will be (x=1, y=5)
1440 As in the case of whole matrices, if you need a deep copy, use \texttt{clone()} method
1441 of the extracted sub-matrices.
1443 \item by making a header for user-allocated-data. It can be useful for
1445 \item processing "foreign" data using OpenCV (e.g. when you implement
1446 a DirectShow filter or a processing module for gstreamer etc.), e.g.
1449 void process_video_frame(const unsigned char* pixels,
1450 int width, int height, int step)
1452 cv::Mat img(height, width, CV_8UC3, pixels, step);
1453 cv::GaussianBlur(img, img, cv::Size(7,7), 1.5, 1.5);
1457 \item for quick initialization of small matrices and/or super-fast element access
1459 double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}};
1460 cv::Mat M = cv::Mat(3, 3, CV_64F, m).inv();
1464 partial yet very common cases of this "user-allocated data" case are conversions
1465 from \cross{CvMat} and \cross{IplImage} to \texttt{Mat}. For this purpose there are special constructors
1466 taking pointers to \texttt{CvMat} or \texttt{IplImage} and the optional
1467 flag indicating whether to copy the data or not.
1469 Backward conversion from \texttt{Mat} to \texttt{CvMat} or \texttt{IplImage} is provided via cast operators
1470 \texttt{Mat::operator CvMat() const} an \texttt{Mat::operator IplImage()}.
1471 The operators do \emph{not} copy the data.
1474 IplImage* img = cvLoadImage("greatwave.jpg", 1);
1475 Mat mtx(img); // convert IplImage* -> cv::Mat
1476 CvMat oldmat = mtx; // convert cv::Mat -> CvMat
1477 CV_Assert(oldmat.cols == img->width && oldmat.rows == img->height &&
1478 oldmat.data.ptr == (uchar*)img->imageData && oldmat.step == img->widthStep);
1481 \item by using MATLAB-style matrix initializers, \texttt{zeros(), ones(), eye()}, e.g.:
1484 // create a double-precision identity martix and add it to M.
1485 M += Mat::eye(M.rows, M.cols, CV_64F);
1488 \item by using comma-separated initializer:
1490 // create 3x3 double-precision identity matrix
1491 Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
1494 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.
1498 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).
1499 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()}.
1501 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.
1502 %\includegraphics[width=1.0\textwidth]{pics/roi.png}
1504 Given these parameters, address of the matrix element $M_{ij}$ is computed as following:
1507 \texttt{addr($M_{ij}$)=M.data + M.step*i + j*M.elemSize()}
1510 if you know the matrix element type, e.g. it is \texttt{float}, then you can use \texttt{at<>()} method:
1513 \texttt{addr($M_{ij}$)=\&M.at<float>(i,j)}
1515 (where \& is used to convert the reference returned by \texttt{at} to a pointer).
1516 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{[]}:
1519 // compute sum of positive matrix elements
1520 // (assuming that M is double-precision matrix)
1522 for(int i = 0; i < M.rows; i++)
1524 const double* Mi = M.ptr<double>(i);
1525 for(int j = 0; j < M.cols; j++)
1526 sum += std::max(Mi[j], 0.);
1530 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:
1533 // compute sum of positive matrix elements, optimized variant
1535 int cols = M.cols, rows = M.rows;
1536 if(M.isContinuous())
1541 for(int i = 0; i < rows; i++)
1543 const double* Mi = M.ptr<double>(i);
1544 for(int j = 0; j < cols; j++)
1545 sum += std::max(Mi[j], 0.);
1548 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.
1550 Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows:
1552 // compute sum of positive matrix elements, iterator-based variant
1554 MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>();
1555 for(; it != it_end; ++it)
1556 sum += std::max(*it, 0.);
1559 The matrix iterators are random-access iterators, so they can be passed to any STL algorithm, including \texttt{std::sort()}.
1561 \subsection{Matrix Expressions}
1563 This is a list of implemented matrix operations that can be combined in arbitrary complex expressions
1564 (here \emph{A}, \emph{B} stand for matrices (\texttt{Mat}), \emph{s} for a scalar (\texttt{Scalar}),
1565 \emph{$\alpha$} for a real-valued scalar (\texttt{double})):
1568 \item addition, subtraction, negation: $\texttt{A}\pm \texttt{B},\;\texttt{A}\pm \texttt{s},\;\texttt{s}\pm \texttt{A},\;-\texttt{A}$
1569 \item scaling: \texttt{A*$\alpha$, A/$\alpha$}
1570 \item per-element multiplication and division: \texttt{A.mul(B), A/B, $\alpha$/A}
1571 \item matrix multiplication: \texttt{A*B}
1572 \item transposition: \texttt{A.t() $\sim A^t$}
1573 \item matrix inversion and pseudo-inversion, solving linear systems and least-squares problems:
1574 \texttt{A.inv([method]) $\sim A^{-1}$}, \texttt{A.inv([method])*B $\sim X:\,AX=B$}
1575 \item comparison: $\texttt{A}\gtreqqless \texttt{B},\;\texttt{A} \ne \texttt{B},\;\texttt{A}\gtreqqless \alpha,\; \texttt{A} \ne \alpha$.
1576 The result of comparison is 8-bit single channel mask, which elements are set to 255
1577 (if the particular element or pair of elements satisfy the condition) and 0 otherwise.
1578 \item bitwise logical operations: \verb"A & B, A & s, A | B, A | s, A ^ B, A ^ s, ~A"
1579 \item element-wise minimum and maximum: \texttt{min(A, B), min(A, $\alpha$), max(A, B), max(A, $\alpha$)}
1580 \item element-wise absolute value: \texttt{abs(A)}
1581 \item cross-product, dot-product: \texttt{A.cross(B), A.dot(B)}
1582 \item any function of matrix or matrices and scalars that returns a matrix or a scalar, such as
1583 \cvCppCross{norm}, \cvCppCross{mean}, \cvCppCross{sum}, \cvCppCross{countNonZero}, \cvCppCross{trace},
1584 \cvCppCross{determinant}, \cvCppCross{repeat} etc.
1585 \item matrix initializers (\texttt{eye(), zeros(), ones()}), matrix comma-separated initializers,
1586 matrix constructors and operators that extract sub-matrices (see \cross{Mat} description).
1587 \item \verb"Mat_<destination_type>()" constructors to cast the result to the proper type.
1589 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.
1591 \subsection{Mat\_}\label{MatT}
1592 Template matrix class derived from \cross{Mat}
1595 template<typename _Tp> class Mat_ : public Mat
1598 typedef _Tp value_type;
1599 typedef typename DataType<_Tp>::channel_type channel_type;
1600 typedef MatIterator_<_Tp> iterator;
1601 typedef MatConstIterator_<_Tp> const_iterator;
1604 // equivalent to Mat(_rows, _cols, DataType<_Tp>::type)
1605 Mat_(int _rows, int _cols);
1606 // other forms of the above constructor
1607 Mat_(int _rows, int _cols, const _Tp& value);
1608 explicit Mat_(Size _size);
1609 Mat_(Size _size, const _Tp& value);
1610 // copy/conversion contructor. If m is of different type, it's converted
1613 Mat_(const Mat_& m);
1614 // construct a matrix on top of user-allocated data.
1615 // step is in bytes(!!!), regardless of the type
1616 Mat_(int _rows, int _cols, _Tp* _data, size_t _step=AUTO_STEP);
1618 Mat_(const Mat_& m, const Range& rowRange, const Range& colRange);
1619 Mat_(const Mat_& m, const Rect& roi);
1620 // to support complex matrix expressions
1621 Mat_(const MatExpr_Base& expr);
1622 // makes a matrix out of Vec or std::vector. The matrix will have a single column
1623 template<int n> explicit Mat_(const Vec<_Tp, n>& vec);
1624 Mat_(const vector<_Tp>& vec, bool copyData=false);
1626 Mat_& operator = (const Mat& m);
1627 Mat_& operator = (const Mat_& m);
1628 // set all the elements to s.
1629 Mat_& operator = (const _Tp& s);
1631 // iterators; they are smart enough to skip gaps in the end of rows
1634 const_iterator begin() const;
1635 const_iterator end() const;
1637 // equivalent to Mat::create(_rows, _cols, DataType<_Tp>::type)
1638 void create(int _rows, int _cols);
1639 void create(Size _size);
1641 Mat_ cross(const Mat_& m) const;
1642 // to support complex matrix expressions
1643 Mat_& operator = (const MatExpr_Base& expr);
1644 // data type conversion
1645 template<typename T2> operator Mat_<T2>() const;
1646 // overridden forms of Mat::row() etc.
1647 Mat_ row(int y) const;
1648 Mat_ col(int x) const;
1649 Mat_ diag(int d=0) const;
1652 // transposition, inversion, per-element multiplication
1653 MatExpr_<...> t() const;
1654 MatExpr_<...> inv(int method=DECOMP_LU) const;
1656 MatExpr_<...> mul(const Mat_& m, double scale=1) const;
1657 MatExpr_<...> mul(const MatExpr_<...>& m, double scale=1) const;
1659 // overridden forms of Mat::elemSize() etc.
1660 size_t elemSize() const;
1661 size_t elemSize1() const;
1664 int channels() const;
1665 size_t step1() const;
1666 // returns step()/sizeof(_Tp)
1667 size_t stepT() const;
1669 // overridden forms of Mat::zeros() etc. Data type is omitted, of course
1670 static MatExpr_Initializer zeros(int rows, int cols);
1671 static MatExpr_Initializer zeros(Size size);
1672 static MatExpr_Initializer ones(int rows, int cols);
1673 static MatExpr_Initializer ones(Size size);
1674 static MatExpr_Initializer eye(int rows, int cols);
1675 static MatExpr_Initializer eye(Size size);
1677 // some more overriden methods
1678 Mat_ reshape(int _rows) const;
1679 Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright );
1680 Mat_ operator()( const Range& rowRange, const Range& colRange ) const;
1681 Mat_ operator()( const Rect& roi ) const;
1683 // more convenient forms of row and element access operators
1684 _Tp* operator [](int y);
1685 const _Tp* operator [](int y) const;
1687 _Tp& operator ()(int row, int col);
1688 const _Tp& operator ()(int row, int col) const;
1689 _Tp& operator ()(Point pt);
1690 const _Tp& operator ()(Point pt) const;
1692 // to support matrix expressions
1693 operator MatExpr_<Mat_, Mat_>() const;
1695 // conversion to vector.
1696 operator vector<_Tp>() const;
1700 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.:
1703 // create 100x100 8-bit matrix
1704 Mat M(100,100,CV_8U);
1705 // this will compile fine. no any data conversion will be done.
1706 Mat_<float>& M1 = (Mat_<float>&)M;
1707 // the program will likely crash at the statement below
1711 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:
1714 Mat_<double> M(20,20);
1715 for(int i = 0; i < M.rows; i++)
1716 for(int j = 0; j < M.cols; j++)
1717 M(i,j) = 1./(i+j+1);
1720 cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0);
1723 \emph{How to use \texttt{Mat\_} for multi-channel images/matrices?}
1725 This is simple - just pass \texttt{Vec} as \texttt{Mat\_} parameter:
1727 // allocate 320x240 color image and fill it with green (in RGB space)
1728 Mat_<Vec3b> img(240, 320, Vec3b(0,255,0));
1729 // now draw a diagonal white line
1730 for(int i = 0; i < 100; i++)
1731 img(i,i)=Vec3b(255,255,255);
1732 // and now scramble the 2nd (red) channel of each pixel
1733 for(int i = 0; i < img.rows; i++)
1734 for(int j = 0; j < img.cols; j++)
1735 img(i,j)[2] ^= (uchar)(i ^ j);
1738 \subsection{MatND}\label{MatND}
1739 n-dimensional dense array
1745 // default constructor
1747 // constructs array with specific size and data type
1748 MatND(int _ndims, const int* _sizes, int _type);
1749 // constructs array and fills it with the specified value
1750 MatND(int _ndims, const int* _sizes, int _type, const Scalar& _s);
1751 // copy constructor. only the header is copied.
1752 MatND(const MatND& m);
1753 // sub-array selection. only the header is copied
1754 MatND(const MatND& m, const Range* ranges);
1755 // converts old-style nd array to MatND; optionally, copies the data
1756 MatND(const CvMatND* m, bool copyData=false);
1758 MatND& operator = (const MatND& m);
1760 // creates a complete copy of the matrix (all the data is copied)
1761 MatND clone() const;
1762 // sub-array selection; only the header is copied
1763 MatND operator()(const Range* ranges) const;
1765 // copies the data to another matrix.
1766 // Calls m.create(this->size(), this->type()) prior to
1768 void copyTo( MatND& m ) const;
1769 // copies only the selected elements to another matrix.
1770 void copyTo( MatND& m, const MatND& mask ) const;
1771 // converts data to the specified data type.
1772 // calls m.create(this->size(), rtype) prior to the conversion
1773 void convertTo( MatND& m, int rtype, double alpha=1, double beta=0 ) const;
1775 // assigns "s" to each array element.
1776 MatND& operator = (const Scalar& s);
1777 // assigns "s" to the selected elements of array
1778 // (or to all the elements if mask==MatND())
1779 MatND& setTo(const Scalar& s, const MatND& mask=MatND());
1780 // modifies geometry of array without copying the data
1781 MatND reshape(int _newcn, int _newndims=0, const int* _newsz=0) const;
1783 // allocates a new buffer for the data unless the current one already
1784 // has the specified size and type.
1785 void create(int _ndims, const int* _sizes, int _type);
1786 // manually increment reference counter (use with care !!!)
1788 // decrements the reference counter. Dealloctes the data when
1789 // the reference counter reaches zero.
1792 // converts the matrix to 2D Mat or to the old-style CvMatND.
1793 // In either case the data is not copied.
1794 operator Mat() const;
1795 operator CvMatND() const;
1796 // returns true if the array data is stored continuously
1797 bool isContinuous() const;
1798 // returns size of each element in bytes
1799 size_t elemSize() const;
1800 // returns size of each element channel in bytes
1801 size_t elemSize1() const;
1802 // returns OpenCV data type id (CV_8UC1, ... CV_64FC4,...)
1804 // returns depth (CV_8U ... CV_64F)
1806 // returns the number of channels
1807 int channels() const;
1808 // step1() ~ step()/elemSize1()
1809 size_t step1(int i) const;
1811 // return pointer to the element (versions for 1D, 2D, 3D and generic nD cases)
1813 const uchar* ptr(int i0) const;
1814 uchar* ptr(int i0, int i1);
1815 const uchar* ptr(int i0, int i1) const;
1816 uchar* ptr(int i0, int i1, int i2);
1817 const uchar* ptr(int i0, int i1, int i2) const;
1818 uchar* ptr(const int* idx);
1819 const uchar* ptr(const int* idx) const;
1821 // convenient template methods for element access.
1822 // note that _Tp must match the actual matrix type -
1823 // the functions do not do any on-fly type conversion
1824 template<typename _Tp> _Tp& at(int i0);
1825 template<typename _Tp> const _Tp& at(int i0) const;
1826 template<typename _Tp> _Tp& at(int i0, int i1);
1827 template<typename _Tp> const _Tp& at(int i0, int i1) const;
1828 template<typename _Tp> _Tp& at(int i0, int i1, int i2);
1829 template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
1830 template<typename _Tp> _Tp& at(const int* idx);
1831 template<typename _Tp> const _Tp& at(const int* idx) const;
1833 enum { MAGIC_VAL=0x42FE0000, AUTO_STEP=-1,
1834 CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, MAX_DIM=CV_MAX_DIM };
1836 // combines data type, continuity flag, signature (magic value)
1838 // the array dimensionality
1841 // data reference counter
1843 // pointer to the data
1845 // and its actual beginning and end
1849 // step and size for each dimension, MAX_DIM at max
1851 size_t step[MAX_DIM];
1855 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:
1857 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}
1859 which is more general form of the respective formula for \cross{Mat}, wherein $\texttt{size[0]}\sim\texttt{rows}$,
1860 $\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()}.
1862 In other aspects \texttt{MatND} is also very similar to \texttt{Mat}, with the following limitations and differences:
1864 \item much less operations are implemented for \texttt{MatND}
1865 \item currently, algebraic expressions with \texttt{MatND}'s are not supported
1866 \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.
1869 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):
1872 void computeColorHist(const Mat& image, MatND& hist, int N)
1874 const int histSize[] = {N, N, N};
1876 // make sure that the histogram has proper size and type
1877 hist.create(3, histSize, CV_32F);
1882 // the loop below assumes that the image
1883 // is 8-bit 3-channel, so let's check it.
1884 CV_Assert(image.type() == CV_8UC3);
1885 MatConstIterator_<Vec3b> it = image.begin<Vec3b>(),
1886 it_end = image.end<Vec3b>();
1887 for( ; it != it_end; ++it )
1889 const Vec3b& pix = *it;
1891 // we could have incremented the cells by 1.f/(image.rows*image.cols)
1892 // instead of 1.f to make the histogram normalized.
1893 hist.at<float>(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f;
1898 And here is how you can iterate through \texttt{MatND} elements:
1901 void normalizeColorHist(MatND& hist)
1904 // intialize iterator (the style is different from STL).
1905 // after initialization the iterator will contain
1906 // the number of slices or planes
1907 // the iterator will go through
1908 MatNDIterator it(hist);
1910 // iterate through the matrix. on each iteration
1911 // it.planes[*] (of type Mat) will be set to the current plane.
1912 for(int p = 0; p < it.nplanes; p++, ++it)
1913 s += sum(it.planes[0])[0];
1914 it = MatNDIterator(hist);
1916 for(int p = 0; p < it.nplanes; p++, ++it)
1919 // this is a shorter implementation of the above
1920 // using built-in operations on MatND
1921 double s = sum(hist)[0];
1922 hist.convertTo(hist, hist.type(), 1./s, 0);
1924 // and this is even shorter one
1925 // (assuming that the histogram elements are non-negative)
1926 normalize(hist, hist, 1, 0, NORM_L1);
1931 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.
1933 \subsection{MatND\_}
1934 Template class for n-dimensional dense array derived from \cross{MatND}.
1937 template<typename _Tp> class MatND_ : public MatND
1940 typedef _Tp value_type;
1941 typedef typename DataType<_Tp>::channel_type channel_type;
1943 // constructors, the same as in MatND, only the type is omitted
1945 MatND_(int dims, const int* _sizes);
1946 MatND_(int dims, const int* _sizes, const _Tp& _s);
1947 MatND_(const MatND& m);
1948 MatND_(const MatND_& m);
1949 MatND_(const MatND_& m, const Range* ranges);
1950 MatND_(const CvMatND* m, bool copyData=false);
1951 MatND_& operator = (const MatND& m);
1952 MatND_& operator = (const MatND_& m);
1953 // different initialization function
1954 // where we take _Tp instead of Scalar
1955 MatND_& operator = (const _Tp& s);
1957 // no special destructor is needed; use the one from MatND
1959 void create(int dims, const int* _sizes);
1960 template<typename T2> operator MatND_<T2>() const;
1961 MatND_ clone() const;
1962 MatND_ operator()(const Range* ranges) const;
1964 size_t elemSize() const;
1965 size_t elemSize1() const;
1968 int channels() const;
1969 // step[i]/elemSize()
1970 size_t stepT(int i) const;
1971 size_t step1(int i) const;
1973 // shorter alternatives for MatND::at<_Tp>.
1974 _Tp& operator ()(const int* idx);
1975 const _Tp& operator ()(const int* idx) const;
1976 _Tp& operator ()(int idx0);
1977 const _Tp& operator ()(int idx0) const;
1978 _Tp& operator ()(int idx0, int idx1);
1979 const _Tp& operator ()(int idx0, int idx1) const;
1980 _Tp& operator ()(int idx0, int idx1, int idx2);
1981 const _Tp& operator ()(int idx0, int idx1, int idx2) const;
1982 _Tp& operator ()(int idx0, int idx1, int idx2);
1983 const _Tp& operator ()(int idx0, int idx1, int idx2) const;
1987 \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.
1990 // alternative variant of the above histogram accumulation loop
1992 CV_Assert(hist.type() == CV_32FC1);
1993 MatND_<float>& _hist = (MatND_<float>&)hist;
1994 for( ; it != it_end; ++it )
1996 const Vec3b& pix = *it;
1997 _hist(pix[0]*N/256, pix[1]*N/256, pix[2]*N/256) += 1.f;
2002 \subsection{SparseMat}\label{SparseMat}
2003 Sparse n-dimensional array.
2009 typedef SparseMatIterator iterator;
2010 typedef SparseMatConstIterator const_iterator;
2012 // internal structure - sparse matrix header
2018 // sparse matrix node - element of a hash table
2023 int idx[CV_MAX_DIM];
2026 ////////// constructors and destructor //////////
2027 // default constructor
2029 // creates matrix of the specified size and type
2030 SparseMat(int dims, const int* _sizes, int _type);
2032 SparseMat(const SparseMat& m);
2033 // converts dense 2d matrix to the sparse form,
2034 // if try1d is true and matrix is a single-column matrix (Nx1),
2035 // then the sparse matrix will be 1-dimensional.
2036 SparseMat(const Mat& m, bool try1d=false);
2037 // converts dense n-d matrix to the sparse form
2038 SparseMat(const MatND& m);
2039 // converts old-style sparse matrix to the new-style.
2040 // all the data is copied, so that "m" can be safely
2041 // deleted after the conversion
2042 SparseMat(const CvSparseMat* m);
2046 ///////// assignment operations ///////////
2048 // this is O(1) operation; no data is copied
2049 SparseMat& operator = (const SparseMat& m);
2050 // (equivalent to the corresponding constructor with try1d=false)
2051 SparseMat& operator = (const Mat& m);
2052 SparseMat& operator = (const MatND& m);
2054 // creates full copy of the matrix
2055 SparseMat clone() const;
2057 // copy all the data to the destination matrix.
2058 // the destination will be reallocated if needed.
2059 void copyTo( SparseMat& m ) const;
2060 // converts 1D or 2D sparse matrix to dense 2D matrix.
2061 // If the sparse matrix is 1D, then the result will
2062 // be a single-column matrix.
2063 void copyTo( Mat& m ) const;
2064 // converts arbitrary sparse matrix to dense matrix.
2065 // watch out the memory!
2066 void copyTo( MatND& m ) const;
2067 // multiplies all the matrix elements by the specified scalar
2068 void convertTo( SparseMat& m, int rtype, double alpha=1 ) const;
2069 // converts sparse matrix to dense matrix with optional type conversion and scaling.
2070 // When rtype=-1, the destination element type will be the same
2071 // as the sparse matrix element type.
2072 // Otherwise rtype will specify the depth and
2073 // the number of channels will remain the same is in the sparse matrix
2074 void convertTo( Mat& m, int rtype, double alpha=1, double beta=0 ) const;
2075 void convertTo( MatND& m, int rtype, double alpha=1, double beta=0 ) const;
2078 void assignTo( SparseMat& m, int type=-1 ) const;
2080 // reallocates sparse matrix. If it was already of the proper size and type,
2081 // it is simply cleared with clear(), otherwise,
2082 // the old matrix is released (using release()) and the new one is allocated.
2083 void create(int dims, const int* _sizes, int _type);
2084 // sets all the matrix elements to 0, which means clearing the hash table.
2086 // manually increases reference counter to the header.
2088 // decreses the header reference counter, when it reaches 0,
2089 // the header and all the underlying data are deallocated.
2092 // converts sparse matrix to the old-style representation.
2093 // all the elements are copied.
2094 operator CvSparseMat*() const;
2095 // size of each element in bytes
2096 // (the matrix nodes will be bigger because of
2097 // element indices and other SparseMat::Node elements).
2098 size_t elemSize() const;
2099 // elemSize()/channels()
2100 size_t elemSize1() const;
2102 // the same is in Mat and MatND
2105 int channels() const;
2107 // returns the array of sizes and 0 if the matrix is not allocated
2108 const int* size() const;
2109 // returns i-th size (or 0)
2110 int size(int i) const;
2111 // returns the matrix dimensionality
2113 // returns the number of non-zero elements
2114 size_t nzcount() const;
2116 // compute element hash value from the element indices:
2118 size_t hash(int i0) const;
2120 size_t hash(int i0, int i1) const;
2122 size_t hash(int i0, int i1, int i2) const;
2124 size_t hash(const int* idx) const;
2126 // low-level element-acccess functions,
2127 // special variants for 1D, 2D, 3D cases and the generic one for n-D case.
2129 // return pointer to the matrix element.
2130 // if the element is there (it's non-zero), the pointer to it is returned
2131 // if it's not there and createMissing=false, NULL pointer is returned
2132 // if it's not there and createMissing=true, then the new element
2133 // is created and initialized with 0. Pointer to it is returned
2134 // If the optional hashval pointer is not NULL, the element hash value is
2135 // not computed, but *hashval is taken instead.
2136 uchar* ptr(int i0, bool createMissing, size_t* hashval=0);
2137 uchar* ptr(int i0, int i1, bool createMissing, size_t* hashval=0);
2138 uchar* ptr(int i0, int i1, int i2, bool createMissing, size_t* hashval=0);
2139 uchar* ptr(const int* idx, bool createMissing, size_t* hashval=0);
2141 // higher-level element access functions:
2142 // ref<_Tp>(i0,...[,hashval]) - equivalent to *(_Tp*)ptr(i0,...true[,hashval]).
2143 // always return valid reference to the element.
2144 // If it's did not exist, it is created.
2145 // find<_Tp>(i0,...[,hashval]) - equivalent to (_const Tp*)ptr(i0,...false[,hashval]).
2146 // return pointer to the element or NULL pointer if the element is not there.
2147 // value<_Tp>(i0,...[,hashval]) - equivalent to
2148 // { const _Tp* p = find<_Tp>(i0,...[,hashval]); return p ? *p : _Tp(); }
2149 // that is, 0 is returned when the element is not there.
2150 // note that _Tp must match the actual matrix type -
2151 // the functions do not do any on-fly type conversion
2154 template<typename _Tp> _Tp& ref(int i0, size_t* hashval=0);
2155 template<typename _Tp> _Tp value(int i0, size_t* hashval=0) const;
2156 template<typename _Tp> const _Tp* find(int i0, size_t* hashval=0) const;
2159 template<typename _Tp> _Tp& ref(int i0, int i1, size_t* hashval=0);
2160 template<typename _Tp> _Tp value(int i0, int i1, size_t* hashval=0) const;
2161 template<typename _Tp> const _Tp* find(int i0, int i1, size_t* hashval=0) const;
2164 template<typename _Tp> _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
2165 template<typename _Tp> _Tp value(int i0, int i1, int i2, size_t* hashval=0) const;
2166 template<typename _Tp> const _Tp* find(int i0, int i1, int i2, size_t* hashval=0) const;
2169 template<typename _Tp> _Tp& ref(const int* idx, size_t* hashval=0);
2170 template<typename _Tp> _Tp value(const int* idx, size_t* hashval=0) const;
2171 template<typename _Tp> const _Tp* find(const int* idx, size_t* hashval=0) const;
2173 // erase the specified matrix element.
2174 // When there is no such element, the methods do nothing
2175 void erase(int i0, int i1, size_t* hashval=0);
2176 void erase(int i0, int i1, int i2, size_t* hashval=0);
2177 void erase(const int* idx, size_t* hashval=0);
2179 // return the matrix iterators,
2180 // pointing to the first sparse matrix element,
2181 SparseMatIterator begin();
2182 SparseMatConstIterator begin() const;
2183 // ... or to the point after the last sparse matrix element
2184 SparseMatIterator end();
2185 SparseMatConstIterator end() const;
2187 // and the template forms of the above methods.
2188 // _Tp must match the actual matrix type.
2189 template<typename _Tp> SparseMatIterator_<_Tp> begin();
2190 template<typename _Tp> SparseMatConstIterator_<_Tp> begin() const;
2191 template<typename _Tp> SparseMatIterator_<_Tp> end();
2192 template<typename _Tp> SparseMatConstIterator_<_Tp> end() const;
2194 // return value stored in the sparse martix node
2195 template<typename _Tp> _Tp& value(Node* n);
2196 template<typename _Tp> const _Tp& value(const Node* n) const;
2198 ////////////// some internal-use methods ///////////////
2201 // pointer to the sparse matrix header
2206 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:
2209 \item query operations (\texttt{SparseMat::ptr} and the higher-level \texttt{SparseMat::ref}, \texttt{SparseMat::value} and \texttt{SparseMat::find}), e.g.:
2212 int size[] = {10, 10, 10, 10, 10};
2213 SparseMat sparse_mat(dims, size, CV_32F);
2214 for(int i = 0; i < 1000; i++)
2217 for(int k = 0; k < dims; k++)
2218 idx[k] = rand()%sparse_mat.size(k);
2219 sparse_mat.ref<float>(idx) += 1.f;
2222 \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:
2224 // prints elements of a sparse floating-point matrix
2225 // and the sum of elements.
2226 SparseMatConstIterator_<float>
2227 it = sparse_mat.begin<float>(),
2228 it_end = sparse_mat.end<float>();
2230 int dims = sparse_mat.dims();
2231 for(; it != it_end; ++it)
2233 // print element indices and the element value
2234 const Node* n = it.node();
2236 for(int i = 0; i < dims; i++)
2237 printf("%3d%c", n->idx[i], i < dims-1 ? ',' : ')');
2238 printf(": %f\n", *it);
2241 printf("Element sum is %g\n", s);
2243 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.
2244 \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:
2246 double cross_corr(const SparseMat& a, const SparseMat& b)
2248 const SparseMat *_a = &a, *_b = &b;
2249 // if b contains less elements than a,
2250 // it's faster to iterate through b
2251 if(_a->nzcount() > _b->nzcount())
2253 SparseMatConstIterator_<float> it = _a->begin<float>(),
2254 it_end = _a->end<float>();
2256 for(; it != it_end; ++it)
2258 // take the next element from the first matrix
2260 const Node* anode = it.node();
2261 // and try to find element with the same index in the second matrix.
2262 // since the hash value depends only on the element index,
2263 // we reuse hashvalue stored in the node
2264 float bvalue = _b->value<float>(anode->idx,&anode->hashval);
2265 ccorr += avalue*bvalue;
2272 \subsection{SparseMat\_}
2273 Template sparse n-dimensional array class derived from \cross{SparseMat}
2276 template<typename _Tp> class SparseMat_ : public SparseMat
2279 typedef SparseMatIterator_<_Tp> iterator;
2280 typedef SparseMatConstIterator_<_Tp> const_iterator;
2283 // the created matrix will have data type = DataType<_Tp>::type
2285 SparseMat_(int dims, const int* _sizes);
2286 SparseMat_(const SparseMat& m);
2287 SparseMat_(const SparseMat_& m);
2288 SparseMat_(const Mat& m);
2289 SparseMat_(const MatND& m);
2290 SparseMat_(const CvSparseMat* m);
2291 // assignment operators; data type conversion is done when necessary
2292 SparseMat_& operator = (const SparseMat& m);
2293 SparseMat_& operator = (const SparseMat_& m);
2294 SparseMat_& operator = (const Mat& m);
2295 SparseMat_& operator = (const MatND& m);
2297 // equivalent to the correspoding parent class methods
2298 SparseMat_ clone() const;
2299 void create(int dims, const int* _sizes);
2300 operator CvSparseMat*() const;
2302 // overriden methods that do extra checks for the data type
2305 int channels() const;
2307 // more convenient element access operations.
2308 // ref() is retained (but <_Tp> specification is not need anymore);
2309 // operator () is equivalent to SparseMat::value<_Tp>
2310 _Tp& ref(int i0, size_t* hashval=0);
2311 _Tp operator()(int i0, size_t* hashval=0) const;
2312 _Tp& ref(int i0, int i1, size_t* hashval=0);
2313 _Tp operator()(int i0, int i1, size_t* hashval=0) const;
2314 _Tp& ref(int i0, int i1, int i2, size_t* hashval=0);
2315 _Tp operator()(int i0, int i1, int i2, size_t* hashval=0) const;
2316 _Tp& ref(const int* idx, size_t* hashval=0);
2317 _Tp operator()(const int* idx, size_t* hashval=0) const;
2320 SparseMatIterator_<_Tp> begin();
2321 SparseMatConstIterator_<_Tp> begin() const;
2322 SparseMatIterator_<_Tp> end();
2323 SparseMatConstIterator_<_Tp> end() const;
2327 \texttt{SparseMat\_} is a thin wrapper on top of \cross{SparseMat}, made in the same way as \texttt{Mat\_} and \texttt{MatND\_}.
2328 It simplifies notation of some operations, and that's it.
2330 int sz[] = {10, 20, 30};
2331 SparseMat_<double> M(3, sz);
2333 M.ref(1, 2, 3) = M(4, 5, 6) + M(7, 8, 9);