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:
- If the current array shape and the type match the new ones, return immediately. Otherwise, de-reference the previous data by callingMat::release().
- Initialize the new header.
- Allocate the new data of total()*elemSize() bytes.
- 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 会帮我们先调用一个create方法。
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.
本文深入解析了C++矩阵类Mat的create方法,包括参数、使用场景和内部实现机制,并通过实例展示了如何高效地创建矩阵,特别强调了cv::Mat类在图像处理任务中的应用。
2129

被折叠的 条评论
为什么被折叠?



