计算机视觉_OpenCV开源库讲解(第一节:Mat矩阵)

Mat矩阵详解:

Mat类可以被看做是opencv中C++版本的矩阵类,替代原来C版本的矩阵结构体CvMat和图像结构体IplImage;

Mat最大的优势跟STL的兼容性很好,有很多类似于STL的操作。但是Mat远远强于后者;

Mat是一种高效的数据类型,它对内存进行动态的管理,不需要之前用户手动的管理内存。

Mat定义如下:

class CV_EXPORTS Mat
{
public:
// … a lot of methods …

/*! includes several bit-fields:
     - the magic signature
     - continuity flag
     - depth
     - number of channels
 */
int flags;
//! the array dimensionality, >= 2
int dims;
//! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions
int rows, cols;
//! pointer to the data
uchar* data;

//! pointer to the reference counter;
// when array points to user-allocated data, the pointer is NULL
int* refcount;

// other members
...

};
Mat类可以分为两个部分:矩阵头指向像素数据的矩阵指针
Mat矩阵构造函数有很多,如下:
在这里插入图片描述
1,dims:矩阵的维数
2,cahnnels:矩阵的通道数,如彩色图像是3通道,灰度图像为单通道
3,row:矩阵的行数,col矩阵的列数
4,step:为矩阵元素寻址

另外,矩阵的API还有:
1,Mat::t,//转置矩阵
2,Mat::inv, //反转矩阵
3,Mat::mul,//执行两个矩阵按元素相乘或这两个矩阵的除法
4,Mat::cross, //计算3元素向量的一个叉乘积
5,Mat::dot, //计算两向量的点乘
6,Mat::zeros, //返回指定的大小和类型的零数组
7,Mat::ones, //返回一个指定的大小和类型的全为1的数组
8,Mat::eye, //返回一个恒等指定大小和类型矩阵
如: Mat A = Mat::eye(4, 4, CV_32F)*0.1; / / 创建4 x 4 的对角矩阵并在对角线上以0.1的比率缩小。
9,Mat::create, //分配新的阵列数据
10,Mat::resize, //更改矩阵的行数
11,Mat::total,//返回数组元素的总数
12,Mat::isContinuous,//返回矩阵是否连续
13,Mat::elemSize, //返回矩阵元素大小 (以字节为单位)
14,Mat::elemSize1, //以字节为单位返回每个矩阵元素通道的大小
15,Mat::type,//返回一个矩阵元素的类型
16,Mat::depth, //返回一个矩阵元素的深度
17,Mat::channels, //返回矩阵通道的数目
18,Mat::step1, // 返回矩阵归一化迈出的一步
19,Mat::size,//为矩阵的大小
20,Mat::empty, //如果数组有没有 elemens,则返回 true
21,Mat::ptr, //返回指定矩阵行的指针
22,Mat::at, //返回对指定数组元素的引用
23,Mat::begin, //返回矩阵迭代器,并将其设置为第一矩阵元
24,Mat::end, //返回矩阵迭代器,并将其设置为 最后元素之后(after-last)的矩阵元

Mat操作像素:
(1)基于Mat对象的随机像素访问API实现,通过行列索引方式遍历每个像素值
在这里插入图片描述
(2)基于Mat对象的行随机访问指针方式实现对每个像素的遍历
在这里插入图片描述

(3)直接获取Mat对象的像素块的数据指针,基于指针操作,实现快速像素方法
在这里插入图片描述

实践证明,第三种方法速度最快。

发布了18 篇原创文章 · 获赞 12 · 访问量 429

猜你喜欢

转载自blog.csdn.net/qq_41408585/article/details/103900947