opencv Mat


Mat (Matrix) is an n-dimensional array that can be used to store various type of data, such as RGB, HSV or grayscale images, vectors with real or complex values, other matrices etc.

A Mat contains the following information: width, height, type, channels, data, flags, datastart, dataend and so on.

It has several methods, some of them are: create, copyTo, convertTo, isContinious etc.

There are many ways to create a Mat variable. Consider I want to create a matrix with 100 rows, 200 columns, type 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

Initializing Mat:

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++)
  <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));