*``CV_TERMCRIT_EPS`` - stop the algorithm after the achieved algorithm-dependent accuracy becomes lower than ``epsilon``.
*``CV_TERMCRIT_ITER+CV_TERMCRIT_EPS`` - stop the algorithm after ``max_iter`` iterations or when the achieved accuracy is lower than ``epsilon``, whichever comes the earliest.
Matrix elements are stored row by row. Element (i, j) (i - 0-based row index, j - 0-based column index) of a matrix can be retrieved or modified using ``CV_MAT_ELEM`` macro: ::
A ``CvMatND`` signature (``CV_MATND_MAGIC_VAL``) plus the type of elements. Type of the matrix elements can be retrieved using ``CV_MAT_TYPE`` macro: ::
int type = CV_MAT_TYPE(ndmatrix->type);
..ocv:member:: int dims
The number of array dimensions
..ocv:member:: int* refcount
Underlying data reference counter
..ocv:member:: union data
Pointers to the actual matrix data
* ptr - pointer to 8-bit unsigned elements
* s - pointer to 16-bit signed elements
* i - pointer to 32-bit signed elements
* fl - pointer to 32-bit floating-point elements
* db - pointer to 64-bit floating-point elements
..ocv:member:: array dim
Arrays of pairs (array size along the i-th dimension, distance between neighbor elements along i-th dimension): ::
A ``CvSparseMat`` signature (CV_SPARSE_MAT_MAGIC_VAL) plus the type of sparse matrix elements. Similarly to ``CvMat`` and ``CvMatND``, use ``CV_MAT_TYPE()`` to retrieve type of the elements.
Number of channels. Most OpenCV functions support 1-4 channels.
..ocv:member:: int alphaChannel
Ignored by OpenCV
..ocv:member:: int depth
Channel depth in bits + the optional sign bit ( ``IPL_DEPTH_SIGN`` ). The supported depths are:
*``IPL_DEPTH_8U`` - unsigned 8-bit integer. Equivalent to ``CV_8U`` in matrix types.
*``IPL_DEPTH_8S`` - signed 8-bit integer. Equivalent to ``CV_8S`` in matrix types.
*``IPL_DEPTH_16U`` - unsigned 16-bit integer. Equivalent to ``CV_16U`` in matrix types.
*``IPL_DEPTH_16S`` - signed 8-bit integer. Equivalent to ``CV_16S`` in matrix types.
*``IPL_DEPTH_32S`` - signed 32-bit integer. Equivalent to ``CV_32S`` in matrix types.
*``IPL_DEPTH_32F`` - single-precision floating-point number. Equivalent to ``CV_32F`` in matrix types.
*``IPL_DEPTH_64F`` - double-precision floating-point number. Equivalent to ``CV_64F`` in matrix types.
..ocv:member:: char[] colorModel
Ignored by OpenCV.
..ocv:member:: char[] channelSeq
Ignored by OpenCV
..ocv:member:: int dataOrder
0 = ``IPL_DATA_ORDER_PIXEL`` - interleaved color channels, 1 - separate color channels. :ocv:cfunc:`CreateImage` only creates images with interleaved channels. For example, the usual layout of a color image is: :math:`b_{00} g_{00} r_{00} b_{10} g_{10} r_{10} ...`
The ``IplImage`` is taken from the Intel Image Processing Library, in which the format is native. OpenCV only supports a subset of possible ``IplImage`` formats, as outlined in the parameter list above.
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.
CvArr
-----
..ocv:struct:: CvArr
This is the "metatype" used *only* as a function parameter. It denotes 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. In C++ interface the role of ``CvArr`` is played by ``InputArray`` and ``OutputArray``.
The function clears (sets to zero) a specific element of a dense array or deletes the element of a sparse array. If the sparse array element does not exists, the function does nothing.
CloneImage
----------
Makes a full copy of an image, including the header, data, and ROI.
Creates a full copy of a matrix and returns a pointer to the copy. Note that the matrix copy is compacted, that is, it will not have gaps between rows.
CloneMatND
----------
Creates full copy of a multi-dimensional array and returns a pointer to the copy.
The function has several different purposes, and thus has several different names. It copies one array to another with optional scaling, which is performed first, and/or optional type conversion, performed after:
If any of the passed arrays is of ``IplImage`` type, then its ROI and COI fields are used. Both arrays must have the same type, the same number of dimensions, and the same size. The function can also copy sparse arrays (mask is not supported in this case).
The function allocates image, matrix or multi-dimensional dense array data. Note that in the case of matrix types OpenCV allocation functions are used. In the case of IplImage they are used
unless ``CV_TURN_ON_IPL_COMPATIBILITY()`` has been called before. In the latter case IPL functions are used to allocate the data.
CreateImage
-----------
Creates an image header and allocates the image data.
..ocv:cfunction:: IplImage* cvCreateImage(CvSize size, int depth, int channels)
:param type:The type of the matrix elements in the form ``CV_<bit depth><S|U|F>C<number of channels>`` , where S=signed, U=unsigned, F=float. For example, CV _ 8UC1 means the elements are 8-bit unsigned and the there is 1 channel, and CV _ 32SC2 means the elements are 32-bit signed and there are 2 channels.
The function call is equivalent to the following code: ::
CvMat* mat = cvCreateMatHeader(rows, cols, type);
cvCreateData(mat);
CreateMatHeader
---------------
Creates a matrix header but does not allocate the matrix data.
..ocv:cfunction:: CvMat* cvCreateMatHeader( int rows, int cols, int type)
..ocv:pyoldfunction:: cv.CreateMatHeader(rows, cols, type) -> mat
The function allocates a new matrix header and returns a pointer to it. The matrix data can then be allocated using :ocv:cfunc:`CreateData` or set explicitly to user-allocated data via :ocv:cfunc:`SetData`.
:param type:Type of array elements, see :ocv:cfunc:`CreateMat`
The function allocates a header for a multi-dimensional dense array. The array data can further be allocated using :ocv:cfunc:`CreateData` or set explicitly to user-allocated data via :ocv:cfunc:`SetData`.
CreateSparseMat
---------------
Creates sparse array.
..ocv:cfunction:: CvSparseMat* cvCreateSparseMat(int dims, const int* sizes, int type)
The functions return a specific array element. In the case of a sparse array the functions return 0 if the requested node does not exist (no new node is created by the functions).
GetCol(s)
---------
Returns one of more array columns.
..ocv:cfunction:: CvMat* cvGetCol(const CvArr* arr, CvMat* submat, int col)
The functions return the header, corresponding to a specified column span of the input array. That is, no data is copied. Therefore, any modifications of the submatrix will affect the original array. If you need to copy the columns, use :ocv:cfunc:`CloneMat`. ``cvGetCol(arr, submat, col)`` is a shortcut for ``cvGetCols(arr, submat, col, col+1)``.
GetDiag
-------
Returns one of array diagonals.
..ocv:cfunction:: CvMat* cvGetDiag(const CvArr* arr, CvMat* submat, int diag=0)
:param submat:Pointer to the resulting sub-array header
:param diag:Index of the array diagonal. Zero value corresponds to the main diagonal, -1 corresponds to the diagonal above the main, 1 corresponds to the diagonal below the main, and so forth.
The function returns the array dimensionality and the array of dimension sizes. In the case of ``IplImage`` or `CvMat` it always returns 2 regardless of number of image/matrix rows. For example, the following code calculates total number of array elements: ::
The function returns type of the array elements. In the case of ``IplImage`` the type is converted to ``CvMat``-like representation. For example, if the image has been created as: ::
:param image_header:Pointer to ``IplImage`` structure used as a temporary buffer
The function returns the image header for the input array that can be a matrix (:ocv:struct:`CvMat`) or image (:ocv:struct:`IplImage`). In the case of an image the function simply returns the input pointer. In the case of ``CvMat`` it initializes an ``image_header`` structure with the parameters of the input matrix. Note that if we transform ``IplImage`` to ``CvMat`` using :ocv:cfunc:`GetMat` and then transform ``CvMat`` back to IplImage using this function, we will get different headers if the ROI is set in the original image.
..ocv:pyoldfunction:: cv.GetMat(arr, allowND=0) -> mat
:param arr:Input array
:param header:Pointer to :ocv:struct:`CvMat` structure used as a temporary buffer
:param coi:Optional output parameter for storing COI
:param allowND:If non-zero, the function accepts multi-dimensional dense arrays (CvMatND*) and returns 2D matrix (if CvMatND has two dimensions) or 1D matrix (when CvMatND has 1 dimension or more than 2 dimensions). The ``CvMatND`` array must be continuous.
The function returns a matrix header for the input array that can be a matrix - :ocv:struct:`CvMat`, an image - :ocv:struct:`IplImage`, or a multi-dimensional dense array - :ocv:struct:`CvMatND` (the third option is allowed only if ``allowND != 0``) . In the case of matrix the function simply returns the input pointer. In the case of ``IplImage*`` or ``CvMatND`` it initializes the ``header`` structure with parameters of the current image ROI and returns ``&header``. Because COI is not supported by ``CvMat``, it is returned separately.
The function provides an easy way to handle both types of arrays - ``IplImage`` and ``CvMat`` using the same code. Input array must have non-zero data pointer, otherwise the function will report an error.
..note:: If the input array is ``IplImage`` with planar data layout and COI set, the function returns the pointer to the selected plane and ``COI == 0``. This feature allows user to process ``IplImage`` structures with planar data layout, even though OpenCV does not support such images.
The function moves iterator to the next sparse matrix element and returns pointer to it. In the current version there is no any particular order of the elements, because they are stored in the hash table. The sample below demonstrates how to iterate through the sparse matrix: ::
// print all the non-zero sparse matrix elements and compute their sum
The function fills output variables with low-level information about the array data. All output parameters are optional, so some of the pointers may be set to ``NULL``. If the array is ``IplImage`` with ROI set, the parameters of ROI are returned.
The following example shows how to get access to array elements. It computes absolute values of the array elements ::
:param arr:Input array. Must have a single channel.
:param idx0:The first zero-based component of the element index
:param idx1:The second zero-based component of the element index
:param idx2:The third zero-based component of the element index
:param idx:Array of the element indices
Returns a specific element of a single-channel array. If the array has multiple channels, a runtime error is raised. Note that ``Get?D`` functions can be used safely for both single-channel and multiple-channel arrays though they are a bit slower.
In the case of a sparse array the functions return 0 if the requested node does not exist (no new node is created by the functions).
GetRow(s)
---------
Returns array row or row span.
..ocv:cfunction:: CvMat* cvGetRow(const CvArr* arr, CvMat* submat, int row)
:param delta_row:Index step in the row span. That is, the function extracts every ``delta_row`` -th row from ``start_row`` and up to (but not including) ``end_row`` .
The functions return the header, corresponding to a specified row/row span of the input array. ``cvGetRow(arr, submat, row)`` is a shortcut for ``cvGetRows(arr, submat, row, row+1)``.
The function returns number of rows (CvSize::height) and number of columns (CvSize::width) of the input matrix or image. In the case of image the size of ROI is returned.
GetSubRect
----------
Returns matrix header corresponding to the rectangular sub-array of input image or matrix.
:param submat:Pointer to the resultant sub-array header
:param rect:Zero-based coordinates of the rectangle of interest
The function returns header, corresponding to a specified rectangle of the input array. In other words, it allows the user to treat a rectangular part of input array as a stand-alone array. ROI is taken into account by the function so the sub-array of ROI is actually extracted.
DecRefData
----------
Decrements an array data reference counter.
..ocv:cfunction:: void cvDecRefData(CvArr* arr)
:param arr:Pointer to an array header
The function decrements the data reference counter in a :ocv:struct:`CvMat` or :ocv:struct:`CvMatND` if the reference counter pointer is not NULL. If the counter reaches zero, the data is deallocated. In the current implementation the reference counter is not NULL only if the data was allocated using the :ocv:cfunc:`CreateData` function. The counter will be NULL in other cases such as: external data was assigned to the header using :ocv:cfunc:`SetData`, header is part of a larger matrix or image, or the header was converted from an image or n-dimensional matrix header.
IncRefData
----------
Increments array data reference counter.
..ocv:cfunction:: int cvIncRefData(CvArr* arr)
:param arr:Array header
The function increments :ocv:struct:`CvMat` or :ocv:struct:`CvMatND` data reference counter and returns the new counter value if the reference counter pointer is not NULL, otherwise it returns zero.
InitImageHeader
---------------
Initializes an image header that was previously allocated.
..ocv:cfunction:: IplImage* cvInitImageHeader( IplImage* image, CvSize size, int depth, int channels, int origin=0, int align=4)
:param image:Image header to initialize
:param size:Image width and height
:param depth:Image depth (see :ocv:cfunc:`CreateImage` )
:param channels:Number of channels (see :ocv:cfunc:`CreateImage` )
:param origin:Top-left ``IPL_ORIGIN_TL`` or bottom-left ``IPL_ORIGIN_BL``
:param align:Alignment for image rows, typically 4 or 8 bytes
The returned ``IplImage*`` points to the initialized header.
InitMatHeader
-------------
Initializes a pre-allocated matrix header.
..ocv:cfunction:: CvMat* cvInitMatHeader( CvMat* mat, int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP)
:param mat:A pointer to the matrix header to be initialized
:param rows:Number of rows in the matrix
:param cols:Number of columns in the matrix
:param type:Type of the matrix elements, see :ocv:cfunc:`CreateMat` .
:param data:Optional: data pointer assigned to the matrix header
:param step:Optional: full row width in bytes of the assigned data. By default, the minimal possible step is used which assumes there are no gaps between subsequent rows of the matrix.
This function is often used to process raw data with OpenCV matrix functions. For example, the following code computes the matrix product of two matrices, stored as ordinary arrays: ::
The function initializes iterator of sparse array elements and returns pointer to the first element, or NULL if the array is empty.
Mat
---
Initializes matrix header (lightweight variant).
..ocv:cfunction:: CvMat cvMat( int rows, int cols, int type, void* data=NULL)
:param rows:Number of rows in the matrix
:param cols:Number of columns in the matrix
:param type:Type of the matrix elements - see :ocv:cfunc:`CreateMat`
:param data:Optional data pointer assigned to the matrix header
Initializes a matrix header and assigns data to it. The matrix is filled *row*-wise (the first ``cols`` elements of data form the first row of the matrix, etc.)
This function is a fast inline substitution for :ocv:cfunc:`InitMatHeader`. Namely, it is equivalent to: ::
:param create_node:Optional input parameter for sparse matrices. Non-zero value of the parameter means that the requested element is created if it does not exist already.
:param precalc_hashval:Optional input parameter for sparse matrices. If the pointer is not NULL, the function does not recalculate the node hash value, but takes it from the specified location. It is useful for speeding up pair-wise operations (TODO: provide an example)
The functions return a pointer to a specific array element. Number of array dimension should match to the number of indices passed to the function except for ``cvPtr1D`` function that can be used for sequential access to 1D, 2D or nD dense arrays.
The functions can be used for sparse arrays as well - if the requested node does not exist they create it and set it to zero.
All these as well as other functions accessing array elements (
but it does not use IPL functions by default (see the ``CV_TURN_ON_IPL_COMPATIBILITY`` macro).
ReleaseMat
----------
Deallocates a matrix.
..ocv:cfunction:: void cvReleaseMat(CvMat** mat)
:param mat:Double pointer to the matrix
The function decrements the matrix data reference counter and deallocates matrix header. If the data reference counter is 0, it also deallocates the data. ::
The function decrements the array data reference counter and releases the array header. If the reference counter reaches 0, it also deallocates the data. ::
:param new_rows:New number of rows. 'new_rows = 0' means that the number of rows remains unchanged unless it needs to be changed according to ``new_cn`` value.
The function initializes the CvMat header so that it points to the same data as the original array but has a different shape - different number of channels, different number of rows, or both.
The following example code creates one image buffer and two image headers, the first is for a 320x240x3 image and the second is for a 960x240x1 image: ::
:param new_sizes:Array of new dimension sizes. Only ``new_dims-1`` values are used, because the total number of elements must remain the same. Thus, if ``new_dims = 1``, ``new_sizes`` array is not used.
The function is an advanced version of :ocv:cfunc:`Reshape` that can work with multi-dimensional arrays as well (though it can work with ordinary images and matrices) and change the number of dimensions.
:param idx0:The first zero-based component of the element index
:param idx1:The second zero-based component of the element index
:param idx2:The third zero-based component of the element index
:param idx:Array of the element indices
:param value:The assigned value
The functions assign the new value to a particular array element. In the case of a sparse array the functions create the node if it does not exist yet.
SetData
-------
Assigns user data to the array header.
..ocv:cfunction:: void cvSetData(CvArr* arr, void* data, int step)
:param coi:The channel of interest. 0 - all channels are selected, 1 - first channel is selected, etc. Note that the channel indices become 1-based.
If the ROI is set to ``NULL`` and the coi is *not* 0, the ROI is allocated. Most OpenCV functions do *not* support the COI setting, so to process an individual image/matrix channel one may copy (via :ocv:cfunc:`Copy` or :ocv:cfunc:`Split`) the channel to a separate image/matrix, process it and then copy the result back (via :ocv:cfunc:`Copy` or :ocv:cfunc:`Merge`) if needed.
SetImageROI
-----------
Sets an image Region Of Interest (ROI) for a given rectangle.
If the original image ROI was ``NULL`` and the ``rect`` is not the whole image, the ROI structure is allocated.
Most OpenCV functions support the use of ROI and treat the image rectangle as a separate image. For example, all of the pixel coordinates are counted from the top-left (or bottom-left) corner of the ROI, not the original image.
SetReal?D
---------
Change a specific array element.
..ocv:cfunction:: void cvSetReal1D(CvArr* arr, int idx0, double value)
..ocv:cfunction:: void cvSetReal2D(CvArr* arr, int idx0, int idx1, double value)
..ocv:cfunction:: void cvSetReal3D(CvArr* arr, int idx0, int idx1, int idx2, double value)
:param idx0:The first zero-based component of the element index
:param idx1:The second zero-based component of the element index
:param idx2:The third zero-based component of the element index
:param idx:Array of the element indices
:param value:The assigned value
The functions assign a new value to a specific element of a single-channel array. If the array has multiple channels, a runtime error is raised. Note that the ``Set*D`` function can be used safely for both single-channel and multiple-channel arrays, though they are a bit slower.
In the case of a sparse array the functions create the node if it does not yet exist.
The function clears the array. In the case of dense arrays (CvMat, CvMatND or IplImage), cvZero(array) is equivalent to cvSet(array,cvScalarAll(0),0). In the case of sparse arrays all the elements are removed.
mGet
----
Returns the particular element of single-channel floating-point matrix.
..ocv:cfunction:: double cvmGet(const CvMat* mat, int row, int col)
The function is a fast replacement for :ocv:cfunc:`GetReal2D` in the case of single-channel floating-point matrices. It is faster because it is inline, it does fewer checks for array type and array element type, and it checks for the row and column ranges only in debug mode.
mSet
----
Sets a specific element of a single-channel floating-point matrix.
..ocv:cfunction:: void cvmSet(CvMat* mat, int row, int col, double value)
The function is a fast replacement for :ocv:cfunc:`SetReal2D` in the case of single-channel floating-point matrices. It is faster because it is inline, it does fewer checks for array type and array element type, and it checks for the row and column ranges only in debug mode.
SetIPLAllocators
----------------
Makes OpenCV use IPL functions for allocating IplImage and IplROI structures.
Normally, the function is not called directly. Instead, a simple macro ``CV_TURN_ON_IPL_COMPATIBILITY()`` is used that calls ``cvSetIPLAllocators`` and passes there pointers to IPL allocation functions. ::
The function initializes a random number generator and returns the state. The pointer to the state can be then passed to the :ocv:cfunc:`RandInt`, :ocv:cfunc:`RandReal` and :ocv:cfunc:`RandArr` functions. In the current implementation a multiply-with-carry generator is used.
:param rng:CvRNG state initialized by :ocv:cfunc:`RNG`
:param arr:The destination array
:param dist_type:Distribution type
***CV_RAND_UNI** uniform distribution
***CV_RAND_NORMAL** normal or Gaussian distribution
:param param1:The first parameter of the distribution. In the case of a uniform distribution it is the inclusive lower boundary of the random numbers range. In the case of a normal distribution it is the mean value of the random numbers.
:param param2:The second parameter of the distribution. In the case of a uniform distribution it is the exclusive upper boundary of the random numbers range. In the case of a normal distribution it is the standard deviation of the random numbers.
The function returns a uniformly-distributed random 32-bit unsigned integer and updates the RNG state. It is similar to the rand() function from the C runtime library, except that OpenCV functions always generates a 32-bit random number, regardless of the platform.
RandReal
--------
Returns a floating-point random number and updates RNG.
If the object supports the `array interface <http://docs.scipy.org/doc/numpy/reference/arrays.interface.html>`_
,
return a :ocv:struct:`CvMat` or :ocv:struct:`CvMatND`, depending on ``allowND`` flag:
* If ``allowND = False``, then the object's array must be either 2D or 3D. If it is 2D, then the returned CvMat has a single channel. If it is 3D, then the returned CvMat will have N channels, where N is the last dimension of the array. In this case, N cannot be greater than OpenCV's channel limit, ``CV_CN_MAX``.
* If``allowND = True``, then ``fromarray`` returns a single-channel :ocv:struct:`CvMatND` with the same shape as the original array.
For example, `NumPy <http://numpy.scipy.org/>`_ arrays support the array interface, so can be converted to OpenCV objects:
..note:: In the new Python wrappers (**cv2** module) the function is not needed, since cv2 can process Numpy arrays (and this is the only supported array type).