The general answer is to calculate the total number of elements in the matrix and multiply by the size of each element, for example:
size_t sizeInBytes = mat.total() * mat.elemSize();
This will work in normal scenarios where the matrix has been allocated as a continuous fragment in memory.
, . , mat.cols * mat.elemSize() , mat.isContinuous() - false, , mat.elemSize() , !
, :
size_t sizeInBytes = mat.step[0] * mat.rows;
step .