The section describes the main data structures, used by the OpenCV 1.x API, and the basic functions to create and process the data structures.
CvPoint cvPoint
(int x, int y)¶constructs CvPoint
structure.
CvPoint cvPointFrom32f
(CvPoint2D32f point)¶converts CvPoint2D32f
to CvPoint
.
CvPoint
¶2D point with integer coordinates (usually zero-based).
param x: x-coordinate of the point. param y: y-coordinate of the point. param point: the point to convert.
See also
CvPoint2D32f cvPoint2D32f
(double x, double y)¶constructs CvPoint2D32f
structure.
CvPoint2D32f cvPointTo32f
(CvPoint point)¶converts CvPoint
to CvPoint2D32f
.
CvPoint2D32f
¶2D point with floating-point coordinates.
param x: floating-point x-coordinate of the point. param y: floating-point y-coordinate of the point. param point: the point to convert.
See also
CvPoint3D32f
¶3D point with floating-point coordinates
CvPoint3D32f cvPoint3D32f
(double x, double y, double z)¶constructs CvPoint3D32f
structure.
Parameters: |
|
---|
See also
CvPoint2D64f
¶2D point with double-precision floating-point coordinates.
CvPoint2D64f cvPoint2D64f
(double x, double y)¶constructs CvPoint2D64f
structure.
Parameters: |
|
---|
See also
CvPoint3D64f
¶3D point with double-precision floating-point coordinates.
CvPoint3D64f cvPoint3D64f
(double x, double y, double z)¶constructs CvPoint3D64f
structure.
Parameters: |
|
---|
See also
CvSize
¶Size of a rectangle or an image.
CvSize cvSize
(int width, int height)¶constructs CvSize
structure.
Parameters: |
|
---|
See also
CvSize2D32f
¶Sub-pixel accurate size of a rectangle.
CvSize2D32f cvSize2D32f
(double width, double height)¶constructs CvSize2D32f
structure.
Parameters: |
|
---|
See also
CvRect
¶Stores coordinates of a rectangle.
CvRect cvRect
(int x, int y, int width, int height)¶constructs CvRect
structure.
Parameters: |
|
---|
See also
CvBox2D
¶Stores coordinates of a rotated rectangle.
center
¶Center of the box
size
¶Box width and height
angle
¶Angle between the horizontal axis and the first side (i.e. length) in degrees
See also
See also
CvTermCriteria
¶Termination criteria for iterative algorithms.
type
¶type of the termination criteria, one of:
CV_TERMCRIT_ITER
- stop the algorithm aftermax_iter
iterations at maximum.CV_TERMCRIT_EPS
- stop the algorithm after the achieved algorithm-dependent accuracy becomes lower thanepsilon
.CV_TERMCRIT_ITER+CV_TERMCRIT_EPS
- stop the algorithm aftermax_iter
iterations or when the achieved accuracy is lower thanepsilon
, whichever comes the earliest.
max_iter
¶Maximum number of iterations
epsilon
¶Required accuracy
See also
CvMat
¶A multi-channel dense matrix.
type
CvMat
signature (CV_MAT_MAGIC_VAL
) plus type of the elements. Type of the matrix elements can be retrieved using CV_MAT_TYPE
macro:
int type = CV_MAT_TYPE(matrix->type);
For description of possible matrix elements, see Mat
.
step
¶Full row length in bytes
refcount
¶Underlying data reference counter
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
rows
¶Number of rows
cols
¶Number of columns
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:
uchar pixval = CV_MAT_ELEM(grayimg, uchar, i, j)
CV_MAT_ELEM(cameraMatrix, float, 0, 2) = image.width*0.5f;
To access multiple-channel matrices, you can use CV_MAT_ELEM(matrix, type, i, j*nchannels + channel_idx)
.
CvMat
is now obsolete; consider using Mat
instead.
CvMatND
¶Multi-dimensional dense multi-channel array.
type
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);
dims
¶The number of array dimensions
refcount
Underlying data reference counter
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
dim
¶Arrays of pairs (array size along the i-th dimension, distance between neighbor elements along i-th dimension):
for(int i = 0; i < ndmatrix->dims; i++)
printf("size[i] = %d, step[i] = %d\n", ndmatrix->dim[i].size, ndmatrix->dim[i].step);
CvMatND
is now obsolete; consider using Mat
instead.
CvSparseMat
¶Multi-dimensional sparse multi-channel array.
type
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.
dims
Number of dimensions
refcount
Underlying reference counter. Not used.
heap
¶A pool of hash table nodes
hashtable
¶The hash table. Each entry is a list of nodes.
hashsize
¶Size of the hash table
size
¶Array of dimension sizes
IplImage
¶IPL image header
nSize
¶sizeof(IplImage)
ID
¶Version, always equals 0
nChannels
¶Number of channels. Most OpenCV functions support 1-4 channels.
alphaChannel
¶Ignored by OpenCV
depth
¶Channel depth in bits + the optional sign bit ( IPL_DEPTH_SIGN
). The supported depths are:
IPL_DEPTH_8U
- unsigned 8-bit integer. Equivalent toCV_8U
in matrix types.IPL_DEPTH_8S
- signed 8-bit integer. Equivalent toCV_8S
in matrix types.IPL_DEPTH_16U
- unsigned 16-bit integer. Equivalent toCV_16U
in matrix types.IPL_DEPTH_16S
- signed 8-bit integer. Equivalent toCV_16S
in matrix types.IPL_DEPTH_32S
- signed 32-bit integer. Equivalent toCV_32S
in matrix types.IPL_DEPTH_32F
- single-precision floating-point number. Equivalent toCV_32F
in matrix types.IPL_DEPTH_64F
- double-precision floating-point number. Equivalent toCV_64F
in matrix types.
colorModel
¶Ignored by OpenCV.
channelSeq
¶Ignored by OpenCV
dataOrder
¶0 = IPL_DATA_ORDER_PIXEL
- interleaved color channels, 1 - separate color channels. CreateImage()
only creates images with interleaved channels. For example, the usual layout of a color image is:
origin
¶0 - top-left origin, 1 - bottom-left origin (Windows bitmap style)
align
¶Alignment of image rows (4 or 8). OpenCV ignores this and uses widthStep instead.
width
¶Image width in pixels
height
¶Image height in pixels
roi
¶Region Of Interest (ROI). If not NULL, only this image region will be processed.
maskROI
¶Must be NULL in OpenCV
imageId
¶Must be NULL in OpenCV
tileInfo
¶Must be NULL in OpenCV
imageSize
¶Image data size in bytes. For interleaved data, this equals
imageData
¶A pointer to the aligned image data. Do not assign imageData directly. Use SetData()
.
widthStep
¶The size of an aligned image row, in bytes.
BorderMode
¶Border completion mode, ignored by OpenCV
BorderConst
¶Constant border value, ignored by OpenCV
imageDataOrigin
¶A pointer to the origin of the image data (not necessarily aligned). This is used for image deallocation.
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
¶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
.
Clears a specific array element.
void cvClearND
(CvArr* arr, const int* idx)¶
cv.
ClearND
(arr, idx) → None¶Parameters: |
|
---|
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.
Makes a full copy of an image, including the header, data, and ROI.
IplImage* cvCloneImage
(const IplImage* image)¶
cv.
CloneImage
(image) → image¶Parameters: | image – The original image |
---|
Creates a full matrix copy.
CvMat* cvCloneMat
(const CvMat* mat)¶
cv.
CloneMat
(mat) → mat¶Parameters: | mat – Matrix to be copied |
---|
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.
Creates full copy of a multi-dimensional array and returns a pointer to the copy.
CvMatND* cvCloneMatND
(const CvMatND* mat)¶
cv.
CloneMatND
(mat) → matND¶Parameters: | mat – Input array |
---|
Creates full copy of sparse array.
CvSparseMat* cvCloneSparseMat
(const CvSparseMat* mat)¶Parameters: |
|
---|
The function creates a copy of the input array and returns pointer to the copy.
Converts one array to another with optional linear transformation.
void cvConvertScale
(const CvArr* src, CvArr* dst, double scale=1, double shift=0)¶
cv.
ConvertScale
(src, dst, scale=1.0, shift=0.0) → None¶
cv.
Convert
(src, dst) → None¶#define cvCvtScale cvConvertScale
#define cvScale cvConvertScale
#define cvConvert(src, dst ) cvConvertScale((src), (dst), 1, 0 )
Parameters: |
|
---|
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:
All the channels of multi-channel arrays are processed independently.
The type of conversion is done with rounding and saturation, that is if the result of scaling + conversion can not be represented exactly by a value of the destination array element type, it is set to the nearest representable value on the real axis.
Copies one array to another.
void cvCopy
(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)¶
cv.
Copy
(src, dst, mask=None) → None¶Parameters: |
|
---|
The function copies selected elements from an input array to an output array:
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).
Allocates array data
void cvCreateData
(CvArr* arr)¶
cv.
CreateData
(arr) → None¶Parameters: | arr – Array header |
---|
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.
Creates an image header and allocates the image data.
IplImage* cvCreateImage
(CvSize size, int depth, int channels)¶
cv.
CreateImage
(size, depth, channels) → image¶Parameters: |
---|
This function call is equivalent to the following code:
header = cvCreateImageHeader(size, depth, channels);
cvCreateData(header);
Creates an image header but does not allocate the image data.
IplImage* cvCreateImageHeader
(CvSize size, int depth, int channels)¶
cv.
CreateImageHeader
(size, depth, channels) → image¶Parameters: |
|
---|
Creates a matrix header and allocates the matrix data.
CvMat* cvCreateMat
(int rows, int cols, int type)¶
cv.
CreateMat
(rows, cols, type) → mat¶Parameters: |
|
---|
The function call is equivalent to the following code:
CvMat* mat = cvCreateMatHeader(rows, cols, type);
cvCreateData(mat);
Creates a matrix header but does not allocate the matrix data.
CvMat* cvCreateMatHeader
(int rows, int cols, int type)¶
cv.
CreateMatHeader
(rows, cols, type) → mat¶Parameters: |
|
---|
The function allocates a new matrix header and returns a pointer to it. The matrix data can then be allocated using CreateData()
or set explicitly to user-allocated data via SetData()
.
Creates the header and allocates the data for a multi-dimensional dense array.
CvMatND* cvCreateMatND
(int dims, const int* sizes, int type)¶
cv.
CreateMatND
(dims, type) → matND¶Parameters: |
|
---|
This function call is equivalent to the following code:
CvMatND* mat = cvCreateMatNDHeader(dims, sizes, type);
cvCreateData(mat);
Creates a new matrix header but does not allocate the matrix data.
CvMatND* cvCreateMatNDHeader
(int dims, const int* sizes, int type)¶
cv.
CreateMatNDHeader
(dims, type) → matND¶Parameters: |
|
---|
The function allocates a header for a multi-dimensional dense array. The array data can further be allocated using CreateData()
or set explicitly to user-allocated data via SetData()
.
Creates sparse array.
CvSparseMat* cvCreateSparseMat
(int dims, const int* sizes, int type)¶Parameters: |
|
---|
The function allocates a multi-dimensional sparse array. Initially the array contain no elements, that is
PtrND()
and other related functions will return 0 for every index.
Calculates the cross product of two 3D vectors.
void cvCrossProduct
(const CvArr* src1, const CvArr* src2, CvArr* dst)¶
cv.
CrossProduct
(src1, src2, dst) → None¶Parameters: |
|
---|
The function calculates the cross product of two 3D vectors:
or:
Calculates the dot product of two arrays in Euclidean metrics.
double cvDotProduct
(const CvArr* src1, const CvArr* src2)¶
cv.
DotProduct
(src1, src2) → float¶Parameters: |
|
---|
The function calculates and returns the Euclidean dot product of two arrays.
In the case of multiple channel arrays, the results for all channels are accumulated. In particular,
cvDotProduct(a,a)
where a
is a complex vector, will return .
The function can process multi-dimensional arrays, row by row, layer by layer, and so on.
CvScalar cvGet1D
(const CvArr* arr, int idx0)¶
CvScalar cvGet2D
(const CvArr* arr, int idx0, int idx1)¶
CvScalar cvGet3D
(const CvArr* arr, int idx0, int idx1, int idx2)¶
CvScalar cvGetND
(const CvArr* arr, const int* idx)¶
cv.
Get1D
(arr, idx) → scalar¶
cv.
Get2D
(arr, idx0, idx1) → scalar¶
cv.
Get3D
(arr, idx0, idx1, idx2) → scalar¶
cv.
GetND
(arr, indices) → scalar¶Return a specific array element.
Parameters: |
|
---|
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).
Returns one of more array columns.
CvMat* cvGetCol
(const CvArr* arr, CvMat* submat, int col)¶
CvMat* cvGetCols
(const CvArr* arr, CvMat* submat, int start_col, int end_col)¶
cv.
GetCol
(arr, col) → submat¶
cv.
GetCols
(arr, startCol, endCol) → submat¶Parameters: |
|
---|
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 CloneMat()
. cvGetCol(arr, submat, col)
is a shortcut for cvGetCols(arr, submat, col, col+1)
.
Returns one of array diagonals.
CvMat* cvGetDiag
(const CvArr* arr, CvMat* submat, int diag=0)¶
cv.
GetDiag
(arr, diag=0) → submat¶Parameters: |
|
---|
The function returns the header, corresponding to a specified diagonal of the input array.
Return number of array dimensions
int cvGetDims
(const CvArr* arr, int* sizes=NULL)¶
cv.
GetDims
(arr) -> (dim1, dim2, ...)¶Parameters: |
|
---|
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:
int sizes[CV_MAX_DIM];
int i, total = 1;
int dims = cvGetDims(arr, size);
for(i = 0; i < dims; i++ )
total *= sizes[i];
Returns array size along the specified dimension.
int cvGetDimSize
(const CvArr* arr, int index)¶Parameters: |
|
---|
Returns type of array elements.
int cvGetElemType
(const CvArr* arr)¶
cv.
GetElemType
(arr) → int¶Parameters: | arr – Input array |
---|
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:
IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3);
The code cvGetElemType(img)
will return CV_8UC3
.
Returns image header for arbitrary array.
IplImage* cvGetImage
(const CvArr* arr, IplImage* image_header)¶
cv.
GetImage
(arr) → iplimage¶Parameters: |
|
---|
The function returns the image header for the input array that can be a matrix (CvMat
) or image (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 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.
Returns the index of the channel of interest.
int cvGetImageCOI
(const IplImage* image)¶
cv.
GetImageCOI
(image) → int¶Parameters: | image – A pointer to the image header |
---|
Returns the channel of interest of in an IplImage. Returned values correspond to the coi
in
SetImageCOI()
.
Returns the image ROI.
CvRect cvGetImageROI
(const IplImage* image)¶
cv.
GetImageROI
(image) → CvRect¶Parameters: | image – A pointer to the image header |
---|
If there is no ROI set, cvRect(0,0,image->width,image->height)
is returned.
Returns matrix header for arbitrary array.
CvMat* cvGetMat
(const CvArr* arr, CvMat* header, int* coi=NULL, int allowND=0)¶
cv.
GetMat
(arr, allowND=0) → mat¶Parameters: |
|
---|
The function returns a matrix header for the input array that can be a matrix - CvMat
, an image - IplImage
, or a multi-dimensional dense array - 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.
See also
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.
Returns the next sparse matrix element
CvSparseNode* cvGetNextSparseNode
(CvSparseMatIterator* mat_iterator)¶Parameters: |
|
---|
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
double sum = 0;
int i, dims = cvGetDims(sparsemat);
CvSparseMatIterator it;
CvSparseNode* node = cvInitSparseMatIterator(sparsemat, &it);
for(; node != 0; node = cvGetNextSparseNode(&it))
{
/* get pointer to the element indices */
int* idx = CV_NODE_IDX(array, node);
/* get value of the element (assume that the type is CV_32FC1) */
float val = *(float*)CV_NODE_VAL(array, node);
printf("M");
for(i = 0; i < dims; i++ )
printf("[%d]", idx[i]);
printf("=%g\n", val);
sum += val;
}
printf("nTotal sum = %g\n", sum);
Retrieves low-level information about the array.
void cvGetRawData
(const CvArr* arr, uchar** data, int* step=NULL, CvSize* roi_size=NULL )¶Parameters: |
|
---|
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
float* data;
int step;
CvSize size;
cvGetRawData(array, (uchar**)&data, &step, &size);
step /= sizeof(data[0]);
for(int y = 0; y < size.height; y++, data += step )
for(int x = 0; x < size.width; x++ )
data[x] = (float)fabs(data[x]);
Return a specific element of single-channel 1D, 2D, 3D or nD array.
double cvGetReal1D
(const CvArr* arr, int idx0)¶
double cvGetReal2D
(const CvArr* arr, int idx0, int idx1)¶
double cvGetReal3D
(const CvArr* arr, int idx0, int idx1, int idx2)¶
double cvGetRealND
(const CvArr* arr, const int* idx)¶
cv.
GetReal1D
(arr, idx0) → float¶
cv.
GetReal2D
(arr, idx0, idx1) → float¶
cv.
GetReal3D
(arr, idx0, idx1, idx2) → float¶
cv.
GetRealND
(arr, idx) → float¶Parameters: |
|
---|
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).
Returns array row or row span.
CvMat* cvGetRow
(const CvArr* arr, CvMat* submat, int row)¶
CvMat* cvGetRows
(const CvArr* arr, CvMat* submat, int start_row, int end_row, int delta_row=1 )¶
cv.
GetRow
(arr, row) → submat¶
cv.
GetRows
(arr, startRow, endRow, deltaRow=1) → submat¶Parameters: |
|
---|
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)
.
Returns size of matrix or image ROI.
CvSize cvGetSize
(const CvArr* arr)¶
cv.
GetSize
(arr)-> (width, height)¶Parameters: | arr – array header |
---|
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.
Returns matrix header corresponding to the rectangular sub-array of input image or matrix.
CvMat* cvGetSubRect
(const CvArr* arr, CvMat* submat, CvRect rect)¶
cv.
GetSubRect
(arr, rect) → submat¶Parameters: |
|
---|
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.
Decrements an array data reference counter.
void cvDecRefData
(CvArr* arr)¶Parameters: |
|
---|
The function decrements the data reference counter in a CvMat
or 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 CreateData()
function. The counter will be NULL in other cases such as: external data was assigned to the header using SetData()
, header is part of a larger matrix or image, or the header was converted from an image or n-dimensional matrix header.
Increments array data reference counter.
int cvIncRefData
(CvArr* arr)¶Parameters: |
|
---|
The function increments CvMat
or CvMatND
data reference counter and returns the new counter value if the reference counter pointer is not NULL, otherwise it returns zero.
Initializes an image header that was previously allocated.
IplImage* cvInitImageHeader
(IplImage* image, CvSize size, int depth, int channels, int origin=0, int align=4)¶Parameters: |
|
---|
The returned IplImage*
points to the initialized header.
Initializes a pre-allocated matrix header.
CvMat* cvInitMatHeader
(CvMat* mat, int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP)¶Parameters: |
|
---|
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:
double a[] = { 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 };
double b[] = { 1, 5, 9,
2, 6, 10,
3, 7, 11,
4, 8, 12 };
double c[9];
CvMat Ma, Mb, Mc ;
cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);
cvInitMatHeader(&Mb, 4, 3, CV_64FC1, b);
cvInitMatHeader(&Mc, 3, 3, CV_64FC1, c);
cvMatMulAdd(&Ma, &Mb, 0, &Mc);
// the c array now contains the product of a (3x4) and b (4x3)
Initializes a pre-allocated multi-dimensional array header.
CvMatND* cvInitMatNDHeader
(CvMatND* mat, int dims, const int* sizes, int type, void* data=NULL)¶Parameters: |
|
---|
Initializes sparse array elements iterator.
CvSparseNode* cvInitSparseMatIterator
(const CvSparseMat* mat, CvSparseMatIterator* mat_iterator)¶Parameters: |
|
---|
The function initializes iterator of sparse array elements and returns pointer to the first element, or NULL if the array is empty.
Initializes matrix header (lightweight variant).
CvMat cvMat
(int rows, int cols, int type, void* data=NULL)¶Parameters: |
|
---|
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 InitMatHeader()
. Namely, it is equivalent to:
CvMat mat;
cvInitMatHeader(&mat, rows, cols, type, data, CV_AUTOSTEP);
Return pointer to a particular array element.
uchar* cvPtr1D
(const CvArr* arr, int idx0, int* type=NULL)¶
uchar* cvPtr2D
(const CvArr* arr, int idx0, int idx1, int* type=NULL)¶
uchar* cvPtr3D
(const CvArr* arr, int idx0, int idx1, int idx2, int* type=NULL)¶
uchar* cvPtrND
(const CvArr* arr, const int* idx, int* type=NULL, int create_node=1, unsigned int* precalc_hashval=NULL )¶Parameters: |
|
---|
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 (
GetND()
,
GetRealND()
,
Set()
,
SetND()
,
SetRealND()
) raise an error in case if the element index is out of range.
Releases array data.
void cvReleaseData
(CvArr* arr)¶Parameters: |
|
---|
The function releases the array data. In the case of
CvMat
or
CvMatND
it simply calls cvDecRefData(), that is the function can not deallocate external data. See also the note to
CreateData()
.
Deallocates the image header and the image data.
void cvReleaseImage
(IplImage** image)¶Parameters: |
|
---|
This call is a shortened form of
if(*image )
{
cvReleaseData(*image);
cvReleaseImageHeader(image);
}
Deallocates an image header.
void cvReleaseImageHeader
(IplImage** image)¶Parameters: |
|
---|
This call is an analogue of
if(image )
{
iplDeallocate(*image, IPL_IMAGE_HEADER | IPL_IMAGE_ROI);
*image = 0;
}
but it does not use IPL functions by default (see the CV_TURN_ON_IPL_COMPATIBILITY
macro).
Deallocates a matrix.
void cvReleaseMat
(CvMat** mat)¶Parameters: |
|
---|
The function decrements the matrix data reference counter and deallocates matrix header. If the data reference counter is 0, it also deallocates the data.
if(*mat )
cvDecRefData(*mat);
cvFree((void**)mat);
Deallocates a multi-dimensional array.
void cvReleaseMatND
(CvMatND** mat)¶Parameters: |
|
---|
The function decrements the array data reference counter and releases the array header. If the reference counter reaches 0, it also deallocates the data.
if(*mat )
cvDecRefData(*mat);
cvFree((void**)mat);
Deallocates sparse array.
void cvReleaseSparseMat
(CvSparseMat** mat)¶Parameters: |
|
---|
The function releases the sparse array and clears the array pointer upon exit.
Resets the image ROI to include the entire image and releases the ROI structure.
void cvResetImageROI
(IplImage* image)¶
cv.
ResetImageROI
(image) → None¶Parameters: | image – A pointer to the image header |
---|
This produces a similar result to the following, but in addition it releases the ROI structure.
cvSetImageROI(image, cvRect(0, 0, image->width, image->height ));
cvSetImageCOI(image, 0);
Changes shape of matrix/image without copying data.
CvMat* cvReshape
(const CvArr* arr, CvMat* header, int new_cn, int new_rows=0 )¶
cv.
Reshape
(arr, newCn, newRows=0) → mat¶Parameters: |
|
---|
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:
IplImage* color_img = cvCreateImage(cvSize(320,240), IPL_DEPTH_8U, 3);
CvMat gray_mat_hdr;
IplImage gray_img_hdr, *gray_img;
cvReshape(color_img, &gray_mat_hdr, 1);
gray_img = cvGetImage(&gray_mat_hdr, &gray_img_hdr);
And the next example converts a 3x3 matrix to a single 1x9 vector:
CvMat* mat = cvCreateMat(3, 3, CV_32F);
CvMat row_header, *row;
row = cvReshape(mat, &row_header, 0, 1);
Changes the shape of a multi-dimensional array without copying the data.
CvArr* cvReshapeMatND
(const CvArr* arr, int sizeof_header, CvArr* header, int new_cn, int new_dims, int* new_sizes)¶
cv.
ReshapeMatND
(arr, newCn, newDims) → mat¶Parameters: |
|
---|
The function is an advanced version of 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.
Below are the two samples from the
Reshape()
description rewritten using
ReshapeMatND()
:
IplImage* color_img = cvCreateImage(cvSize(320,240), IPL_DEPTH_8U, 3);
IplImage gray_img_hdr, *gray_img;
gray_img = (IplImage*)cvReshapeMatND(color_img, sizeof(gray_img_hdr), &gray_img_hdr, 1, 0, 0);
...
/* second example is modified to convert 2x2x2 array to 8x1 vector */
int size[] = { 2, 2, 2 };
CvMatND* mat = cvCreateMatND(3, size, CV_32F);
CvMat row_header, *row;
row = (CvMat*)cvReshapeMatND(mat, sizeof(row_header), &row_header, 0, 1, 0);
In C, the header file for this function includes a convenient macro cvReshapeND
that does away with the sizeof_header
parameter. So, the lines containing the call to cvReshapeMatND
in the examples may be replaced as follow:
gray_img = (IplImage*)cvReshapeND(color_img, &gray_img_hdr, 1, 0, 0);
...
row = (CvMat*)cvReshapeND(mat, &row_header, 0, 1, 0);
Sets every element of an array to a given value.
void cvSet
(CvArr* arr, CvScalar value, const CvArr* mask=NULL)¶
cv.
Set
(arr, value, mask=None) → None¶Parameters: |
|
---|
The function copies the scalar value
to every selected element of the destination array:
If array arr
is of IplImage
type, then is ROI used, but COI must not be set.
Change the particular array element.
void cvSet1D
(CvArr* arr, int idx0, CvScalar value)¶
void cvSet2D
(CvArr* arr, int idx0, int idx1, CvScalar value)¶
void cvSet3D
(CvArr* arr, int idx0, int idx1, int idx2, CvScalar value)¶
void cvSetND
(CvArr* arr, const int* idx, CvScalar value)¶
cv.
Set1D
(arr, idx, value) → None¶
cv.
Set2D
(arr, idx0, idx1, value) → None¶
cv.
Set3D
(arr, idx0, idx1, idx2, value) → None¶
cv.
SetND
(arr, indices, value) → None¶Parameters: |
|
---|
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.
Assigns user data to the array header.
void cvSetData
(CvArr* arr, void* data, int step)¶
cv.
SetData
(arr, data, step) → None¶Parameters: |
|
---|
The function assigns user data to the array header. Header should be initialized before using
cvCreateMatHeader()
, cvCreateImageHeader()
, cvCreateMatNDHeader()
,
cvInitMatHeader()
, cvInitImageHeader()
or cvInitMatNDHeader()
.
Sets the channel of interest in an IplImage.
void cvSetImageCOI
(IplImage* image, int coi)¶
cv.
SetImageCOI
(image, coi) → None¶Parameters: |
|
---|
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 Copy()
or Split()
) the channel to a separate image/matrix, process it and then copy the result back (via Copy()
or Merge()
) if needed.
Sets an image Region Of Interest (ROI) for a given rectangle.
void cvSetImageROI
(IplImage* image, CvRect rect)¶
cv.
SetImageROI
(image, rect) → None¶Parameters: |
|
---|
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.
Change a specific array element.
void cvSetReal1D
(CvArr* arr, int idx0, double value)¶
void cvSetReal2D
(CvArr* arr, int idx0, int idx1, double value)¶
void cvSetReal3D
(CvArr* arr, int idx0, int idx1, int idx2, double value)¶
void cvSetRealND
(CvArr* arr, const int* idx, double value)¶
cv.
SetReal1D
(arr, idx, value) → None¶
cv.
SetReal2D
(arr, idx0, idx1, value) → None¶
cv.
SetReal3D
(arr, idx0, idx1, idx2, value) → None¶
cv.
SetRealND
(arr, indices, value) → None¶Parameters: |
|
---|
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.
Clears the array.
void cvSetZero
(CvArr* arr)¶
cv.
SetZero
(arr) → None¶Parameters: | arr – Array to be cleared |
---|
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.
Returns the particular element of single-channel floating-point matrix.
double cvmGet
(const CvMat* mat, int row, int col)¶
cv.
mGet
(mat, row, col) → float¶Parameters: |
|
---|
The function is a fast replacement for 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.
Sets a specific element of a single-channel floating-point matrix.
void cvmSet
(CvMat* mat, int row, int col, double value)¶
cv.
mSet
(mat, row, col, value) → None¶Parameters: |
|
---|
The function is a fast replacement for 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.
Makes OpenCV use IPL functions for allocating IplImage and IplROI structures.
void cvSetIPLAllocators
(Cv_iplCreateImageHeader create_header, Cv_iplAllocateImageData allocate_data, Cv_iplDeallocate deallocate, Cv_iplCreateROI create_roi, Cv_iplCloneImage clone_image)¶Parameters: |
|
---|
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.
...
CV_TURN_ON_IPL_COMPATIBILITY()
...
Initializes a random number generator state.
CvRNG cvRNG
(int64 seed=-1)¶
cv.
RNG
(seed=-1LL) → CvRNG¶Parameters: | seed – 64-bit value used to initiate a random sequence |
---|
The function initializes a random number generator and returns the state. The pointer to the state can be then passed to the RandInt()
, RandReal()
and RandArr()
functions. In the current implementation a multiply-with-carry generator is used.
See also
the C++ class RNG
replaced CvRNG
.
Fills an array with random numbers and updates the RNG state.
void cvRandArr
(CvRNG* rng, CvArr* arr, int dist_type, CvScalar param1, CvScalar param2)¶
cv.
RandArr
(rng, arr, distType, param1, param2) → None¶Parameters: |
|
---|
The function fills the destination array with uniformly or normally distributed random numbers.
See also
Returns a 32-bit unsigned integer and updates RNG.
unsigned int cvRandInt
(CvRNG* rng)¶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.
Returns a floating-point random number and updates RNG.
double cvRandReal
(CvRNG* rng)¶The function returns a uniformly-distributed random floating-point number between 0 and 1 (1 is not included).
Create a CvMat from an object that supports the array interface.
cv.
fromarray
(array, allowND=False) → mat¶Parameters: |
|
---|
If the object supports the array interface
,
return a CvMat
or 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-channelCvMatND
with the same shape as the original array.
For example, NumPy 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).