Overview
TheMat class is the primary data structure in OpenCV for representing n-dimensional dense arrays. It can store real or complex-valued vectors, matrices, grayscale or color images, voxel volumes, vector fields, point clouds, tensors, and histograms.
Constructors
Default Constructor
Size and Type Constructor
int
Number of rows in a 2D array
int
Number of columns in a 2D array
Size
2D array size: Size(cols, rows)
int
Array type. Use CV_8UC1, …, CV_64FC4 to create 1-4 channel matrices, or CV_8UC(n), …, CV_64FC(n) to create multi-channel matrices (up to CV_CN_MAX channels)
Constructor with Initialization
Scalar
Optional value to initialize each matrix element with
Multi-dimensional Constructor
int
Array dimensionality
const int*
Array of integers specifying an n-dimensional array shape
User Data Constructor
void*
Pointer to user data. No data is copied; the matrix header points to the specified data
size_t
Number of bytes each matrix row occupies. If AUTO_STEP, no padding is assumed
The external data is not automatically deallocated, so you should manage it manually.
Copy Constructor
ROI Constructors
Range
Range of rows to take from the source matrix
Range
Range of columns to take from the source matrix
Rect
Region of interest rectangle
Key Methods
create
clone
copyTo
OutputArray
Destination matrix. Reallocated if needed
InputArray
Operation mask (8-bit single channel). Non-zero elements indicate which matrix elements to copy
convertTo
OutputArray
Output matrix
int
Desired output matrix type or depth
double
Optional scale factor
double
Optional delta added to scaled values
m(x,y) = saturate_cast<rType>(alpha*(*this)(x,y) + beta)
row / col
int
0-based row index
int
0-based column index
rowRange / colRange
diag
int
Index of the diagonal. d=0 is the main diagonal, d<0 is below, d>0 is above
reshape
int
New number of channels. If 0, the number of channels remains unchanged
int
New number of rows. If 0, the number of rows remains unchanged
at
ptr
Static Initialization Methods
zeros
ones
eye
Properties
Data Layout
int
Number of rows (for 2D arrays)
int
Number of columns (for 2D arrays)
int
Number of matrix dimensions (≥ 2)
uchar*
Pointer to the data
size_t[]
Array of strides (step[0] contains the full row length in bytes)
Type Information
int
Returns the matrix element type (CV_8UC1, CV_32FC3, etc.)
int
Returns the depth of the matrix elements (CV_8U, CV_32F, etc.)
int
Returns the number of channels
size_t
Returns element size in bytes
size_t
Returns size of each element channel in bytes
Size Information
Size
Returns the matrix size (for 2D matrices)
size_t
Returns the total number of array elements
bool
Returns true if the array has no elements
bool
Returns true if the matrix is continuous (no gaps at the end of rows)
bool
Returns true if the matrix is a submatrix of another matrix
Operators
Assignment
Element Access
Arithmetic Operators
Matrix arithmetic operators (+, -, *, /) are supported through matrix expressions:Memory Management
Mat uses reference counting for automatic memory management. When no one references the data, it’s automatically deallocated. Example:Data Layout
For a 2D array, element (i,j) address is computed as:step[0] being the row length in bytes.
The data is stored continuously if
isContinuous() returns true, meaning there are no gaps between rows. This allows for more efficient processing.