1、Mat类
//Mat的常见构造方法
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
//Mat类的构造
Mat a = cv::Mat::Mat();//a的属性由所赋的值决定
a = (cv::Mat_<int>(3, 2) << 1, 2, 3, 4, 5, 6);
Mat b = cv::Mat::Mat(3, 2, CV_8UC1);//指定行数和列数构造单通道的3x2矩阵
Mat c = cv::Mat::Mat(Size(3, 2), CV_8UC1);//指定行数和列数构造单通道的2x3矩阵,注意与上面语句相反
Mat d = cv::Mat::Mat(c);//通过已有Mat对象构造一个新的Mat对象,c和d的指向内容一样
Mat e = d.clone();//通过复制构造一个新的Mat对象,d和e的指向内容不一样
/*
@param rowRange Range of the m rows to take.As usual, the range start is inclusive and the range
end is exclusive.Use Range::all() to take all the rows.
@param colRange Range of the m columns to take.Use Range::all() to take all the columns.
这两个注释是对应cv::Mat::Mat(a, Range(0, 2), Range(0, 2))中Range(0, 2), Range(0, 2),说明这样写就是取0~1行和0~1列
*/
Mat f = cv::Mat::Mat(a, Range(0, 2), Range(0, 2));//通过已有Mat对象构造一个Mat子对象
cout << "a:" << endl << a << endl;
cout << "b:" << endl << b << endl;
cout << "c:" << endl << c << endl;
cout << "d:" << endl << d << endl;
cout << "e:" << endl << e << endl;
cout << "f:" << endl << f << endl;
return 0;
}
//Mat类的常见赋值
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
//Mat类的赋值
Mat a, b, c;
a = b = c = cv::Mat::Mat();
b = (cv::Mat_<int>(3, 2) << 1, 2, 3, 4, 5, 6);//枚举赋值法,必须加括号,不然VS2017会报错
c = cv::Mat::Mat(2, 2, CV_8UC3, cv::Scalar(0, 0, 255));//3通道矩阵,每个像素的三个通道值都是0, 0, 255
//循环赋值法
Mat d = cv::Mat_<int>(3, 3);
for (int i = 0; i < d.rows; i++)
{
for (int j = 0; j < d.cols; j++)
{
d.at<int>(i, j) = 1;
}
}
Mat e = cv::Mat::Mat::zeros(2, 2, CV_8UC1);//类方法赋值
float m_array[8] = { 1,2,3,4,5,6,7,8 };
Mat f = cv::Mat(2, 2, CV_32FC2, m_array);//利用数组赋值
cout << "a:" << endl << a << endl;
cout << "b:" << endl << b << endl;
cout << "c:" << endl << c << endl;
cout << "d:" << endl << d << endl;
cout << "e:" << endl << e << endl;
cout << "f:" << endl << f << endl;
return 0;
}
//Mat类的运算
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
//Mat类的运算
Mat a, b, c;
a = (cv::Mat_<float>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
b = (cv::Mat_<float>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
c = (cv::Mat_<float>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
cout << "加法:" << endl;
cout << a << "+" << endl << b << "=" << endl << a + b << endl;
cout << "减法:" << endl;
cout << a << "-" << endl << b << "=" << endl << a - b << endl;
cout << "数乘:" << endl;
cout << a << "*" << endl << 2 << "=" << endl << a * 2 << endl;
cout << "除法:" << endl;
cout << a << "/" << endl << 2 << "=" << endl << a / 2 << endl;
cout << "内积:" << endl;
cout << a << "*" << endl << c << "=" << endl << a.dot(c) << endl;
cout << "点乘:" << endl;
cout << a << ".*" << endl << b << "=" << endl << a.mul(b) << endl;
return 0;
}
//Mat类元素的读取
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main()
{
//Mat类元素的读取
//1、通过at方法读取
Mat a = cv::Mat(3, 3, CV_8UC3, Scalar(1,1,1));
cv::Vec3b b = a.at<Vec3b>(0, 0);//Vec3b对应uchar类型
cout << "a[0,0]=" << b << endl;
//2、通过指针读取
uchar* ptr;
for (int i = 0; i < a.rows; i++)
{
ptr = a.ptr<uchar>(i);
for (int j = 0; j < a.cols*a.channels(); j++)//注意列数循环要乘上通道数
{
cout << (int)ptr[j] << " ";//注意要转换为int类型
}
cout << endl;
}
//3、通过迭代器
//我不会迭代器
//4、通过地址
//int c = (int)(*(a.data + a.step[0]*row + a.step[1]*col + channel));
cv::Mat d(3, 3, CV_8UC3, Scalar(0, 1, 2));
int c = (int)(*(d.data + d.step[0] * 0 + d.step[1] * 0 + 0));
cout << "d[0,0,1]=" << c << endl;
c = (int)(*(d.data + d.step[0] * 0 + d.step[1] * 0 + 1));
cout << "d[0,0,2]=" << c << endl;
c = (int)(*(d.data + d.step[0] * 0 + d.step[1] * 0 + 2));
cout << "d[0,0,3]=" << c << endl;
return 0;
}