cv::Mat是OpenCV2和OpenCV3中基本的数据类型,在cv::Mat类中,关于cv::Mat的定义和初始化有多种不同的形式,这里,将对其进行详尽的总结和介绍。
一、无数据拷贝的cv::Mat的定义和初始化
1、默认形式
cv::Mat m;
2、指定类型和大小(行列)的二维数组
cv::Mat m(int rows, int cols, int type);
3、有初始化值的指定类型和大小(行列)的二维数组
cv::Mat m(int rows, int cols, int type, const Scalar& s);
4、使用预先存在数据定义的指定类型和大小(行列)的二维数组
cv::Mat m(int rows, int cols, int type, void* data, size_t step = AUTO_STEP);
5、指定大小(size)和类型的二维数组
cv::Mat m(cv::Size sz, int type, const Scalar& s);
6、使用预先存在的数据定义的指定大小(size)和类型的二维数组
cv::Mat m(cv::Size sz, int type, void* data, size_t step = AUTO_STEP);
7、指定类型的多维数组
cv::Mat m(int ndims, const int* sizes, int type);
8、有初始化值的指定类型多维数组
cv::Mat m(int ndims, const int* sizes, int type, const Scalar& s);
9、使用预先存在的数据定义的指定类型的多维数组
cv::Mat m(int ndims, const int* sizes, int type, void* data, size_t step = AUTO_STEP);
二、从其他cv::Mat进行数据拷贝的定义和初始化
1、拷贝构造形式
cv::Mat m(const cv::Mat& mat);
2、指定行列范围的拷贝构造
cv::Mat m(const cv::Mat& mat, const cv::Range& rows, const cv::Range& cols);
3、指定ROI的拷贝构造
cv::Mat m(const cv::Mat& mat, const cv::Rect& roi);
4、使用多维数组中指定范围内的数据的拷贝构造
cv::Mat(const cv::Mat& mat, const cv::Range* ranges);
1、使用cv::Vec定义相同类型、大小为n的一维数组
cv::Mat m(const cv::Vec<T, n>& vec, bool = copyData = true);
2、使用cv::Matx定义相同类型、大小为mxn的二维数组
cv::Mat(const cv::Matx<T, m, n>& vec, bool copyData = true);
3、使用STL vector定义相同类型的一维数组
cv::Mat(const std::vector<T>& vec, bool copyData = true);
四、直接使用静态函数创建cv::Mat
1、使用zeros()函数定义指定大小和类型的cv::Mat(全为0)
cv::Mat m = cv::Mat::zeros(int rows, int cols, int type);
2、使用ones()函数定义指定大小和类型的cv::Mat(全为0)
cv::Mat m = cv::Mat::ones(int rows, int cols, int type);
3、使用eye()函数定义指定大小和类型的cv::Mat(恒等矩阵)
cv::Mat m = cv::Mat::eye(int rows, int cols, int type);