对OpenCV中的Mat类的初步认识

Mat

1. Mat对象的创建方法:

  1. cv::Mat::Mat Constructor

    Mat M(2,2, CV_8UC3, Scalar(0,0,255));
    cout << "M = " << endl << " " << M << endl << endl;
    
  2. 利用数组可以创建多维矩阵

    int sz[3] = {2,2,2};
    Mat L(3,sz, CV_8UC(1), Scalar::all(0));
    
  3. cv::Mat::create function:

    M.create(4,4, CV_8UC(2));
    cout << "M = "<< endl << " "  << M << endl << endl;
    
  4. 熟悉的MATLAB初始化方式

    Mat E = Mat::eye(4, 4, CV_64F);
    cout << "E = " << endl << " " << E << endl << endl;
    Mat O = Mat::ones(2, 2, CV_32F);
    cout << "O = " << endl << " " << O << endl << endl;
    Mat Z = Mat::zeros(3,3, CV_8UC1);
    cout << "Z = " << endl << " " << Z << endl << endl;
    
  5. 采用 cv::Mat::clone() or cv::Mat::copyTo() 方法

    Mat RowClone = C.row(1).clone();
    cout << "RowClone = " << endl << " " << RowClone << endl << endl;
    

2. Mat一些特征概括

官方文档中,对于Mat的几点概括如下:
1. Output image allocation for OpenCV functions is automatic (unless specified otherwise).
2. You do not need to think about memory management with OpenCVs C++ interface.
3. The assignment operator and the copy constructor only copies the header.
4. The underlying matrix of an image may be copied using the cv::Mat::clone() and cv::Mat::copyTo() functions.

Mat的以下操作均为浅复制,A,B,C中每个Mat对象共享同一个矩阵数据,只是header不一样:

Mat A, C;                          // creates just the header parts
A = imread(argv[1], IMREAD_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A);                                 // Use the copy constructor
C = A;                                    // Assignment operator

在实际应用中,也经常是多个Mat实例共用一个矩阵数据,或者矩阵数据中的某部分,如下面:

Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值