美文网首页
OpenCV 教程 05 : 创建 Mat 对象的几种方式

OpenCV 教程 05 : 创建 Mat 对象的几种方式

作者: wjundong | 来源:发表于2020-02-23 19:14 被阅读0次

示例代码

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char const *argv[])
{
    // 1. 使用构造函数
    Mat M(4, 4, CV_8UC3, Scalar(0, 0, 255));
    cout << "M: \n" << M << endl;

    // 2. 在 C/C++ 中通过构造函数进行初始化
    // 构造多为数组,指定3维,并传入一个数组,包含每个维度的尺寸
    int sz[3] = {2, 2 ,2};
    Mat L(3, sz, CV_8UC3, Scalar::all(0));

    // 3. 利用 Create() 成员函数进行初始化
    // 该方法不能为矩阵设置初值
    M.create(2, 2, CV_8UC3);

    // 4. 采用 Matlab 式初始化方式
    Mat E = Mat::eye(4, 4, CV_64F);
    Mat O = Mat::ones(2, 2, CV_32F);
    Mat Z = Mat::zeros(3, 3, CV_8UC1);
    cout << "E: \n" << E << endl;

    // 5. 使用逗号分隔符初始化
    Mat C = (Mat_<double>(3, 3) << 0, -1, 0, 5, -1, 0, -1, 0);

    // 6. 使用成员函数 clone() 或 copyTo() 创建
    Mat RowClone = C.row(1);

    Mat Copy;
    C.copyTo(Copy);
    cout << "Copy: \n" << RowClone << endl;

    waitKey(0);
    return 0;
}

运行结果

image.png

相关文章

网友评论

      本文标题:OpenCV 教程 05 : 创建 Mat 对象的几种方式

      本文链接:https://www.haomeiwen.com/subject/uessqhtx.html