The Temple of Dendur, The MET NYC (c) AD Photography
The Mat Structure
Mat is how OpenCV handles the images. It is the underlying data structures that allows OpenCV to process and manipulate the images or image sequences.
The upside of using OpenCV is that it provides you memory management out of the box, the programmer doesn’t have tow orry about it. And it all happens because of the way this unique data structure is designed.
The Mat structure contains two parts. The first is its header. Think of it as a meta store. This meta store contains information such as the size of the matrix, method used for storing, the address in memory at which the matrix is stored. The second part contains a pointer to the matrix containing the pixel values.
How is that beneficial? Say, when you copy an image to another variable,
Mat can be used to store real or complex-valued vectors and matrices, grayscale or color images, voxel volumes (volumetric pixel or a voxel is a three-dimensional (3D) equivalent of a pixel and the tiniest distinguishable element of a 3D object.), vector fields, point clouds (A point cloud is a set of data points in space.), tensors, histograms.
The data layout of the array M is defined by the array M.step[]. The memory location/address of each pixel is calculated by a rather simple expression (which can be thought of with higher dimensions). Here’s one for easier to understand, 2-dimensional array: