OpenCV4 daily practice day4: creation, assignment and reading of Mat class

1. Understand the Mat class

Insert picture description here
Insert picture description here
Insert picture description here

2. Three common ways to create Mat classes:

The rows and cols parameters of creating the Mat class are the same as the result obtained by counting
Insert picture description here
Insert picture description here
Insert picture description here
Mat.rowRange(int x, int y) and Mat.rowRange(range(int x,int y) from 1, the actual number of rows taken by the function yx, only Take the left boundary of the range, not the right boundary.

3. Assignment of Mat class

Insert picture description here
Insert picture description here
Insert picture description here

Four. Example:

Insert picture description here
Operation result:
Insert picture description here
Attach example code:

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

using namespace std;
using namespace cv;

int main()
{
    
    
	system("color F0");//把运行结果窗口处的界面由黑色变成白色
	Mat a(3, 3, CV_8UC1);      //Mat类的创建(利用矩阵宽、高和类型参数)创建一个行为3,列为3的单通道矩阵
	Mat b(Size(4, 4), CV_8UC1);//Mat类的创建(利用矩阵Size()结构和类型参数)创建一个行为3,列为3的单通道矩阵

	Mat c0(5, 5, CV_8UC3, Scalar(0, 0, 255));//Mat类的赋值(创建时赋值):创建一个三通道矩阵,每个像素都是0,0,255
	Mat c1(5, 5, CV_8UC2, Scalar(0, 255));   //Mat类的赋值(创建时赋值):创建一个双通道矩阵,每个像素都是0,255
	Mat c2(5, 5, CV_8UC1, Scalar(255));      //Mat类的赋值(创建时赋值):创建一个单通道矩阵,每个像素都是255

	Mat d = (cv::Mat_<int>(1, 5) << 1, 2, 3, 4, 5);  //Mat类的赋值(枚举法赋值)
	Mat e = Mat::diag(d); 					         //Mat类的赋值(类方法赋值)
	Mat f = Mat(e, Range(2, 4), Range(2, 4));        //Mat类的创建(利用已有Mat类创建新的Mat类),从e中截取部分数据构造f

	cout << "a=" << endl << a << endl << endl;
	cout << "b=" << endl << b << endl << endl;
	cout << "c0="<< endl << c0<< endl << endl;
	cout << "c1="<< endl << c1<< endl << endl;
	cout << "c2="<< endl << c2<< endl << endl;
	cout << "d=" << endl << d << endl << endl;
	cout << "e=" << endl << e << endl << endl;
	cout << "f=" << endl << f << endl << endl;
	system("pause");
	return 0;
}

Five. Mat class reading

Insert picture description here
Insert picture description here
Read the row and col parameters of the Mat matrix elements are counted from 0

1.

Insert picture description here

2.

Insert picture description here

6. Example:

Insert picture description here
Operation result:
Insert picture description here
Attach example code:

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

using namespace std;
using namespace cv;

int main()
{
    
    
	system("color F0");//把运行结果窗口处的界面由黑色变成白色

	Mat a = (cv::Mat_<int>(3,3) << 1,2,3,4,5,6,7,8,9);  //Mat类的赋值(枚举法赋值)

	Mat c1(5, 5, CV_8UC2, Scalar(4, 5));   //Mat类的赋值(创建时赋值):创建一个双通道矩阵,每个像素都是0,255
	Mat c2(3, 3, CV_8UC3, Scalar(4, 5, 6));//Mat类的赋值(创建时赋值):创建一个三通道矩阵,每个像素都是0,0,255

	cout << a.at<int>(0, 0) << endl;

	Vec2b vc = c1.at<Vec2b>(0, 1);
	cout << vc << endl;
	cout << (int)vc[0] << endl;
	cout << vc[1] << endl;//没有读出来

	cout << c2 << endl;
	cout << (double)(*(c2.data + c2.step[0] * 1 + c2.step[1] * 2 + 2)) << endl;//第1行第2列第2个通道(从0开始数的)

	system("pause");

	return 0;
}

7. Operations supported by Mat

Here are a few, the functions provided by OpenCV are far more than this:
Insert picture description here
you will find the function documentation provided by OpenCV: https://docs.opencv.org/
When you encounter a specific function, check the current one.
Insert picture description here

1. Four arithmetic operations

Insert picture description here

2. The arithmetic functions provided

Insert picture description here

8. Example:

Insert picture description here
Operation result:
Insert picture description here
Attach example code:

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

using namespace std;
using namespace cv;

int main()
{
    
    
	system("color F0");//把运行结果窗口处的界面由黑色变成白色

	Mat a = (cv::Mat_<int>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
	Mat b = (cv::Mat_<int>(3, 3) << 1, 2, 3, 7, 8, 9, 4, 5, 6);
	Mat c = (cv::Mat_<double>(3, 3) << 1.0, 2.1, 3.2, 4.0, 5.1, 6.2, 2, 2, 2);
	Mat d = (cv::Mat_<double>(3, 3) << 1.0, 2.1, 3.2, 4.0, 5.1, 6.2, 2, 2, 2);

	cout << "两个矩阵的和=" << endl << a + b << endl;
	cout << "两个矩阵的差=" << endl << c - d << endl;
	cout << "矩阵数乘" << endl << 2 * a << endl;
	cout << "矩阵数除" << endl << d / 2.0 << endl;
	cout << "矩阵数减" << endl << a - 1 << endl;
	cout << "两矩阵相乘" << endl << c*d << endl;
	cout << "矩阵内积" << endl << a.dot(b) << endl;
	cout << "矩阵对应位相乘" << endl << a.mul(b) << endl;
	cout << "两个矩阵最小值" << endl << min(a, b) << endl;

	system("pause");

	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43297891/article/details/114370374