OpenCV3 之 显式创建Mat对象的几种方法

指定存储元素的数据类型以及每个矩阵点的通道数

CV_[位数][是否带符号][类型前缀]C[通道数]

   
   
   
   
  • 1
#include
#include
using namespace std;
using namespace cv;

int main() {

	// 指定存储元素的数据类型以及每个矩阵点的通道数:
	//		CV_[位数][是否带符号][类型前缀]C[通道数]

	// 方法1:使用Mat()构造函数
	Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
	cout << "M = " << endl << " " << M << endl << endl;
	cout << "---------------------------------------" << endl;

	// 方法2:利用create()函数
	M.create(4, 4, CV_8UC(3));
	cout << "M = " << endl << " " << M << endl << endl;
	cout << "---------------------------------------" << endl;

	
	// 方法3:采用Matlab式的初始化方式
	Mat E = Mat::eye(4, 4, CV_64F);
	cout << "E = " << endl << " " << E << endl << endl;
	cout << "---------------------------------------" << endl;

	Mat O = Mat::ones(2, 2, CV_32F);
	cout << "O = " << endl << " " << O << endl << endl;
	cout << "---------------------------------------" << endl;

	Mat Z = Mat::zeros(3, 3, CV_8UC1);
	cout << "Z = " << endl << " " << Z << endl << endl;
	cout << "---------------------------------------" << endl;

	// 方法4:对小矩阵使用逗号分隔式初始化函数
	//						分隔式顺序按列从左到右
	Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	cout << "C = " << endl << " " << C << endl << endl;
	cout << "---------------------------------------" << endl;

	// 方法5:为已存在的对象创建新信息头
	Mat RowClone = C.row(1).clone();
	cout << "RowClone = " << endl << " " << RowClone << endl << endl;
	cout << "---------------------------------------" << endl;

	// 使用randu()方法填充矩阵
	Mat r = Mat(10, 3, CV_8UC3);
	randu(r, Scalar::all(0), Scalar::all(255));
	cout << r<<endl << endl;
	cout << "------------------------------" << endl;
	
	system("pause");
}

   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

参考书籍《OpenCV3编程入门》

                                

你可能感兴趣的:(opencv,c++,opencv3,Mat)