初识Mat类

目录

 

1.Mat类简介

2.单通道Mat

3.多通道Mat


1.Mat类简介

Mat是Matrix的缩写,代表矩阵或者数组的意思,该类的声明在头文件opencv2/core.hpp中。构造Mat对象需要四个基本要素:行数(高),列数(宽)、通道数及数据类型。其构造函数如下:

//方法一
Mat(int rows,int cols,int type)

//方法二
Mat(Size(int cols,int rows),int type)

rows代表行数,cols代表列数,type代表类型,包括通道数和数据类型,可设置值为CV_8UC(n)CV_8SC(n)CV_16SC(n)CV_16UC(n)CV_32SC(n)CV_64FC(n)。其中8U、8S、16S、16U、32S、64F前面的数字代表Mat中每一个数值所占的bit数,而1byte=8bit,所以32F就是4字节的float类型,64F是占8字节的double类型,32S是占4字节的int类型,8U是占1字节的uchar类型……C(n)代表通道数,当n=1时,即构造单通道矩阵或者二维矩阵,当n>1时,构造多通道矩阵即三维矩阵。

2.单通道Mat

void useSingleMat()
{
	Mat m = (Mat_<int>(3, 2) << 11, 12, 33, 43, 51, 16);
	///获取Mat矩阵的信息/
	cout << "rows" << m.rows << endl;
	cout << "cols" << m.cols << endl;

	Size size = m.size();
	cout << "size = " << size << endl;
	cout << "channels = " << m.channels() << endl;

	cout << "rows * cols = " << m.total() << endl;
	cout << "dims = " << m.dims << endl;

	///访问Mat的值///
	int i, j;
	//-----------at
	cout << "at方法访问Mat值" << endl;
	for (i = 0; i < m.rows; i++)//i---y
	{
		for (j = 0; j < m.cols; j++)//j--x
		{
			//cout << m.at<int>(i, j) << ",";//第i行第j列的值
			cout << m.at<int>((Point(j, i))) << ",";//将索引变成坐标形式
		}
		cout << endl;
	}
	//-----------ptr
	cout << "ptr方法访问Mat值" << endl;
	for (i = 0; i < m.rows; i++)
	{
		const int* ptr = m.ptr<int>(i);//得到矩阵m的第i行的首地址
		for (j = 0; j < m.cols; j++)
		{
			cout << ptr[j] << ",";
		}
		cout << endl;
	}
	//-----------isComtinuous ptr
	//isComtinuous 表示行与行之间是否是连续存储;如果isComtinuous=true,则行与行之间连续存储
	cout << "isComtinuous和ptr方法访问Mat值" << endl;
	if (m.isContinuous())
	{
		//得到矩阵m的第一个值的地址
		int *ptr = m.ptr<int>(0);
		for (int n = 0; n < m.total(); n++)
			cout << ptr[n] << ",";
	}
}

结果

3.多通道Mat

void useMultiMat(Mat m) 
{
	///多通道的基础,向量
	Vec<int, 3> vi(21, 32, 14);//构造一个长度为3,数据类型为int,且初始化为21 32 14的列向量
	cout << "rows = " << vi.rows << endl;
	cout << "cols = " << vi.cols << endl;
	cout << "vi[0] = " << vi[0] << endl;
	cout << "vi(1) = " << vi(1) << endl;

	//构造多通道
	Mat mm = (Mat_<Vec3f>(2, 2) << Vec3f(1, 11, 21), Vec3f(2, 12, 22), Vec3f(3, 13, 23), Vec3f(4, 24, 34));
	/访问多通道Mat对象中的值
	//-----------at
	//cout << "at方法访问Mat值" << endl;
	for (int r = 0; r < mm.rows; r++)
	{
		for (int c = 0; c < mm.cols; c++)
		{
			cout << mm.at<Vec3f>(r, c) << ",";
		}
		cout << endl;
	}
	//-----------ptr
	cout << "ptr方法访问Mat值" << endl;
	for (int r = 0; r < mm.rows; r++)
	{
		Vec3f* ptr = mm.ptr<Vec3f>(r);//得到矩阵m的第i行的首地址
		for (int c = 0; c < mm.cols; c++)
		{
			cout << ptr[c] << ",";
		}
		cout << endl;
	}
	/将多通道分离成多个单通道
	cout << "多通道分离成多个单通道" << endl;
	vector<Mat> planes;
	split(mm, planes);
	for (int i = 0; i < planes.size(); i++)
	{
		/*cout << "planes[" << i << "] = " << planes[i] << endl;*/
		cout << planes[i] << endl;
	}
	将多个单通道合并成一个多通道
	//cout << "多个单通道合并成一个多通道" << endl;
	//-----法一:使用矩阵
	Mat plane0 = (Mat_<int>(2, 2) << 1, 2, 3, 4);
	Mat plane1 = (Mat_<int>(2, 2) << 5, 6, 7, 8);
	Mat plane2 = (Mat_<int>(2, 2) << 9, 10, 11, 12);

	Mat plane[] = { plane0,plane1,plane2 };//用三个单通道矩阵初始化一个数组
	Mat mat;
	merge(plane, 3, mat);//合并
	//-----法二:使用Mat数组,将其存放在Vector容器中
	vector<Mat> vplane;
	vplane.push_back(plane0);
	vplane.push_back(plane1);
	vplane.push_back(plane2);
	merge(vplane, mat);
}

结果

猜你喜欢

转载自blog.csdn.net/guaizaiguaizai/article/details/112402482
Mat