OpenCV Mat Object - Get Data Length

In OpenCV, I can capture frames using VideoCapture in C ++, however, when I try to get data from a frame and calculate the length, it just returns me 0.

The following is sample code:

VideoCapture cap(0);
for(;;) {
  Mat frame;
  cap >> frame;
  int length = strlen((char*) frame.data); // returns 0
}

As I mentioned above, if I save the frame in a PNG file, I can really see the image, so I can not understand why the data length goes out to zero.

Any clue?

+4
source share
2 answers

The strlen method only works with strings, which are arrays of characters ending with a special character:

http://www.cplusplus.com/reference/cstring/strlen/

You have selected the type Matas char*, so this is not a string.

, :

Mat mat;
int rows = mat.rows;
int cols = mat.cols;
int num_el = rows*cols;
int len = num_el*mat.elemSize1();

. , elemSize(), (.. 3 elemSize1(), Mat 3- ).

Mat :

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-type

+7

:

Mat mat;
int len = mat.total() * mat.elemSize(); // or mat.elemSize1()
+2

Source: https://habr.com/ru/post/1534923/


All Articles