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
Number of rows in a 2D array
Number of columns in a 2D array
2D array size: Size(cols, rows)
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
Optional value to initialize each matrix element with
Multi-dimensional Constructor
Array dimensionality
Array of integers specifying an n-dimensional array shape
User Data Constructor
Pointer to user data. No data is copied; the matrix header points to the specified data
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 of rows to take from the source matrix
Range of columns to take from the source matrix
Region of interest rectangle
Key Methods
create
clone
copyTo
Destination matrix. Reallocated if needed
Operation mask (8-bit single channel). Non-zero elements indicate which matrix elements to copy
convertTo
Output matrix
Desired output matrix type or depth
Optional scale factor
Optional delta added to scaled values
m(x,y) = saturate_cast<rType>(alpha*(*this)(x,y) + beta)
row / col
0-based row index
0-based column index
rowRange / colRange
diag
Index of the diagonal. d=0 is the main diagonal, d<0 is below, d>0 is above
reshape
New number of channels. If 0, the number of channels remains unchanged
New number of rows. If 0, the number of rows remains unchanged
at
ptr
Static Initialization Methods
zeros
ones
eye
Properties
Data Layout
Number of rows (for 2D arrays)
Number of columns (for 2D arrays)
Number of matrix dimensions (≥ 2)
Pointer to the data
Array of strides (step[0] contains the full row length in bytes)
Type Information
Returns the matrix element type (CV_8UC1, CV_32FC3, etc.)
Returns the depth of the matrix elements (CV_8U, CV_32F, etc.)
Returns the number of channels
Returns element size in bytes
Returns size of each element channel in bytes
Size Information
Returns the matrix size (for 2D matrices)
Returns the total number of array elements
Returns true if the array has no elements
Returns true if the matrix is continuous (no gaps at the end of rows)
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.