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

原文链接:https://blog.csdn.net/weixin_43455581/article/details/100557022

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

CV_[位数][是否带符号][类型前缀]C[通道数]
#include<opencv2\opencv.hpp>
#include<iostream>
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");
}
发布了107 篇原创文章 · 获赞 125 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/Li_haiyu/article/details/105360620
今日推荐