Mat对象
一、Mat对象与IplImage
-
自动分配内存(是面向对象的数据结构)
-
分为两个部分:头部与数据部分
二、Mat对象使用
- 常用:
void copyTo(Mat mat):完全拷贝到matvoid convertTo(Mat dst, int type);:(转化目标,转化模式)Mat clone():完全拷贝int channels():返回图像的通道数int depth()bool empty()uchar* ptr(i = 0)- 构造函数
Mat()-
Mat(int rows, int cols, int type)- 例:
Mat(3,3,CV_8UC3,Scalar(0,0,255));- 前两个数字分别表示长和宽
CV_8UC3:- 8表示每个通道占8位
- U表示无符号
- C表示char类型
- 3表示通道数目是3
- 例:
-
Mat(Size size, int type) -
使用cout输出Mat对象,会输出每一个像素点的RGB值
- 复制:
-
部分复制: 一般情况下指挥复制Mat对象的头和指针部分,不会复制其他部分
Mat A = imread("filepath"); Mat B(A);
-
完全复制:如果想把Mat对象的头部和数据部分一起复制,可以通过如下的两个API实现
Mat F = A.clone()Mat G; A.copyTo(G)
三、Mat定义数组
-
一般创建二维
-
通过create方式实现
Mat M;
M.create(4,3,CV_8UC2);
M = Scalar(127,127);
cout << "M = " << endl << M << endl;
uchar* firstRow = M.ptr<uchar>(0);
printf("%d",firstRow);
-
定义小数组(掩膜操作里使用过)
-
C++ Mat C = (Mat_<double>(3,3)<<0,-1,0,-1,5,-1,0,-1,0); cout << "C=" << endl << " " << C << endl << endl;
四、其他
Mat::eye():使对角线上都是1,其他地方为0Mat::zeros():全为0