Mat::create

来源:互联网 发布:网络鬼差系统txt下载 编辑:程序博客网 时间:2024/05/29 20:01

Mat::create

Allocates new array data if needed.

C++: void Mat::create(int rows, int cols, int type)
C++: void Mat::create(Size size, int type)
C++: void Mat::create(int ndims, const int* sizes, int type)
Parameters:
  • ndims – New array dimensionality.
  • rows – New number of rows.
  • cols – New number of columns.
  • size – Alternative new matrix size specification: Size(cols, rows)
  • sizes – Array of integers specifying a new array shape.
  • type – New matrix type.

This is one of the key Mat methods. Most new-style OpenCV functions and methods that produce arrays call this method for each output array. The method uses the following algorithm:

  1. If the current array shape and the type match the new ones, return immediately. Otherwise, de-reference the previous data by calling Mat::release().
  2. Initialize the new header.
  3. Allocate the new data of total()*elemSize() bytes.
  4. Allocate the new, associated with the data, reference counter and set it to 1.

Such a scheme makes the memory management robust and efficient at the same time and helps avoid extra typing for you. This means that usually there is no need to explicitly allocate output arrays. That is, instead of writing:

Mat color;...Mat gray(color.rows, color.cols, color.depth());cvtColor(color, gray, CV_BGR2GRAY);

you can simply write:

Mat color;...Mat gray;cvtColor(color, gray, CV_BGR2GRAY);

because cvtColor , as well as the most of OpenCV functions, calls Mat::create() for the output array internally.