1.主要内容
Mat对象与Ip|Image对象
Mat对象分为两个部分,头部和数据部分
Mat定义数组
2.Mat对象创建
cv::Mat::Mat构造函数
Mat M(2,2,CV_8UC3,Scalar(0,0,255))
其中前两个参数分别表示行(row)跟列(column),第三个CV_8UC3中的8表示每个通道占8位,U表示无符号,C表示Char类型,3表示通道数目是3,第四个参数是向量表示初始化每个像素是多少,向量长度对应通道数目一致
创建多维数组cv::Mat::create
int sz[3]={2,2,2};
Mat L(3,sz,CV_8UC1,Scalar::all(0));
cv::Mat::create实现
Mat M;
M.create(4,3,CV_8UC2);
M=Scalar(127,127);
cout<<"M="<<endl<<""<<M<<endl<<endl;
uchar*firstRow=M.ptr<uchar>(0);
printf("%d",*firstRow);
3.Mat对象构造函数与常用方法
Mat对象构造函数:
Mat()
Mat(int rows,int cols,int type)
Mat(Size size,int type)
Mat(int rows,int cols,int type,const Scalar &s)
Mat(Size size,int type,const Scalar &s)
Mat(int ndims,const int *sizes,int type)
Mat(int ndims,const int *sizes,int type,const Scalar *s)
常用方法:
void copyTo(Mat mat)
void convertTo(Mat dst,int type)
Mat clone()
int channels()
int depth()
bool empty()
uchar * ptr(i=0)
4. 输出当前图像的通道(channel)数
printf("input image channels: %d",src.channels());
printf("output image channels: %d",dst.channels());
5.得到第0行第一个像素的指针
const uchar*firstRow=dst.ptr<uchar>(0);
printf("first pixel value : %d",*firstRow);
6.获取当前对象的行和列
int cols=dst.cols;
int rows=dst.rows;
prinyf("rows: %d ,cols: %d \n",rows,cols);
7.创造一个3*3的图像,我们可以输出它的图像来查看或者以数字的形式输出它的像素值来查看
作用:可以验证一些算法,验证其准确性
Mat M(100,100,CV_8UC3,Scalar(0,0,255));
// cout<<"M="<<endl<<M<<endl;
8.Mat需要注意:
输出图像的内存是自动分配的
使用opencv的C++接口,不需要考虑内存分配问题
赋值操作和拷贝构造函数只会复制头部分
使用clone和copyTo两个函数实现数据的完全复制
9.定义小数组
Mat C=(Mat_<double>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);
cout<<"C="<<endl<<""<<C<<endl<<endl;
10. 把m2初始化为1个纯黑色的图片,大小类型一致
Mat m2=Mat::zeros(src.size(),src.type());
也可以更改大小和格式
Mat m2=Mat::zeros(2,2,CV_8UC1);
11.部分复制与完全复制
部分复制:一般情况下只会复制Mat对象的头和指针部分,不会复制数据部分
Mat A= imread(imgFilePath);
Mat B(A) // 只复制
完全复制:如果想把Mat对象的头部和数据部分一起复制,可以通过如下两个API实现
Mat F = A.clone(); 或 Mat G; A.copyTo(G);
课外扩展:
OpenCV中Mat的传值、传引用、clone()、copyto()
https://blog.csdn.net/liuxiao214/article/details/77870341
.Scalar()函数作用:为每一个通道中的像素赋值
https://blog.csdn.net/wwhhhhll/article/details/79748014
opencv 的基本数据结构
https://www.cnblogs.com/cvtoEyes/p/8502602.html
OpenCV3之——copyTo()函数的使用方法
https://blog.csdn.net/weixin_40710375/article/details/88419892