Building Computer Vision Projects with OpenCV 4 and C++
上QQ阅读APP看书,第一时间看更新

Basic matrix operations

In this section, we will learn a number of basic and important matrix operations that we can apply to images or any matrix data. We learned how to load an image and store it in a Mat variable, but we can create Mat manually. The most common constructor is giving the matrix a size and type, as follows:

Mat a= Mat(Size(5,5), CV_32F); 
You can create a new matrix linking with a stored buffer from third-party libraries without copying data using this constructor: Mat(size, type, pointer_to_buffer).

The types supported depend on the type of number you want to store and the number of channels. The most common types are as follows:

CV_8UC1 
CV_8UC3 
CV_8UC4 
CV_32FC1 
CV_32FC3 
CV_32FC4
You can create any type of matrix using CV_number_typeC(n), where the number_type is  8 bits unsigned (8U) to 64 float (64F), and where  (n) is the number of channels; the number of channels permitted ranges from 1 to CV_CN_MAX.

The initialization does not set up the data values, and hence you can get undesirable values. To avoid undesirable values, you can initialize the matrix with 0 or 1 values with their respective functions:

Mat mz= Mat::zeros(5,5, CV_32F); 
Mat mo= Mat::ones(5,5, CV_32F); 

The results of the preceding matrix are as follows:

A special matrix initialization is the eye function that creates an identity matrix with the specified type and size:

Mat m= Mat::eye(5,5, CV_32F); 

The output is as follows:

All matrix operations are allowed in OpenCV's Mat class. We can add or subtract two matrices of the same size using the + and - operators, as demonstrated in the following code block:

Mat a= Mat::eye(Size(3,2), CV_32F); 
Mat b= Mat::ones(Size(3,2), CV_32F); 
Mat c= a+b; 
Mat d= a-b;

The results of the preceding operations are as follows:

We can multiply by a scalar using the * operator or a matrix per element using the mul function, and we can perform matrix multiplication using the  * operator:

Mat m1= Mat::eye(2,3, CV_32F); 
Mat m2= Mat::ones(3,2, CV_32F); 
// Scalar by matrix 
cout << "nm1.*2n" << m1*2 << endl; 
// matrix per element multiplication 
cout << "n(m1+2).*(m1+3)n" << (m1+1).mul(m1+3) << endl; 
// Matrix multiplication 
cout << "nm1*m2n" << m1*m2 << endl; 

The results of the preceding operations are as follows:

Other common mathematical matrix operations are transposition and matrix inversion, defined by the t() and inv() functions, respectively. Other interesting functions that OpenCV provides are array operations in matrix, for example, counting the nonzero elements. This is useful for counting the pixels or areas of objects:

int countNonZero(src); 

OpenCV provides some statistical functions. Mean and standard deviation by channel can be calculated using the meanStdDev function:

meanStdDev(src, mean, stddev); 

Another useful statistical function is minMaxLoc. This function finds the minimum and the maximum of a matrix or array, and returns the location and value:

minMaxLoc(src, minVal, maxVal, minLoc, maxLoc); 

Here src is the input matrix, minVal and maxVal are double values detected, and minLoc and maxLoc are Point values detected.

Other core and useful functions are described in detail at:  http://docs.opencv.org/modules/core/doc/core.html.