cv::Mat類型的定義初始化方法彙總

原文鏈接:https://blog.csdn.net/guduruyu/article/details/66973415
cv::Mat 是Opencv2和OpenCV3中基本的數據類型,在cv::Mat類中,關於cv::Mat的定義和初始化有多種不同的形式,下面對其進行總結。
一、無數據拷貝的cv::Mat的定義和初始化
1、默認形式
cv::Mat m;
2、指定類型和大小(行列)的二維數組
cv::Mat m(int cols,int rows,int type);
或者 cv::Mat m(Size(int cols,int rows),int type);
for example,
cv::Mat m(2,3,CV_64FC1);
cv::Mat m(Size(2,3),CV_64FC1);
注:Size(width, height), 寬
3、有初始化值的指定類型和大小(行列)的二維數組
cv::Mat m(int cols,int rows,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,voiddata,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);
二、從其他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);
三、使用Opencv中的模板進行定義和初始化
1、使用cv::Vec定義相同類型、大小爲n的一維數組
cv::Mat m(const cv::Vec<T, n>& vec, bool=copyData=true);
2、使用cv::Matx定義相同類型、大小爲m*n的二維數組
cv::Mat m(const cv::Matx<T,m,n>& vec, bool copyData=true);
3、使用STL vector定義相同的一維數組
cv::Mat (const std::vector& 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(全爲1)
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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章