Mat的创建方式

注意:

Mat a(行, 列, CV_8U); //CV_8U默认205

Mat a(Size(宽, 高), CV_8U);

创建与已有img等大小的矩阵:
将下列代码中的2,2都可以换成img.size(), img单通道对应CV_8U,通道对应CV_8UC3。一般使用8位就可以。16、32不推荐。

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
    //Scalar自定义
    Mat a3(2, 2, CV_8UC3, Scalar(255,0,0));// 三通道
    cout << "三通道" << endl << " " << a3 << endl;
    Mat a1(2, 2, CV_8U, Scalar(255));// 单通道,CV_8UC1中C1可省略
    cout << "单通道" << endl << " " << a1 << endl;
    //全205
    Mat MC(2, 2, CV_8U);//Mat MC;MC.create(2, 2, CV_8U); //CV_8UC3也全是205
    cout << "全205" << endl << " " << MC << endl;
    //单位矩阵
    Mat E = Mat::eye(2, 2, CV_8U);
    cout << "单位矩阵" << endl << " " << E << endl;
    //全1
    Mat O = Mat::ones(2, 2, CV_8U);
    cout << "全1矩阵" << endl << " " << O << endl;
    //全0
    Mat Z = Mat::zeros(2, 2, CV_8U);
    cout << "0矩阵" << endl << " " << Z << endl;
    //自定义
    uchar d[2][3] = { 1,2,3,4,5,6 };
    Mat d2(Size(3, 2), CV_8U, d);//    Mat d2 = (Mat_<int>(2,3)<<1,2,3,4,5,6);
    cout << "自定义" << endl << " " <<d2 << endl;

    std::system("pause");
    return 0;
}

 【我的其他相关博文】

图与矩阵的创建

1、Mat类的属性、方法

posted @ 2020-02-26 17:09  夕西行  阅读(440)  评论(0编辑  收藏  举报