天天看點

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" target="_blank" rel="external nofollow"  rel="stylesheet">
                </div>
           

繼續閱讀