使用指针初始化cv::Mat

 

使用指针初始化cv::Mat

使用指针初始化cv::Mat的时候没有发生实际的内存拷贝

,所以不能使用临时变量初始化一个Mat。

使用指针进行初始化的几个构造函数如下:

Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);
Mat(Size size, int type, void* data, size_t step=AUTO_STEP);
Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);
Mat(const std::vector<int>& sizes, int type, void* data, const size_t* steps=0);

# 它们的函数注释上,对于data的解释都是一样的:
/**
@param data Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.
*/

即整个构造过程只是将Mat.data 赋值为传入的 data指针。

Ps: 使用vector初始化可以参看如下构造函数的声明,可以看出来默认不拷贝的数据。

 template<typename _Tp> explicit Mat(const std::vector<_Tp>& vec, bool copyData=false);