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);
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
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.
- 數(shù)據(jù)挖掘原理與實(shí)踐
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)
- 文本挖掘:基于R語言的整潔工具
- 云計(jì)算與大數(shù)據(jù)應(yīng)用
- 揭秘云計(jì)算與大數(shù)據(jù)
- 深入淺出MySQL:數(shù)據(jù)庫開發(fā)、優(yōu)化與管理維護(hù)(第2版)
- Learning Proxmox VE
- SQL優(yōu)化最佳實(shí)踐:構(gòu)建高效率Oracle數(shù)據(jù)庫的方法與技巧
- 數(shù)據(jù)中心數(shù)字孿生應(yīng)用實(shí)踐
- 大數(shù)據(jù)架構(gòu)商業(yè)之路:從業(yè)務(wù)需求到技術(shù)方案
- PostgreSQL指南:內(nèi)幕探索
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)開發(fā)實(shí)例
- 聯(lián)動Oracle:設(shè)計(jì)思想、架構(gòu)實(shí)現(xiàn)與AWR報告
- 大數(shù)據(jù)技術(shù)原理與應(yīng)用:概念、存儲、處理、分析與應(yīng)用
- 區(qū)塊鏈+:落地場景與應(yīng)用實(shí)戰(zhàn)