Skip to main content

Overview

The Mat 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

Creates an empty matrix with no allocated data.

Size and Type Constructor

rows
int
Number of rows in a 2D array
cols
int
Number of columns in a 2D array
size
Size
2D array size: Size(cols, rows)
type
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)
Example:

Constructor with Initialization

s
Scalar
Optional value to initialize each matrix element with
Example:

Multi-dimensional Constructor

ndims
int
Array dimensionality
sizes
const int*
Array of integers specifying an n-dimensional array shape
Example:

User Data Constructor

data
void*
Pointer to user data. No data is copied; the matrix header points to the specified data
step
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

Creates a matrix header for the same data. This is an O(1) operation that does not copy data but increments the reference counter.

ROI Constructors

rowRange
Range
Range of rows to take from the source matrix
colRange
Range
Range of columns to take from the source matrix
roi
Rect
Region of interest rectangle
Example:

Key Methods

create

Allocates new array data if needed. If the array already has the specified size and type, the method does nothing.

clone

Creates a full copy of the array and underlying data. Example:

copyTo

m
OutputArray
Destination matrix. Reallocated if needed
mask
InputArray
Operation mask (8-bit single channel). Non-zero elements indicate which matrix elements to copy

convertTo

m
OutputArray
Output matrix
rtype
int
Desired output matrix type or depth
alpha
double
Optional scale factor
beta
double
Optional delta added to scaled values
Converts array to another data type with optional scaling: m(x,y) = saturate_cast<rType>(alpha*(*this)(x,y) + beta)

row / col

Creates a matrix header for the specified matrix row or column. This is an O(1) operation.
y
int
0-based row index
x
int
0-based column index
Example:

rowRange / colRange

Creates a matrix header for the specified row or column span.

diag

d
int
Index of the diagonal. d=0 is the main diagonal, d<0 is below, d>0 is above
Extracts a diagonal from a matrix or creates a diagonal matrix.

reshape

cn
int
New number of channels. If 0, the number of channels remains unchanged
rows
int
New number of rows. If 0, the number of rows remains unchanged
Changes the shape and/or number of channels without copying data.

at

Returns a reference to the specified array element. Example:
No bounds checking is performed in Release builds. Use with caution.

ptr

Returns a pointer to the specified matrix row. Example:

Static Initialization Methods

zeros

Returns a zero array of the specified size and type. Example:

ones

Returns an array of all 1’s of the specified size and type.

eye

Returns an identity matrix of the specified size and type. Example:

Properties

Data Layout

rows
int
Number of rows (for 2D arrays)
cols
int
Number of columns (for 2D arrays)
dims
int
Number of matrix dimensions (≥ 2)
data
uchar*
Pointer to the data
step
size_t[]
Array of strides (step[0] contains the full row length in bytes)

Type Information

type()
int
Returns the matrix element type (CV_8UC1, CV_32FC3, etc.)
depth()
int
Returns the depth of the matrix elements (CV_8U, CV_32F, etc.)
channels()
int
Returns the number of channels
elemSize()
size_t
Returns element size in bytes
elemSize1()
size_t
Returns size of each element channel in bytes

Size Information

size()
Size
Returns the matrix size (for 2D matrices)
total()
size_t
Returns the total number of array elements
empty()
bool
Returns true if the array has no elements
isContinuous()
bool
Returns true if the matrix is continuous (no gaps at the end of rows)
isSubmatrix()
bool
Returns true if the matrix is a submatrix of another matrix

Operators

Assignment

Matrix assignment is an O(1) operation that copies the header and increments the reference counter.

Element Access

Provides element access similar to at() method.

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:
Matrices are stored row-by-row, with 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.