endl;
2. 对图像进行灰度化操作,将Mat转为二维。
// 进行图像灰度化操作
cvtColor(mat, mat, CV_BGR2GRAY);
3. Mat有rows和cols属性,rows表示对应矩阵行数,cols表示对应矩阵列数:
//保存mat的行和列
int row = mat.rows;
int col = mat.cols;
4. Mat还具有size () 方法,该方法可以得到width宽度和height高度:
Size s = mat.size();
int width = s.width;
int height = s.height;
5. rows,cols,width,height 之间的关系:
cout << " s.width : " << s.width << endl;
cout << " s.height : " << s.height << endl;
cout << " mat.rows : " << mat.rows << endl;
cout << " mat.cols : " << mat.cols << endl;
6. 打印结果:
7. 结论:
由第5步和第6步可得:
mat.size().width = mat.cols;
mat.size().height = mat.rows;
8. 动态创建二维数组:
例:创建一个4行3列的二维数组,如下:
{ { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }
即:{ { 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 }
// 创建一个二维数组中包含4个一维数组,即先定义4行
int **array = new int *[4];
// 循环二维数组,并将二维数组中的每个元素创建为包含3个元素的一维数组
// 先分配多少行,再每行分配多少列
for (int i = 0; i < 4; i ++){
array[i] = new int[3];
// 循环遍历二维数组,行作为外循环,列作为内循环,一行一行地遍历
for (int i = 0; i < 4; i ++){
for (int j = 0; j < 3; j ++){
array[i][j] = 0;
cout << " " << array[i][j] ;
cout << endl;
// 使用完请求分配的数值需要释放分配空间(内存)
// 释放分配空间,一行一行的删除
for (int i = 0; i < 4; i ++){
delete []array[i];
delete [] array;
9. 使用Mat图像的宽度和高度进行动态创建二维数组,Height(row)代表具有多少行,width(col)代表具有多少列。
// 创建一个二维数组,height(row)行width(col)列
int **La = new int *[height];
for (int i = 0; i < height; i ++){
La[i] = new int[width];
// 循环将Mat中对应的值赋给La二维数组
for (int i = 0; i < row; i ++){
for (int j = 0; j < col; j ++){
La[i][j] = mat.at<uchar>(i, j);
//cout << " " << La[i][j] ;
//cout << endl;
// 释放分配空间
for (int i = 0; i < height; i ++){
delete []La[i];
delete [] La;
10. 创建一个和二维数组行列大小的Mat:
当知道二维数组的大小,需要将二维数组中的值赋给同样大小的Mat,使用Mat显示。
//创建一个Mat变量 height(row)行width(col)列
Mat temp = Mat(height, width, CV_8U, Scalar::all(0));
// 循环赋值
for (int i = 0; i < height; i ++){
for (int j = 0; j < width; j ++){
mat.at<uchar>(i, j) = La[i][j];