数字图像可看作一个数值矩阵, 其中每个元素代表一个像素点,如下:

OpenCV 中,用 Mat 表示数值矩阵,Mat 是很关键的一种数据结构,因为 OpenCV 中的大部分函数都和 Mat 有关:

有的是 Mat 的成员函数;有的把 Mat 作为参数;还有的将 Mat 作为返回值。

1  Mat 简介

Mat 表示的是 N 维稠密矩阵,与稠密矩阵相对的是稀疏矩阵(只存储非零的像素值),后者常用于直方图处理中,OpenCV 中对应为 cv::SparseMat

如下所示:第一个为稠密矩阵的存储方式,存储所有的像素数值;第二个为稀疏矩阵的存储方式,只存储非零的像素值

$\quad \begin{bmatrix} 0 & 2 & 0 \\ 1 & 0 & 1 \\ 0 & 2 & 0 \end{bmatrix} $        $\quad \begin{bmatrix}  & 2 &  \\ 1 &  & 1 \\  & 2 &  \end{bmatrix} $

当 N=1 时,所有像素存储为一行;当 N=2 时,所有像素按照一行行的顺序排列;当 N=3 时,所有像素按照一面面的顺序排列,其中一行行的像素构成一个平面。

下图左,为灰度图的存储方式;图右,为 RGB 图像的存储方式,注意其存储顺序为 BGR (Blue->Green->Red)

    

2  Mat 特点

2.1  组成

Mat 类包含两部分,一是 矩阵头 (matrix header),二是 矩阵指针 (pointer to matrix),部分矩阵头如下:

int  flags;  // signaling the contents of the matrix
int dims; // dimensions
int rows, cols; // rows and columns
MatSize size; //
MatStep step; //

矩阵指针如下,指向包含所有像素值的矩阵

uchar* data;  // pointer to the data

2.2  赋值算子

Mat 类中的赋值算子 "=" 和 拷贝构造函数,涉及的是浅拷贝,因此,当执行这两个操作时,仅仅是复制了矩阵头。

如果想要深拷贝,达到复制图像矩阵的目的,应使用 clone() 或 copyTo() 函数,如下图所示 (摘自参考资料 -- 4):

2.3  代码示例

简单验证如下,将矩阵 m3 由 copyTo() 函数复制给 m1,而 m2 由 m1 直接赋值,二者指向的是同样的数据。因此,如果改变了 m1,则 m2 对应的矩阵数值,也会进行相应的改变。

Mat m1(, , CV_32FC1, Scalar(1.1f) );
cout << "m1 = " << endl << " " << m1 << endl << endl;
// using assign operator
Mat m2 = m1;
cout << "m2 = " << endl << " " << m2 << endl << endl; Mat m3(, 3, CV_32FC1, Scalar(3.3f) );
m3.copyTo(m1);
cout << "m1 = " << endl << " " << m1 << endl << endl;
cout << "m2 = " << endl << " " << m2 << endl << endl;

3  Mat 创建

3.1  数据类型

在创建 Mat 之前,首先了解 Mat 中元素的数据类型,其格式为 CV_{8U, 16S, 16U, 32S, 32F, 64F}C{1, 2, 3} 或 CV_{8U, 16S, 16U, 32S, 32F, 64F}C(n)

第一个 {} 表示数据的类型:

CV_8U  - 8-bit 无符号整数 ( 0..255 )
CV_8S - 8-bit 有符号整数 ( -128..127 )
CV_16U - 16-bit 无符号整数 ( 0..65535 )
CV_16S - 16-bit 有符号整数 ( -32768..32767 )
CV_32S - 32-bit 有符号整数 ( -2147483648..2147483647 )
CV_32F - 32-bit 浮点数 ( -FLT_MAX..FLT_MAX, INF, NAN )
CV_64F - 64-bit 浮点数 ( -DBL_MAX..DBL_MAX, INF, NAN )

第二个 {} 或 (n),表示的是通道:

CV_8UC3 等价于 CV_8UC(3) - 3通道 8-bit 无符号整数

3.2  创建方式

3.2.1  构造函数

创建一个 3 行 5 列,3 通道 32 位,浮点型的矩阵,通道 1, 2, 3 的值分别为 1.1f,2.2f,3.3f

Mat m(, , CV_32FC3, Scalar(1.1f, 2.2f, 3.3f) );
cout << "m = " << endl << " " << m << endl << endl;

输出的矩阵如下:

3.2.2  create 函数

使用 Mat() + create() + setTo(),也可以构建如上的数值矩阵

Mat m;
// Create data area for 3 rows and 10 columns of 3-channel 32-bit floats
m.create(,,CV_32FC3);
// Set the values in the 1st channel to 1.0, the 2nd to 0.0, and the 3rd to 1.0
m.setTo(Scalar(1.1f, 2.2f,3.3f));
cout << "m = " << endl << " " << m << endl << endl;

3.2.3  特殊矩阵

单位矩阵 (ones),对角矩阵 (eye),零矩阵 (zeros),如下所示:

// 单位矩阵
Mat O = Mat::ones(, , CV_32F);
cout << "O = " << endl << " " << O << endl << endl;
// 零矩阵
Mat Z = Mat::zeros(, , CV_8UC1);
cout << "Z = " << endl << " " << Z << endl << endl;
// 对角矩阵
Mat E = Mat::eye(, , CV_64F);
cout << "E = " << endl << " " << E << endl << endl;

4  Mat 遍历

4.1  at<>() 函数

常用来遍历 Mat 元素的基本函数为 at<>(),其中 <> 内的数据类型,取决于 Mat 中元素的数据类型,二者的对应关系如下:

CV_8U  --  Mat.at<uchar>(y,x)
CV_8S -- Mat.at<schar>(y,x)
CV_16U -- Mat.at<ushort>(y,x)
CV_16S -- Mat.at<short>(y,x)
CV_32S -- Mat.at<int>(y,x)
CV_32F -- Mat.at<float>(y,x)
CV_64F -- Mat.at<double>(y,x)

简单的遍历如下,使用了 Qt 的 qDebug() 来显示输出

Mat m1 = Mat::eye(, , CV_32FC1);
// use qDebug()
qDebug() << "Element (3,3) is : " << m1.at<float>(,); Mat m2 = Mat::eye(, , CV_32FC2);
// use qDebug()
qDebug() << "Element (3,3) is " << m2.at<cv::Vec2f>(,)[] << "," << m2.at<cv::Vec2f>(,)[];

注意:at<>() 函数中 () 内,行索引号在前,列索引号在后,也即 (y, x)

4.2  遍历方式

4.2.1  高效遍历

Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols * channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = ;
}
int i,j;
uchar* p;
for(i=; i<nRows; ++i)
{
p = I.ptr<uchar>(i);
for (j = ; j<nCols; ++j)
{
p[j] = table[p[j]];
}
}
return I;
}

4.2.2  迭代器遍历

Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
const int channels = I.channels();
switch(channels)
{
case :
{
MatIterator_<uchar> it, end;
for(it=I.begin<uchar>(), end=I.end<uchar>(); it!=end; ++it)
*it = table[*it];
break;
}
case :
{
MatIterator_<Vec3b> it, end;
for(it=I.begin<Vec3b>(), end=I.end<Vec3b>(); it!=end; ++it)
{
(*it)[] = table[(*it)[]];
(*it)[] = table[(*it)[]];
(*it)[] = table[(*it)[]];
}
}
}
return I;
}

4.2.3  耗时计算

比较上面两种方法的耗时,可使用如下代码来进行计算:

double t = (double)getTickCount();
// do something ...
t = ((double)getTickCount() - t)/getTickFrequency();
qDebug() << "Times passed in seconds: " << t << endl; // using qDebug()

参考资料:

1.  <Learning OpenCV3> chapter 4

2.  OpenCV Tutorials / The Core Functionality (core module) / Mat - The Basic Image Container

3.  OpenCV Tutorials / The Core Functionality (core module) / How to scan images, lookup tables and time measurement with OpenCV

4.  OpenCV基础篇之Mat数据结构

最新文章

  1. Android Weekly Notes Issue #222
  2. js判断本地是否安装app
  3. nw.js如何处理拖放操作
  4. 【追寻javascript高手之路04】理解prototype
  5. 爬虫3 html解析器 html_parser.py
  6. 小菜鸟学 Spring-Dependency injection(二)
  7. 乌龟棋(noip2010)
  8. PHP 解决时差8小时的问题
  9. MySQL percona-toolkit工具包的使用教程
  10. 经验28--相关时间戳,C#
  11. spring boot访问数据库
  12. Unable to access the IIS metabase.You do not have sufficient privilege
  13. CentOS搭建GIT服务器
  14. javascript中Math函数的属性与方法
  15. 开始记录 Windows Phone 生涯
  16. 转载:Nginx是什么(1.1)《深入理解Nginx》(陶辉)
  17. Component 初识组件
  18. Samsung_tiny4412(驱动笔记06)----list_head,proc file system,GPIO,ioremap
  19. eclipse maven插件创建maven项目
  20. 把数组存入到cookie中

热门文章

  1. 并归排序 (Java版本,时间复杂度为O(n))
  2. 开源Inno Setup官网下载、安装、打包教程(官网安装向导中文语言包)
  3. win7系统中使用DOS命令是出现乱码的解决方法
  4. 数据库外连接及MySQL实现
  5. Pycharm创建的virtualenv环境缺失pip.exe的问题(Windows系统)
  6. 【LeetCode】219. Contains Duplicate II
  7. 6.如何使用jedis的线程池
  8. [图形学] Chp10 OpenGL三维观察程序示例
  9. HDOJ 2209 贪心
  10. CSS3-loading动画(一)