官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 胶南市| 孙吴县| 宜兰市| 青铜峡市| 湄潭县| 邛崃市| 江北区| 南阳市| 定襄县| 大冶市| 新平| 蒲江县| 磐石市| 合阳县| 穆棱市| 宜宾市| 顺义区| 金寨县| 丽水市| 琼中| 巴彦淖尔市| 共和县| 柳河县| 灵宝市| 万年县| 天门市| 叙永县| 建宁县| 哈尔滨市| 五大连池市| 凤台县| 古交市| 高清| 新兴县| 娄底市| 扎赉特旗| 七台河市| 安西县| 潼南县| 江都市| 临猗县|