Mat相关的应用
//1、构造函数
Mat M(2,2,CV_8UC3,Scalar(0,0,255));
//参数[2 2]——矩阵大小
//CV_8UC3——元素类型
//Scalar(0,0,255)——元素初始化
//CV_8UC3——CV_[bit][符号][Type][channel]
cout<<"构造函数创建矩阵 M = "<<endl<<M<<endl<<endl;
//2、三维向量2*2*2
int sz[3] = {2,2,2};
Mat L(3,sz,CV_8UC(1), Scalar::all(0));
cout<<"M = "<<endl<<L.dims<<endl<<endl;
//3、创建IplImage*的 Mat控制头
IplImage* img = cvLoadImage("YY01.jpg",1);
Mat mtx(img);
//4、Create() 方法 //矩阵新尺寸!= 原尺寸时,才重新申请空间
M.create(4,4,CV_8UC(2)); //8UC(2)每个元素两个量
cout<<"Create() 方法创建矩阵 M = "<<endl<<" "<<M<<endl<<endl;
//5、Matlab 风格初始化 zeros(),ones(),eye()
Mat E = Mat::eye(4,4,CV_64F);
cout<<"对角4*4矩阵 E = "<<endl<<" "<<E<<endl<<endl;
Mat O = Mat::ones(4,4,CV_32F);
cout<<"全1-4*4矩阵 O = "<<endl<<" "<<O<<endl<<endl;
Mat Z = Mat::zeros(4,4,CV_8UC1);
cout<<"0-4*4矩阵 Z = "<<endl<<" "<<Z<<endl<<endl;
//6、初始化一个小的矩阵
Mat C = (Mat_<double>(3,3)<<0,-1,0,-1,5,-5,0,-1,0);
cout<<"结构元素C = "<<endl<<" "<<C<<endl<<endl;
//7、为现有Mat对象 创建一个新的矩阵头,并复制it
Mat RowClone = C.row(1).clone();
cout<<"复制已存在矩阵 RowClone = "<<endl<<" "<<RowClone<<endl<<endl;
//8、随机矩阵
Mat RandMat(3,2,CV_8UC3);
randu(RandMat,Scalar::all(0),Scalar::all(255)); //需给定随机值上下限
cout<<"随机矩阵 RandMat = "<<endl<<" "<<RandMat<<endl<<endl;