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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-NC-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43455581/article/details/100557022

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

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

 
 
  • 1
#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");
}

 
 
  • 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编程入门》

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值