栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

opencv之Mat类

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

opencv之Mat类

opencv版本:opencv-3.4.1 

class CV_EXPORTS Mat
{
public:

    //构造函数
    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();

    Mat& operator = (const Mat& m);
    Mat& operator = (const MatExpr& expr);

    //创建一个具有指定了矩形头中行数的参数的矩阵
    Mat row(int y) const;
    //创建一个具有指定了矩形头中列数的参数的矩阵;
    Mat col(int x) const;

    //为指定的行span创建一个新的矩形头,可取指定行区间元素;
    Mat rowRange(int startrow, int endrow) const;
    Mat rowRange(const Range& r) const;

    //为指定的列span创建一个矩形头,可取指定行区间元素;
    Mat colRange(int startcol, int endcol) const;
    Mat colRange(const Range& r) const;

     //为特定的矩阵对角线创建一个新的头;提取对角线元素
    Mat diag(int d=0) const;
    static Mat diag(const Mat& d);

    //创建一个数组及其基础数据的完整副本;
    Mat clone() const;

    //把矩阵复制到另一个矩阵中;
    void copyTo( OutputArray m ) const;
    void copyTo( OutputArray m, InputArray mask ) const;

    //在缩放或不缩放的情况下转换为另一种数据类型;
    void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;

    void assignTo( Mat& m, int type=-1 ) const;

    Mat& operator = (const Scalar& s);

    Mat& setTo(InputArray value, InputArray mask=noArray());

    Mat reshape(int cn, int rows=0) const;
    Mat reshape(int cn, int newndims, const int* newsz) const;
    Mat reshape(int cn, const std::vector& newshape) const;

    MatExpr t() const;

    MatExpr inv(int method=DECOMP_LU) const;

    MatExpr mul(InputArray m, double scale=1) const;

    Mat cross(InputArray m) const;

    double dot(InputArray m) const;

    //返回指定的大小和类型的零数组
    static MatExpr zeros(int rows, int cols, int type);
    static MatExpr zeros(Size size, int type);
    static MatExpr zeros(int ndims, const int* sz, int type);

    //返回一个指定的大小和类型全为1的数组;
    static MatExpr ones(int rows, int cols, int type);
    static MatExpr ones(Size size, int type);
    static MatExpr ones(int ndims, const int* sz, int type);

    static MatExpr eye(int rows, int cols, int type);
    static MatExpr eye(Size size, int type);

    void create(int rows, int cols, int type);
    void create(Size size, int type);
    void create(int ndims, const int* sizes, int type);
    void create(const std::vector& sizes, int type);

    void addref();

    void release();

    void deallocate();
   
    void copySize(const Mat& m);

    void reserve(size_t sz);

    void reserveBuffer(size_t sz);

    void resize(size_t sz);
    void resize(size_t sz, const Scalar& s);

    void push_back_(const void* elem);
    void push_back(const Mat& m);

    void pop_back(size_t nelems=1);

    void locateROI( Size& wholeSize, Point& ofs ) const;

    Mat operator()( const Rect& roi ) const;
    Mat operator()( const Range* ranges ) const;
    Mat operator()(const std::vector& ranges) const;

    bool isContinuous() const;

    bool isSubmatrix() const;
  
    size_t elemSize() const;
    size_t elemSize1() const;

    //返回矩阵元素类型
    int type() const;
    //返回矩阵元素深度
    int depth() const;

    //返回矩形通道的数目
    int channels() const;

    size_t step1(int i=0) const;

    //若数组没有elements,则返回true;
    bool empty() const;

    //返回指定数组元素的引用
    template _Tp& at(int i0=0);
    ...

    size_t total() const;
    size_t total(int startDim, int endDim=INT_MAX) const;

    int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;
   
   
    enum { MAGIC_VAL  = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG };
    enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 };

    int flags; //标志位
	
    //>= 2 矩阵的维数
    int dims;
	
    //矩阵的行数和列数,如果矩阵超过 2 维,这两个变量的值都为-1
    int rows, cols;
	
    // 指向数据的指针
    uchar* data;

    //! helper fields used in locateROI and adjustROI
    const uchar* datastart;
    const uchar* dataend;
    const uchar* datalimit;

    //! interaction with UMat
    UMatData* u;

    MatSize size;
    MatStep step;

protected:
    template void forEach_impl(const Functor& operation);
};

关于Mat类:需要知道:

(1) 不需要手动为其开辟空间;

(2) 使用c++接口时不需要考虑内存释放问题,采用引用计数机制释放内存;

(3) 赋值运算符和拷贝构造函数(构造函数)只复制信息头;

(4) 使用函数clone()或者copyTo()来复制一幅图像的矩阵;

代码示例1:

CV_8UC3:规则如下:

CV[位数][带符号与否][类型前缀]C[通道数];

可指定存储元素的数据类型及每个矩阵点的通道数;

CV_8UC3:使用8位unsigned char型,每个像素由3个元素组成3通道;

Mat A(2,2,CV_8UC3, Scalar(0,0,255));


Mat r = Mat(10, 3, CV_8UC3);
//产生随机值来填充矩阵
randu(r, Scalar::all(0), Scalar::all(255));
 

 

代码示例2:

Mat A(10, 8, CV_8UC1, cv::Scalar(5));

//获取矩阵行列数
std::cout << "A row: " << A.rows << std::endl;//10
std::cout << "A col: " << A.cols << std::endl;//8

//获取指定行列元素
std::cout << A.rowRange(1,3) << std::endl;


std::cout << A.colRange(2,4) << std::endl;


//创建8x8复数矩阵1+5j
Mat B(8, 8, CV_32FC2, cv::Scalar(1,5));

//利用create重新创建10x10的8位无符号3通道矩阵;
B.create(10,10,CV_8UC(3));

std::cout << "B channels = " << B.channels() << std::endl; //3

//转换矩阵类型
B.convertTo(B, CV_32F);
std::cout << "B depth = " << B.depth() << std::endl; //5

//zeros创建矩阵
Mat C = Mat::zeros(B.rows, B.cols, CV_8UC1);

//将A的第4行元素变换成A的第5行元素*2
A.row(4) = A.row(5) * 2;
std::cout << A << std::endl;


//将D矩阵赋值位A的第4列;
Mat D = A.col(4);
std::cout << D << std::endl;


//将A矩阵的第一列复制到D中;
A.col(1).copyTo(D);
std::cout << D << std::endl;

 示例代码3:

Mat A, C;   //仅仅创建信息头部分
A = imread("./lena.jpg", 1); //这里为矩阵开辟内存
Mat B(A);  //拷贝构造函数;
C = A;     //赋值运算符;



//若想复制矩阵本身,而不是矩阵信息头和矩阵指针呢? 使用clone()或copyTo()接口;
Mat D = A.clone();

Mat E;
A.copyTo(E);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009036.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号