0

I can get BGR data from Mat.data pointer, but I don't know how to calculate the data size. Could somebody help me? Thanks.

Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
DJI_lover
  • 61
  • 1
  • 6

1 Answers1

1

If your matrix is continuous, I'd go with cv::Mat::total() to get the number of elements and cv::Mat::elemSize() to get the matrix element size in bytes:

Mat m;
//...

uchar* data = m.data();
auto datasize = m.total() * m.elemSize();

An alternative could be (but I'm not so sure, so double check this) to take the difference between cv::Mat::dataend and cv::Mat::datastart

auto datasize = m.dataend - m.datastart;

If your matrices are not continuous, I guess that you can still use the first method to obtain the size, but don't memcpy() that amount of bytes, because it won't be your image data.

Costantino Grana
  • 3,132
  • 1
  • 15
  • 35