OpenCV Mat实例详解 三

OpenCV Mat实例详解 一、二介绍了,OpenCV Mat类构造函数及其公共属性。下面继续介绍OpenCV Mat类公有静态成员函数

OpenCV Mat类公有静态成员函数(Static Public Member Functions)

static CV_NODISCARD_STD Matdiag (const Mat &d);

该函数用已有的Mat对象的数据矩阵对角线上的数据填充新创建Mat对象数据矩阵。下面创建一个空的控制台应用程序,来演示其用法·,在程序中加入如下代码:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	Mat dst = src.diag();
	cout << src << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行结果如下:

OpenCV Mat类还有一个类似的公有函数,如下:

Mat diag (int d=0) const

d 左上角的位置,如果d值为 负,则为左边算起第一个方阵的右下角。修改上面的代码,来演示该函数的用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	Mat dst = src.diag(0);
	cout << src << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

修改上面示例代码,修改后如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	uchar mdata[3][4] = { (1,2,3,4),(5,6,7,8),(9,10,11,12) };
	Mat src = Mat(3, 4, CV_8UC3, mdata);
	Mat dst = src.diag(0);
	cout << src << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

修改上面示例代码,修改后如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	Mat dst = src.diag(1);
	cout << src << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

修改上面示例代码,修改后如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	Mat dst = src.diag(2);
	cout << src << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

试运行,结果如下:

修改上面示例代码,修改后如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	Mat dst = src.diag(-2);
	cout << src << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

static CV_NODISCARD_STD MatExpr eye (int rows, int cols, int type);

返回一个对角元素为1,其余元素为0的Mat对象

rows 返回Mat对象的rows

cols 返回Mat对象的cols

type 返回Mat对象的type

修改上面示例代码,来演示该函数的用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	Mat src;
	Mat dst = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

修改上面示例代码,修改后如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	Mat src;
	Mat dst = src.eye(3, 3, CV_8UC1);
	src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

再修改上面示例代码,修改后如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	Mat src;
	Mat dst = src.eye(3, 3, CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

可以看出,该函数不会改变原有Mat对象。

static CV_NODISCARD_STD MatExpr eye (Size size, int type);

该函数与上一个函数作用一样,仅是用Size代替了cols,与rows。

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

从结果可以看出,返回Mat对象数据矩阵如果不是方矩阵,只有左上角为第一个元素的方矩阵对角线上的元素被填充为1,其余元素为0.

static MatAllocator * getDefaultAllocator ();

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	MatAllocator* pMem = src.getDefaultAllocator();
	if (pMem)
	{
		cout << "Defaultallocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	waitKey(0);
	return 0;
}

试运行,结果如下:

static MatAllocator * getStdAllocator ();

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	waitKey(0);
	return 0;
}

试运行,结果如下:

static CV_NODISCARD_STD MatExpr ones (int rows, int cols, int type);

返回一个数据矩阵被填充为1的Mat对象

rows 返回Mat对象的rows

cols 返回Mat对象的cols

type 返回Mat对象的type

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/
	Mat src;
	src = src.ones(3, 5, CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;

	waitKey(0);
	return 0;
}

试运行,结果如下:

static CV_NODISCARD_STD MatExpr ones (Size size, int type);

这个函数与上一个函数作用相同,Size中包含了rows、cols。

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(Size(3,5), CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;

	waitKey(0);
	return 0;
}

试运行,结果如下:

注意Size的两个参数与cols,rows的对应关系。

static CV_NODISCARD_STD MatExpr ones (int ndims, const int *sz, int type);

返回一个数据矩阵被填充为1的Mat对象

ndims 返回Mat对象的维度,1,2有效

*sz 含有返回Mat对象cows,rols数据的int数组

type 返回Mat对象的type

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/
	int mdat[] = {3,4};
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(2, mdat,CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;

	waitKey(0);
	return 0;
}

试运行,结果如下:

修改上面的示例代码,将ndims的值修改为1,修改后的代码如下:

复制代码
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/
	int mdat[] = {3,4};
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(1, mdat,CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;

	waitKey(0);
	return 0;
}

试运行,结果如下:

static void setDefaultAllocator (MatAllocator *allocator);

修改上面的示例代码,来演示其用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/

	/*
	int mdat[] = {3,4};
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(1, mdat,CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	*/

	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	src.resize(src.rows / 2, src.cols / 2);
	MatAllocator* pMem = src.getDefaultAllocator();
	Mat dst = Mat(src.rows, src.cols, src.type());
	dst.setDefaultAllocator(pMem);
	dst.data = src.data;
	imshow("dst", dst);
	waitKey(0);
	return 0;
}

试运行,结果如下:

static CV_NODISCARD_STD MatExpr zeros (int rows, int cols, int type);

static CV_NODISCARD_STD MatExpr zeros (Size size, int type);

static CV_NODISCARD_STD MatExpr zeros (int ndims, const int *sz, int type);

返回数据矩阵被填充为0的Mat对象

row 返回Mat对象的rows

cols 返回Mat对象的cols

type 返回Mat对象的type

ndims 返回Mat对象的维度,1,2有效。

sz 含有返回Mat对象cols,rows数值的int数组

修改上面的示例代码,来演示这几个函数用法,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/

	/*
	int mdat[] = {3,4};
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(1, mdat,CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	src.resize(src.rows / 2, src.cols / 2);
	MatAllocator* pMem = src.getDefaultAllocator();
	Mat dst = Mat(src.rows, src.cols, src.type());
	dst.setDefaultAllocator(pMem);
	dst.data = src.data;
	imshow("dst", dst);
	*/

	Mat src;
	src = src.zeros(3, 4, CV_8UC1);
	cout << "src cols:  " << src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

再修改上面的示例代码,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/

	/*
	int mdat[] = {3,4};
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(1, mdat,CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	src.resize(src.rows / 2, src.cols / 2);
	MatAllocator* pMem = src.getDefaultAllocator();
	Mat dst = Mat(src.rows, src.cols, src.type());
	dst.setDefaultAllocator(pMem);
	dst.data = src.data;
	imshow("dst", dst);
	*/

	Mat src;
	//src = src.zeros(3, 4, CV_8UC1);
	src = src.zeros(Size(4,3), CV_8UC1);
	cout << "src cols:  " << src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

再修改上面的示例代码,修改后的代码如下:

复制代码
// OpenCVMatTest3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	
	//Mat src = (Mat_<uchar>(3, 3 ) << 1, 0, 0,  0, 1, 0,  0, 0, 1);
	//Mat dst = src.diag();
	//Mat dst = src.diag(0);

	//uchar mdata[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	//Mat src = Mat(3, 4, CV_8UC1, mdata);
	//Mat dst = src.diag(0);
	//Mat dst = src.diag(1);
	//Mat dst = src.diag(2);
	//Mat dst = src.diag(-1);
	/*
	Mat src;
	Mat dst = src.eye(Size(3,4), CV_8UC1);
	//src = src.eye(3, 3, CV_8UC1);
	cout << "dst cols: " << dst.cols << endl;
	cout << "dst rows: " << dst.rows << endl;
	cout << "dst tpye: " << dst.type() << endl;
	cout << "src:" << endl;
	cout << src << endl;
	cout << "dst:" << endl;
	cout << dst << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	//MatAllocator* pMem = src.getDefaultAllocator();
	MatAllocator* pMem = src.getStdAllocator();
	if (pMem)
	{
		cout << "StdAllocator Address:  " << (unsigned int)pMem << endl;
	}
	else
	{
		cout << "pMem is null" << endl;
	}
	imshow("", src);
	*/

	/*
	int mdat[] = {3,4};
	Mat src;
	//src = src.ones(3, 5, CV_8UC1);
	src = src.ones(1, mdat,CV_8UC1);
	cout << "src cols:  "<< src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	*/

	/*
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "Conn't read image!" << endl;
		return -1;
	}
	src.resize(src.rows / 2, src.cols / 2);
	MatAllocator* pMem = src.getDefaultAllocator();
	Mat dst = Mat(src.rows, src.cols, src.type());
	dst.setDefaultAllocator(pMem);
	dst.data = src.data;
	imshow("dst", dst);
	*/
	int  mdat[] = {3,5 };
	Mat src;
	//src = src.zeros(3, 4, CV_8UC1);
	//src = src.zeros(Size(4,3), CV_8UC1);
	src = src.zeros(2, mdat,CV_8UC1);
	cout << "src cols:  " << src.cols << endl;
	cout << "src rows:  " << src.rows << endl;
	cout << "src type:  " << src.type() << endl;
	cout << src << endl;
	waitKey(0);
	return 0;
}

试运行,结果如下:

到这里,OpenCV Mat类的静态公有成员函数就介绍完了。

示例是基于OpenCV4.8(opencv目录位于d盘根目录下)及VS2022。示例源代码,已上传到CSDN,链接为:https://download.csdn.net/download/billliu66/88836976

相关推荐
九年义务漏网鲨鱼1 小时前
【大模型学习 | MINIGPT-4原理】
人工智能·深度学习·学习·语言模型·多模态
元宇宙时间1 小时前
Playfun即将开启大型Web3线上活动,打造沉浸式GameFi体验生态
人工智能·去中心化·区块链
开发者工具分享2 小时前
文本音频违规识别工具排行榜(12选)
人工智能·音视频
产品经理独孤虾2 小时前
人工智能大模型如何助力电商产品经理打造高效的商品工业属性画像
人工智能·机器学习·ai·大模型·产品经理·商品画像·商品工业属性
老任与码2 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
蹦蹦跳跳真可爱5892 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
雷羿 LexChien2 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
两棵雪松3 小时前
如何通过向量化技术比较两段文本是否相似?
人工智能
heart000_13 小时前
128K 长文本处理实战:腾讯混元 + 云函数 SCF 构建 PDF 摘要生成器
人工智能·自然语言处理·pdf
敲键盘的小夜猫3 小时前
LLM复杂记忆存储-多会话隔离案例实战
人工智能·python·langchain