DJI_lover
DJI_lover

Reputation: 61

How to get BGR data and data_size from cv::Mat

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.

Upvotes: 0

Views: 583

Answers (1)

Costantino Grana
Costantino Grana

Reputation: 3418

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.

Upvotes: 1

Related Questions