OpenCV学习笔记之Mat

迈不过友情╰ 2023-07-10 15:45 69阅读 0赞

一. Mat介绍

在2001年刚刚出现的时候,OpenCV基于 C 语言接口而建。为了在内存(memory)中存放图像,当时采用名为 IplImage 的C语言结构体,时至今日这仍出现在大多数的旧版教程和教学材料。但这种方法必须接受C语言所有的不足,这其中最大的不足要数手动内存管理,其依据是用户要为开辟和销毁内存负责。虽然对于小型的程序来说手动管理内存不是问题,但一旦代码开始变得越来越庞大,你需要越来越多地纠缠于这个问题,而不是着力解决你的开发目标。

幸运的是,C++出现了,并且带来类的概念,这给用户带来另外一个选择:自动的内存管理(不严谨地说)。这是一个好消息,如果C++完全兼容C的话,这个变化不会带来兼容性问题。为此,OpenCV在2.0版本中引入了一个新的C++接口,利用自动内存管理给出了解决问题的新方法。使用这个方法,你不需要纠结在管理内存上,而且你的代码会变得简洁(少写多得)。但C++接口唯一的不足是当前许多嵌入式开发系统只支持C语言。所以,当目标不是这种开发平台时,没有必要使用 方法(除非你是自找麻烦的受虐狂码农)。

关于 Mat ,首先要知道的是你不必再手动地(1)为其开辟空间(2)在不需要时立即将空间释放。但手动地做还是可以的:大多数OpenCV函数仍会手动地为输出数据开辟空间。当传递一个已经存在的 Mat 对象时,开辟好的矩阵空间会被重用。也就是说,我们每次都使用大小正好的内存来完成任务。

基本上讲 Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针。矩阵头的尺寸是常数值,但矩阵本身的尺寸会依图像的不同而不同,通常比矩阵头的尺寸大数个数量级。因此,当在程序中传递图像并创建拷贝时,大的开销是由矩阵造成的,而不是信息头。OpenCV是一个图像处理库,囊括了大量的图像处理函数,为了解决问题通常要使用库中的多个函数,因此在函数中传递图像是家常便饭。同时不要忘了我们正在讨论的是计算量很大的图像处理算法,因此,除非万不得已,我们不应该拷贝 的图像,因为这会降低程序速度。

为了搞定这个问题,OpenCV使用引用计数机制。其思路是让每个 Mat 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则 只拷贝信息头和矩阵指针 ,而不拷贝矩阵。

  1. #include <QCoreApplication>
  2. #include <iostream>
  3. #include <opencv2/core.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <QDebug>
  6. #include <QDir>
  7. #include <QFile>
  8. using namespace std;
  9. using namespace cv;
  10. int main()
  11. {
  12. Mat A, C; // 只创建信息头部分
  13. A = imread("C:/1.png", IMREAD_COLOR); // 这里为矩阵开辟内存
  14. Mat B(A); // 使用拷贝构造函数
  15. C = A; // 赋值运算符
  16. imshow("A",A);
  17. imshow("B",B);
  18. waitKey(0);
  19. }

以上代码中的所有Mat对象最终都指向同一个也是唯一一个数据矩阵。虽然它们的信息头不同,但通过任何一个对象所做的改变也会影响其它对象。实际上,不同的对象只是访问相同数据的不同途径而已。这里还要提及一个比较棒的功能:你可以创建只引用部分数据的信息头。比如想要创建一个感兴趣区域( ROI ),你只需要创建包含边界信息的信息头:

  1. #include <QCoreApplication>
  2. #include <iostream>
  3. #include <opencv2/core.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <QDebug>
  6. #include <QDir>
  7. #include <QFile>
  8. using namespace std;
  9. using namespace cv;
  10. int main()
  11. {
  12. Mat A, C; // 只创建信息头部分
  13. A = imread("C:/1.png", IMREAD_COLOR); // 这里为矩阵开辟内存
  14. Mat B(A); // 使用拷贝构造函数
  15. C = A; // 赋值运算符
  16. Mat D(A, Rect(10, 10, 100, 100) ); // using a rectangle
  17. Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
  18. imshow("A",A);
  19. imshow("B",B);
  20. imshow("C",C);
  21. imshow("D",D);
  22. imshow("E",E);
  23. waitKey(0);
  24. }

现在你也许会问,如果矩阵属于多个 Mat 对象,那么当不再需要它时谁来负责清理?简单的回答是:最后一个使用它的对象。通过引用计数机制来实现。无论什么时候有人拷贝了一个 Mat 对象的信息头,都会增加矩阵的引用次数;反之当一个头被释放之后,这个计数被减一;当计数值为零,矩阵会被清理。但某些时候你仍会想拷贝矩阵本身(不只是信息头和矩阵指针),这时可以使用函数 clone() 或者 copyTo() 。

  1. #include <QCoreApplication>
  2. #include <iostream>
  3. #include <opencv2/core.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <QDebug>
  6. #include <QDir>
  7. #include <QFile>
  8. using namespace std;
  9. using namespace cv;
  10. int main()
  11. {
  12. Mat A, C; // 只创建信息头部分
  13. A = imread("C:/1.png", IMREAD_COLOR); // 这里为矩阵开辟内存
  14. Mat B(A); // 使用拷贝构造函数
  15. C = A; // 赋值运算符
  16. Mat D(A, Rect(10, 10, 100, 100) ); // using a rectangle
  17. Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
  18. Mat F = A.clone();
  19. Mat G;
  20. A.copyTo(G);
  21. imshow("A",A);
  22. imshow("B",B);
  23. imshow("C",C);
  24. imshow("D",D);
  25. imshow("E",E);
  26. imshow("F",F);
  27. imshow("G",G);
  28. waitKey(0);
  29. }

现在改变 F 或者 G 就不会影响 Mat 信息头所指向的矩阵。总结一下,你需要记住的是

  • OpenCV函数中输出图像的内存分配是自动完成的(如果不特别指定的话)。
  • 使用OpenCV的C++接口时不需要考虑内存释放问题。
  • 赋值运算符和拷贝构造函数( ctor )只拷贝信息头。
  • 使用函数 clone() 或者 copyTo() 来拷贝一副图像的矩阵。

二、Mat矩阵创建时参数详解

Mat不但是一个非常有用的图像容器类,同时也是一个通用的矩阵类,创建一个Mat对象的方法很多,我们现在先看一下Mat矩阵/图像容器类在OpenCv中的有关源代码:。

  1. //! default constructor
  2. Mat();
  3. //! constructs 2D matrix of the specified size and type
  4. // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
  5. Mat(int rows, int cols, int type);
  6. Mat(Size size, int type);
  7. //! constucts 2D matrix and fills it with the specified value _s.
  8. Mat(int rows, int cols, int type, const Scalar& s);
  9. Mat(Size size, int type, const Scalar& s);
  10. //! constructs n-dimensional matrix
  11. Mat(int ndims, const int* sizes, int type);
  12. Mat(int ndims, const int* sizes, int type, const Scalar& s);
  13. //! Matlab-style matrix initialization
  14. static MatExpr zeros(int rows, int cols, int type);
  15. static MatExpr zeros(Size size, int type);
  16. static MatExpr zeros(int ndims, const int* sz, int type);
  17. static MatExpr ones(int rows, int cols, int type);
  18. static MatExpr ones(Size size, int type);
  19. static MatExpr ones(int ndims, const int* sz, int type);
  20. static MatExpr eye(int rows, int cols, int type);
  21. static MatExpr eye(Size size, int type);

举例:

  1. Mat H(320,640,CV_8UC3,Scalar(255,255,255));

针对OpenCV创建矩阵时的type(CV_8UC3),其他格式在OpenCV里详细列举如下:

  1. #define CV_8U 0
  2. #define CV_8S 1
  3. #define CV_16U 2
  4. #define CV_16S 3
  5. #define CV_32S 4
  6. #define CV_32F 5
  7. #define CV_64F 6
  8. #define CV_16F 7
  9. #define CV_MAT_DEPTH_MASK (CV_DEPTH_MAX - 1)
  10. #define CV_MAT_DEPTH(flags) ((flags) & CV_MAT_DEPTH_MASK)
  11. #define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))
  12. #define CV_MAKE_TYPE CV_MAKETYPE
  13. #define CV_8UC1 CV_MAKETYPE(CV_8U,1)
  14. #define CV_8UC2 CV_MAKETYPE(CV_8U,2)
  15. #define CV_8UC3 CV_MAKETYPE(CV_8U,3)
  16. #define CV_8UC4 CV_MAKETYPE(CV_8U,4)
  17. #define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))
  18. #define CV_8SC1 CV_MAKETYPE(CV_8S,1)
  19. #define CV_8SC2 CV_MAKETYPE(CV_8S,2)
  20. #define CV_8SC3 CV_MAKETYPE(CV_8S,3)
  21. #define CV_8SC4 CV_MAKETYPE(CV_8S,4)
  22. #define CV_8SC(n) CV_MAKETYPE(CV_8S,(n))
  23. #define CV_16UC1 CV_MAKETYPE(CV_16U,1)
  24. #define CV_16UC2 CV_MAKETYPE(CV_16U,2)
  25. #define CV_16UC3 CV_MAKETYPE(CV_16U,3)
  26. #define CV_16UC4 CV_MAKETYPE(CV_16U,4)
  27. #define CV_16UC(n) CV_MAKETYPE(CV_16U,(n))
  28. #define CV_16SC1 CV_MAKETYPE(CV_16S,1)
  29. #define CV_16SC2 CV_MAKETYPE(CV_16S,2)
  30. #define CV_16SC3 CV_MAKETYPE(CV_16S,3)
  31. #define CV_16SC4 CV_MAKETYPE(CV_16S,4)
  32. #define CV_16SC(n) CV_MAKETYPE(CV_16S,(n))
  33. #define CV_32SC1 CV_MAKETYPE(CV_32S,1)
  34. #define CV_32SC2 CV_MAKETYPE(CV_32S,2)
  35. #define CV_32SC3 CV_MAKETYPE(CV_32S,3)
  36. #define CV_32SC4 CV_MAKETYPE(CV_32S,4)
  37. #define CV_32SC(n) CV_MAKETYPE(CV_32S,(n))
  38. #define CV_32FC1 CV_MAKETYPE(CV_32F,1)
  39. #define CV_32FC2 CV_MAKETYPE(CV_32F,2)
  40. #define CV_32FC3 CV_MAKETYPE(CV_32F,3)
  41. #define CV_32FC4 CV_MAKETYPE(CV_32F,4)
  42. #define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))
  43. #define CV_64FC1 CV_MAKETYPE(CV_64F,1)
  44. #define CV_64FC2 CV_MAKETYPE(CV_64F,2)
  45. #define CV_64FC3 CV_MAKETYPE(CV_64F,3)
  46. #define CV_64FC4 CV_MAKETYPE(CV_64F,4)
  47. #define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))
  48. #define CV_16FC1 CV_MAKETYPE(CV_16F,1)
  49. #define CV_16FC2 CV_MAKETYPE(CV_16F,2)
  50. #define CV_16FC3 CV_MAKETYPE(CV_16F,3)
  51. #define CV_16FC4 CV_MAKETYPE(CV_16F,4)
  52. #define CV_16FC(n) CV_MAKETYPE(CV_16F,(n))
  53. 这里的type可以是任何的预定义类型,预定义类型的结构如下所示:
  54. CV_<bit_depth>(S|U|F)C<number_of_channels>
  55. 1--bit_depth---比特数---代表8bite,16bites,32bites,64bites-
  56. 2--S|U|F--S--代表---signed int---有符号整形
  57. U--代表--unsigned int--无符号整形
  58. F--代表--float---------单精度浮点型
  59. 3--C<number_of_channels>----代表---一张图片的通道数

显示创建Mat矩阵

  1. #include <QCoreApplication>
  2. #include <iostream>
  3. #include <opencv2/core.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <QDebug>
  6. #include <QDir>
  7. #include <QFile>
  8. using namespace std;
  9. using namespace cv;
  10. int main()
  11. {
  12. Mat M(2,2, CV_8UC3, Scalar(0,0,255));
  13. cout << "M = " << endl << " " << M << endl << endl;
  14. M.create(4,4, CV_8UC(2));
  15. cout << "M = "<< endl << " " << M << endl << endl;
  16. Mat E = Mat::eye(4, 4, CV_64F);
  17. cout << "E = " << endl << " " << E << endl << endl;
  18. Mat O = Mat::ones(2, 2, CV_32F);
  19. cout << "O = " << endl << " " << O << endl << endl;
  20. Mat Z = Mat::zeros(3,3, CV_8UC1);
  21. cout << "Z = " << endl << " " << Z << endl << endl;
  22. Mat RowClone = Z.row(1).clone();
  23. cout << "RowClone = " << endl << " " << RowClone << endl << endl;
  24. waitKey(0);
  25. }
  26. 输出:
  27. M =
  28. [ 0, 0, 255, 0, 0, 255;
  29. 0, 0, 255, 0, 0, 255]
  30. M =
  31. [205, 205, 205, 205, 205, 205, 205, 205;
  32. 205, 205, 205, 205, 205, 205, 205, 205;
  33. 205, 205, 205, 205, 205, 205, 205, 205;
  34. 205, 205, 205, 205, 205, 205, 205, 205]
  35. E =
  36. [1, 0, 0, 0;
  37. 0, 1, 0, 0;
  38. 0, 0, 1, 0;
  39. 0, 0, 0, 1]
  40. O =
  41. [1, 1;
  42. 1, 1]
  43. Z =
  44. [ 0, 0, 0;
  45. 0, 0, 0;
  46. 0, 0, 0]
  47. RowClone =
  48. [ 0, 0, 0]

在已知通道数和每个通道数据类型的情况下,指定给 at 方法的数据类型如下表所示:




















































类型 C1 C2 C3 C4 C6
uchar uchar cv::Vec2b cv::Vec3b cv::Vec4b  
short short cv::Vec2s cv::Vec3s cv::Vec4b  
int int cv::Vec2i cv::Vec3i cv::Vec4i  
float float cv::Vec2f cv::Vec3f cv::Vec4f cv::Vec6f
double double cv::Vec2d cv::Vec3d cv::Vec4d cv::Vec6f
  1. #include <QCoreApplication>
  2. #include <iostream>
  3. #include <opencv2/core.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <QDebug>
  6. #include <QDir>
  7. #include <QFile>
  8. using namespace std;
  9. using namespace cv;
  10. int main()
  11. {
  12. Mat M(2,2, CV_8UC3, Scalar(0,0,255));
  13. Vec3b vec3b = M.at<Vec3b>(0,0);
  14. uchar vec3b0 = vec3b[0];
  15. uchar vec3b1 = vec3b[1];
  16. uchar vec3b2 = vec3b[2];
  17. qDebug()<<vec3b0<<vec3b1<<vec3b2;
  18. waitKey(0);
  19. }
  20. 输出:
  21. 0 0 255
  22. /** @name Shorter aliases for the most popular specializations of Vec<T,n>
  23. @{
  24. */
  25. typedef Vec<uchar, 2> Vec2b;
  26. typedef Vec<uchar, 3> Vec3b;
  27. typedef Vec<uchar, 4> Vec4b;
  28. typedef Vec<short, 2> Vec2s;
  29. typedef Vec<short, 3> Vec3s;
  30. typedef Vec<short, 4> Vec4s;
  31. typedef Vec<ushort, 2> Vec2w;
  32. typedef Vec<ushort, 3> Vec3w;
  33. typedef Vec<ushort, 4> Vec4w;
  34. typedef Vec<int, 2> Vec2i;
  35. typedef Vec<int, 3> Vec3i;
  36. typedef Vec<int, 4> Vec4i;
  37. typedef Vec<int, 6> Vec6i;
  38. typedef Vec<int, 8> Vec8i;
  39. typedef Vec<float, 2> Vec2f;
  40. typedef Vec<float, 3> Vec3f;
  41. typedef Vec<float, 4> Vec4f;
  42. typedef Vec<float, 6> Vec6f;
  43. typedef Vec<double, 2> Vec2d;
  44. typedef Vec<double, 3> Vec3d;
  45. typedef Vec<double, 4> Vec4d;
  46. typedef Vec<double, 6> Vec6d;
  47. #include <QCoreApplication>
  48. #include <iostream>
  49. #include <opencv2/core.hpp>
  50. #include <opencv2/highgui.hpp>
  51. #include <QDebug>
  52. #include <QDir>
  53. #include <QFile>
  54. using namespace std;
  55. using namespace cv;
  56. int main()
  57. {
  58. Mat M(2,2, CV_8UC1, Scalar(255));
  59. uchar vec1b = M.at<uchar>(0,0);
  60. qDebug()<<vec1b;
  61. waitKey(0);
  62. }

参考资料:

  1. Mat - 基本图像容器

  2. OpenCv学习笔记(二)—Mat矩阵(图像容器)的创建及CV_8UC1,CV_8UC2等参数详解

  3. opencv cv::Mat数据类型总结

发表评论

表情:
评论列表 (有 0 条评论,69人围观)

还没有评论,来说两句吧...

相关阅读