Search for size in bytes cv :: Mat

I use OpenCV with objects cv::Mat, and I need to know the number of bytes that my matrix takes to pass it to the low-level API C. It seems that the OpenCV API does not have a method that returns the number of bytes that the matrix uses, and I only have open uchar *dataopen A member without a member that contains its actual size.

How to find the size cv::Matin bytes?

+4
source share
1 answer

The general answer is to calculate the total number of elements in the matrix and multiply by the size of each element, for example:

// Given cv::Mat named mat.
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 .

+21

All Articles