OpenCv学习笔记(九)再谈OpenCv中Mat类源码的详细解读

本文详细解析了 OpenCV 中核心图像容器类 Mat 的构造方法、属性及常用操作,包括矩阵的创建、复制、转换等,适合初学者快速掌握 Mat 类的基本使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. /*********************************************************************************************************************************** 
  2. 文件说明: 
  3.         1)OpenCv2.X系列,cv::Mat类源码解读 
  4.         2)相似的数据类型:OpenCv1.x中的IplImage,CvMat 
  5. ************************************************************************************************************************************/  
  6.   
  7. class CV_EXPORTS Mat  
  8. {  
  9. public:  
  10.     /***************************************************************************************************************************** 
  11.     模块说明: 
  12.              基础图像容器类Mat类的----构造函数 
  13.     具体说明: 
  14.              1)我们可以通过图像矩阵容器类Mat的构造函数,在实例化Mat类的类对象的时候,初始化相关的图像属性 
  15.              2)可以显式的使用Mat类的构造函数创建Mat类的类对象 
  16.     ******************************************************************************************************************************/  
  17.     Mat();                                      //【1】图像容器类Mat的默认构造函数  
  18.     Mat(int rows, int cols, int type);         //【2】有参构造函数,实例化一个二维的图像容器类,指定Mat的行数和列数,以及存储元素的数据  
  19.                                                 //类型和图像的通道数;具体的图像像素点数据元素类型和通道数有:CV_8UC1、CV_64FC3、CV_32SC  
  20.     Mat(Size size, int type);                   //【3】同上,只不过这个是通过Size类来指定图像的高度和宽度(rows*cols)  
  21.                                                 //【4】构造一个指定大小(高*宽==rows*cols),指定类型(每一个像素点的数据类型和图像通道数),  
  22.                                                 //     并且使用指定数据初始化的图像矩阵  
  23.     Mat(int rows, int cols, int type, const Scalar& s);  
  24.     Mat(Size size, int type, const Scalar& s); //【5】同上  
  25.     Mat(int ndims, const int* sizes, int type);//【6】构造一个指定大小和指定类型的N维图像矩阵  
  26.                                                  //【7】构造一个指定大小,指定类型,并且对矩阵使用指定数值进行初始化的N维矩阵  
  27.     Mat(int ndims, const int* sizes, int type, const Scalar& s);  
  28.     Mat(const Mat& m);                          //【8】复制构造函数  
  29.                                                  //【9】为用户使用的矩阵体构造一个矩阵头  
  30.     Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);  
  31.                                                  //【10】构造矩阵头----create a matrix header  
  32.     Mat(Size size, int type, void* data, size_t step=AUTO_STEP);  
  33.                                                  //【11】创建矩阵头  
  34.     Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);  
  35.                                                  //【12】为一个大矩阵体的一部分创建一个矩阵头  
  36.     Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());  
  37.     Mat(const Mat& m, const Rect& roi);  
  38.     Mat(const Mat& m, const Range* ranges);  
  39.                                                  //【13】将旧式风格的矩阵CvMat*转换为一个新式的矩阵Mat,但是默认的并没有拷贝矩阵体  
  40.     Mat(const CvMat* m, bool copyData=false);  
  41.                                                  //【14】将旧式风格的N维矩阵CvMatND*转换为新式矩阵Mat,矩阵体没有复制  
  42.     Mat(const CvMatND* m, bool copyData=false);  
  43.                                                  //【15】将旧式风格的图像容器IplImage*转换为Mat类型,矩阵体没有被复制  
  44.     Mat(const IplImage* img, bool copyData=false);  
  45.                                                   //【16】从std::vector构件矩阵,矩阵体被复制或者没有被复制  
  46.     template<typename _Tp> explicit Mat(const vector<_Tp>& vec, bool copyData=false);  
  47.                                                   //【17】从cv::Vec构件矩阵,矩阵体被默认的一起复制过来  
  48.     template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true);  
  49.                                                   //【18】从cv::Matx构件矩阵,矩阵体被默认的一起复制过来  
  50.     template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true);  
  51.                                                   //【19】从二维点构建矩阵  
  52.     template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);  
  53.                                                   //【20】从三维点构建矩阵  
  54.     template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true);  
  55.                                                   //【21】builds matrix from comma initializer  
  56.     template<typename _Tp> explicit Mat(const MatCommaInitializer_<_Tp>& commaInitializer);  
  57.                                                   //【22】从GpuMat下载数据  
  58.     explicit Mat(const gpu::GpuMat& m);            
  59.                                                   //【23】图像容器类Mat的析构函数  
  60.     ~Mat();  
  61.     Mat& operator = (const Mat& m);  
  62.     Mat& operator = (const MatExpr& expr);  
  63.   
  64.     //! returns a new matrix header for the specified row  
  65.     Mat row(int y) const;  
  66.     //! returns a new matrix header for the specified column  
  67.     Mat col(int x) const;  
  68.     //! ... for the specified row span  
  69.     Mat rowRange(int startrow, int endrow) const;  
  70.     Mat rowRange(const Range& r) const;  
  71.     //! ... for the specified column span  
  72.     Mat colRange(int startcol, int endcol) const;  
  73.     Mat colRange(const Range& r) const;  
  74.     //! ... for the specified diagonal  
  75.     // (d=0 - the main diagonal,  
  76.     //  >0 - a diagonal from the lower half,  
  77.     //  <0 - a diagonal from the upper half)  
  78.     Mat diag(int d=0) const;  
  79.     //! constructs a square diagonal matrix which main diagonal is vector "d"  
  80.     static Mat diag(const Mat& d);  
  81.                                                     //【24】返回一个深复制的矩阵  
  82.     Mat clone() const;                                
  83.                                                     //【25】复制一个矩阵到m,调用之前先调用m.create(this->size(), this->type())  
  84.     void copyTo( OutputArray m ) const;  
  85.                                                     //【26】复制复制矩阵的元素到m,并且这些m矩阵的原始的元素被非零的掩膜元素标记  
  86.     void copyTo( OutputArray m, InputArray mask ) const;  
  87.                                                     //【27】将矩阵转换为另一种数据类型的矩阵(图像元素的数据类型可选)  
  88.     void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;  
  89.     void assignTo( Mat& m, int type=-1 ) const;  
  90.                                                     //【28】将每一个矩阵的元素设置为s  
  91.     Mat& operator = (const Scalar& s);  
  92.     //! sets some of the matrix elements to s, according to the mask  
  93.     Mat& setTo(InputArray value, InputArray mask=noArray());  
  94.     //! creates alternative matrix header for the same data, with different  
  95.     // number of channels and/or different number of rows. see cvReshape.  
  96.     Mat reshape(int cn, int rows=0) const;  
  97.     Mat reshape(int cn, int newndims, const int* newsz) const;  
  98.   
  99.     //! matrix transposition by means of matrix expressions  
  100.     MatExpr t() const;  
  101.     //! matrix inversion by means of matrix expressions  
  102.     MatExpr inv(int method=DECOMP_LU) const;  
  103.     //! per-element matrix multiplication by means of matrix expressions  
  104.     MatExpr mul(InputArray m, double scale=1) const;  
  105.   
  106.     //! computes cross-product of 2 3D vectors  
  107.     Mat cross(InputArray m) const;  
  108.     //! computes dot-product  
  109.     double dot(InputArray m) const;  
  110.     /***************************************************************************************************************************** 
  111.     模块说明: 
  112.              Matlab风格,矩阵的初始化 
  113.     ******************************************************************************************************************************/  
  114.                                                               //【29】实例化一个指定大小,指定类型、初始值为0的矩阵对象  
  115.     static MatExpr zeros(int rows, int cols, int type);          
  116.     static MatExpr zeros(Size size, int type);  
  117.     static MatExpr zeros(int ndims, const int* sz, int type);  
  118.                                                               //【30】实例化一个指定大小,指定类型、初始值为1的矩阵  
  119.     static MatExpr ones(int rows, int cols, int type);  
  120.     static MatExpr ones(Size size, int type);  
  121.     static MatExpr ones(int ndims, const int* sz, int type);  
  122.                                                                //【31】实例化一个指定大小,指定类型的单位矩阵  
  123.     static MatExpr eye(int rows, int cols, int type);  
  124.     static MatExpr eye(Size size, int type);  
  125.     /***************************************************************************************************************************** 
  126.     模块说明: 
  127.              创建一个新的矩阵体 
  128.     ******************************************************************************************************************************/                                                             
  129.     void create(int rows, int cols, int type);  
  130.     void create(Size size, int type);  
  131.     void create(int ndims, const int* sizes, int type);  
  132.   
  133.     //! increases the reference counter; use with care to avoid memleaks  
  134.     void addref();  
  135.     //! decreases reference counter;  
  136.     // deallocates the data when reference counter reaches 0.  
  137.     void release();  
  138.   
  139.     //! deallocates the matrix data  
  140.     void deallocate();  
  141.     //! internal use function; properly re-allocates _size, _step arrays  
  142.     void copySize(const Mat& m);  
  143.   
  144.     //! reserves enough space to fit sz hyper-planes  
  145.     void reserve(size_t sz);  
  146.     //! resizes matrix to the specified number of hyper-planes  
  147.     void resize(size_t sz);  
  148.     //! resizes matrix to the specified number of hyper-planes; initializes the newly added elements  
  149.     void resize(size_t sz, const Scalar& s);  
  150.     //! internal function  
  151.     void push_back_(const void* elem);  
  152.     //! adds element to the end of 1d matrix (or possibly multiple elements when _Tp=Mat)  
  153.     template<typename _Tp> void push_back(const _Tp& elem);  
  154.     template<typename _Tp> void push_back(const Mat_<_Tp>& elem);  
  155.     void push_back(const Mat& m);  
  156.     //! removes several hyper-planes from bottom of the matrix  
  157.     void pop_back(size_t nelems=1);  
  158.   
  159.     //! locates matrix header within a parent matrix. See below  
  160.     void locateROI( Size& wholeSize, Point& ofs ) const;  
  161.     //! moves/resizes the current matrix ROI inside the parent matrix.  
  162.     Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );  
  163.     //! extracts a rectangular sub-matrix  
  164.     // (this is a generalized form of row, rowRange etc.)  
  165.     Mat operator()( Range rowRange, Range colRange ) const;  
  166.     Mat operator()( const Rect& roi ) const;  
  167.     Mat operator()( const Range* ranges ) const;  
  168.   
  169.     //! converts header to CvMat; no data is copied  
  170.                                                                //【32】将Mat的矩阵头转换为CvMat类型  
  171.     operator CvMat() const;                   
  172.     operator CvMatND() const;  
  173.                                                                //【33】将Mat的矩阵头转换为IplImage类型  
  174.     operator IplImage() const;  
  175.   
  176.     template<typename _Tp> operator vector<_Tp>() const;  
  177.     template<typename _Tp, int n> operator Vec<_Tp, n>() const;  
  178.     template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;  
  179.   
  180.     //! returns true iff the matrix data is continuous  
  181.     // (i.e. when there are no gaps between successive rows).  
  182.     // similar to CV_IS_MAT_CONT(cvmat->type)  
  183.     bool isContinuous() const;  
  184.   
  185.     //! returns true if the matrix is a submatrix of another matrix  
  186.     bool isSubmatrix() const;  
  187.   
  188.     //! returns element size in bytes,  
  189.     // similar to CV_ELEM_SIZE(cvmat->type)  
  190.                                                                  //【34】返回图像中,每一个图像元素的大小(单位是字节,比如,int---4)  
  191.     size_t elemSize() const;  
  192.     //! returns the size of element channel in bytes.  
  193.     size_t elemSize1() const;  
  194.                                                                  //【35】返回元素的类型,类似CV_MAT_TYPE()  
  195.     int type() const;  
  196.                                                                  //【36】返回元素的深度,类似CV_MAT_DEPTH()  
  197.     int depth() const;  
  198.                                                                  //【37】返回元素的通道数,类此CV_MAT_CN()  
  199.     int channels() const;  
  200.                                                                  //【38】返回图像中每一行指针移动的个数step/elemSize1()  
  201.     size_t step1(int i=0) const;   
  202.                                                                  //【39】判断矩阵是否为空,如果为空,则返回True  
  203.     bool empty() const;  
  204.                                                                  //【40】返回矩阵图像中,元素点的个数  
  205.     size_t total() const;  
  206.   
  207.     //! returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise  
  208.     int checkVector(int elemChannels, int depth=-1, bool requireContinuous=trueconst;  
  209.                                                                  //【41】返回图像矩阵中,指定行的首地址  
  210.     uchar* ptr(int i0=0);  
  211.     const uchar* ptr(int i0=0) const;  
  212.                                                                  //【42】返回矩阵图像中,指定点的首地址  
  213.     uchar* ptr(int i0, int i1);  
  214.     const uchar* ptr(int i0, int i1) const;  
  215.     uchar* ptr(int i0, int i1, int i2);  
  216.     const uchar* ptr(int i0, int i1, int i2) const;  
  217.   
  218.     //! returns pointer to the matrix element  
  219.     uchar* ptr(const int* idx);  
  220.     //! returns read-only pointer to the matrix element  
  221.     const uchar* ptr(const int* idx) const;  
  222.   
  223.     template<int n> uchar* ptr(const Vec<int, n>& idx);  
  224.     template<int n> const uchar* ptr(const Vec<int, n>& idx) const;  
  225.   
  226.     //! template version of the above method  
  227.     template<typename _Tp> _Tp* ptr(int i0=0);  
  228.     template<typename _Tp> const _Tp* ptr(int i0=0) const;  
  229.   
  230.     template<typename _Tp> _Tp* ptr(int i0, int i1);  
  231.     template<typename _Tp> const _Tp* ptr(int i0, int i1) const;  
  232.   
  233.     template<typename _Tp> _Tp* ptr(int i0, int i1, int i2);  
  234.     template<typename _Tp> const _Tp* ptr(int i0, int i1, int i2) const;  
  235.   
  236.     template<typename _Tp> _Tp* ptr(const int* idx);  
  237.     template<typename _Tp> const _Tp* ptr(const int* idx) const;  
  238.   
  239.     template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);  
  240.     template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;  
  241.   
  242.     /***************************************************************************************************************************** 
  243.     模块说明: 
  244.              返回图像矩阵中指定元素的地址(引用) 
  245.     ******************************************************************************************************************************/   
  246.     template<typename _Tp> _Tp& at(int i0=0);  
  247.     template<typename _Tp> const _Tp& at(int i0=0) const;  
  248.   
  249.     template<typename _Tp> _Tp& at(int i0, int i1);  
  250.     template<typename _Tp> const _Tp& at(int i0, int i1) const;  
  251.   
  252.     template<typename _Tp> _Tp& at(int i0, int i1, int i2);  
  253.     template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;  
  254.   
  255.     template<typename _Tp> _Tp& at(const int* idx);  
  256.     template<typename _Tp> const _Tp& at(const int* idx) const;  
  257.   
  258.     template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);  
  259.     template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;  
  260.   
  261.     //! special versions for 2D arrays (especially convenient for referencing image pixels)  
  262.     template<typename _Tp> _Tp& at(Point pt);  
  263.     template<typename _Tp> const _Tp& at(Point pt) const;  
  264.   
  265.     //! template methods for iteration over matrix elements.  
  266.     // the iterators take care of skipping gaps in the end of rows (if any)  
  267.     template<typename _Tp> MatIterator_<_Tp> begin();  
  268.     template<typename _Tp> MatIterator_<_Tp> end();  
  269.     template<typename _Tp> MatConstIterator_<_Tp> begin() const;  
  270.     template<typename _Tp> MatConstIterator_<_Tp> end() const;  
  271.   
  272.     enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG, SUBMATRIX_FLAG=CV_SUBMAT_FLAG };  
  273.     /***************************************************************************************************************************** 
  274.     模块说明: 
  275.              图像矩阵的属性 
  276.     ******************************************************************************************************************************/   
  277.     int flags;                                           //【43】图像矩阵是否连续  
  278.     int dims;                                            //【44】图像矩阵的维数dims>=2  
  279.     int rows, cols;                                      //【45】图像矩阵的行和列数  
  280.                                                           //【46】指向图像矩阵矩阵体的指针  
  281.     uchar* data;  
  282.   
  283.     //! pointer to the reference counter;  
  284.     // when matrix points to user-allocated data, the pointer is NULL  
  285.     int* refcount;  
  286.   
  287.     //! helper fields used in locateROI and adjustROI  
  288.     uchar* datastart;  
  289.     uchar* dataend;  
  290.     uchar* datalimit;  
  291.   
  292.     //! custom allocator  
  293.     MatAllocator* allocator;  
  294.   
  295.     struct CV_EXPORTS MSize  
  296.     {  
  297.         MSize(int* _p);  
  298.         Size operator()() const;  
  299.         const int& operator[](int i) const;  
  300.         int& operator[](int i);  
  301.         operator const int*() const;  
  302.         bool operator == (const MSize& sz) const;  
  303.         bool operator != (const MSize& sz) const;  
  304.   
  305.         int* p;  
  306.     };  
  307.   
  308.     struct CV_EXPORTS MStep  
  309.     {  
  310.         MStep();  
  311.         MStep(size_t s);  
  312.         const size_t& operator[](int i) const;  
  313.         size_t& operator[](int i);  
  314.         operator size_t() const;  
  315.         MStep& operator = (size_t s);  
  316.   
  317.         size_t* p;  
  318.         size_t buf[2];  
  319.     protected:  
  320.         MStep& operator = (const MStep&);  
  321.     };  
  322.   
  323.     MSize size;  
  324.     MStep step;  
  325.   
  326. protected:  
  327.     void initEmpty();  
  328. }; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值