Mat(Matrix)是一個 n 維陣列,可用於儲存各種型別的資料,如 RGB,HSV 或灰度影象,具有實數或複數值的向量,其他矩陣等。

一個 Mat 包含以下資訊:widthheighttypechannelsdataflagsdatastartdataend 等。

它有幾種方法,其中一些是:createcopyToconvertToisContinious 等。

有很多方法可以建立 Mat 變數。考慮我想要建立一個包含 100 行,200 列,型別為 CV_32FC3 的矩陣:

int R = 100, C = 200;
Mat m1; m1.create(R,C,CV_32FC3);//creates empty matrix
Mat m2(cv::Size(R, C), CV_32FC3); // creates a matrix with R rows, C columns with data type T where R and C are integers, 
Mat m3(R,C,CV_32FC3); // same as m2

初始化墊:

Mat m1 = Mat::zeros(R,C,CV_32FC3); // This initialized to zeros, you can use one, eye or cv::randn etc.
Mat m2(R,C,CV_32FC3);
for (int i = 0; i < m2.rows; i++)
    for (int j = 0; j < m2.cols; j++)
        for (int k = 0; k < m2.channels(); k++)
            m2.at<Vec3f>(i,j)[k] = 0;
//Note that, because m2 is a float type and has 3 channels, we used Vec3f, for more info see Vec 

Mat m3(3, out, CV_32FC1, cv::Scalar(0));