Opencv-C++ Notes (2): Matrix operations of opencv

Creation and initialization

Matrix operations in OpenCV are very important. This article summarizes the creation, initialization and basic matrix operations of matrices, and gives sample codes. The main contents include:

1.1 Data Types

To establish a matrix, you must specify the data type stored in the matrix. Several data types commonly used in image processing are as follows:

CV_8UC1// 8位无符号单通道
CV_8UC3// 8位无符号3通道
CV_8UC4
CV_32FC1// 32位浮点型单通道
CV_32FC3// 32位浮点型3通道
CV_32FC4
 

Including data bit depth 8-bit, 32-bit, data type U: uchar, F: float type and number of channels C1: single channel, C3: three channels, C4: four channels.

1.2 Basic method

We can create a Mat type matrix by loading an image, of course, we can also create a matrix directly manually. The basic method is to specify the matrix size and data type:

// 基本方法
	cv::Mat a(cv::Size(5,5),CV_8UC1); // 单通道
	cv::Mat b = cv::Mat(cv::Size(5,5),CV_8UC3); //3通道每个矩阵元素包含3个uchar值
	cout<<"a  = "<<endl<<a<<endl<<endl;
	cout<<"b  = "<<endl<<b<<endl<<endl;
	system("pause");`

insert image description here
In a 3-channel matrix, a matrix element contains 3 variables.

1.3 Initialization method

The above method does not initialize the matrix data, so random values ​​will appear. If you want to avoid this situation, you can use several initialization methods of the Mat class to create matrices:

// 初始化方法 	
cv::Mat mz = cv::Mat::zeros(cv::Size(5,5),CV_8UC1); 
// 全零矩阵 	
cv::Mat mo = cv::Mat::ones(cv::Size(5,5),CV_8UC1); 
 // 全1矩阵 	
 cv::Mat me = cv::Mat::eye(cv::Size(5,5),CV_32FC1);  
 // 对角线为1的对角矩阵 
 	cout<<"mz = "<<endl<<mz<<endl<<endl; 	
 	cout<<"mo = "<<endl<<mo<<endl<<endl; 	
 	cout<<"me = "<<endl<<me<<endl<<endl;

operation result:
insert image description here

Matrix addition and subtraction

We can use "+" and "-" symbols for matrix addition and subtraction.

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

insert image description here

matrix multiplication

Use "*" to calculate the multiplication of matrix and scalar, and the multiplication of matrix and matrix (must meet the corresponding rules for the number of rows and columns of matrix multiplication)

Mat m1= Mat::eye(2,3, CV_32F); //使用cv命名空间可省略cv::前缀,下同
	Mat m2= Mat::ones(3,2, CV_32F);
	cout<<"m1  = "<<endl<<m1<<endl<<endl;
	cout<<"m2  = "<<endl<<m2<<endl<<endl;
	// Scalar by matrix
	cout << "\nm1.*2 = \n" << m1*2 << endl;
	// matrix per element multiplication
	cout << "\n(m1+2).*(m1+3) = \n" << (m1+1).mul(m1+3) << endl;
	// Matrix multiplication
	cout << "\nm1*m2 = \n" << m1*m2 << endl;

insert image description here

Matrix transpose

Matrix transposition is to reverse the row and column order of the matrix (i-th row is converted to i-th column) to form a new matrix. OpenCV is implemented through the t() function of the Mat class.
// Transpose

Mat m1= Mat::eye(2,3, CV_32F);	
Mat m1t = m1.t();
cout<<"m1  = "<<endl<<m1<<endl<<endl;
cout<<"m1t  = "<<endl<<m1t<<endl<<endl;
system("pause");

operation result:
insert image description here

matrix inversion

The inverse matrix often appears in some algorithms, and it is implemented by the inv() method of the Mat class in OpenCV

// 求逆
	Mat meinv = me.inv();
	cout<<"me  = "<<endl<<me<<endl<<endl;
	cout<<"meinv = "<<endl<<meinv<<endl<<endl;
	system("pause");

insert image description here
The inverse of the identity matrix is ​​itself.

The number of non-zero elements in the matrix

Calculating the pixel or area of ​​an object often requires the number of non-zero elements in the calculation matrix, which is implemented in OpenCV using the countNonZero() function.
// number of non-zero elements

int nonZerosNum = countNonZero(me); // me is the input matrix or image
cout<<"me = "<<endl<<me<<endl;
the number of non-zero elements in cout<<"me="<<nonZerosNum< <endl<<endl;
system("pause");

operation result:
insert image description here

Matrix mean and standard deviation

OpenCV provides matrix mean and standard deviation calculation functions, which can be implemented using the meanStdDev(src, mean, stddev) function Parameters
src
– input matrix or image
mean – mean, OutputArray
stddev – standard deviation, OutputArray

 // 均值方差
	Mat mean;
	Mat stddev;
	meanStdDev(me, mean, stddev); //me为前文定义的5×5对角阵
	cout<<"mean = "<<mean<<endl;
	cout<<"stddev = "<<stddev<<endl;
	system("pause");

insert image description here
It should be noted that if src is a multi-channel image or a multi-dimensional matrix, the function calculates the mean and standard deviation of different channels respectively, so the return values ​​mean and stddev are vectors of corresponding dimensions.

Mat mean3;
	Mat stddev3;
	Mat m3(cv::Size(5,5),CV_8UC3,Scalar(255,200,100));
	cout<<"m3  = "<<endl<<m3<<endl<<endl;
	meanStdDev(m3, mean3, stddev3);
	cout<<"mean3 = "<<mean3<<endl;
	cout<<"stddev3 = "<<stddev3<<endl;
	system("pause");

Multi-channel matrix operation result:
insert image description here
insert image description here

Matrix global extremum and position

求输入矩阵的全局最大最小值及其位置,可使用函数:
void minMaxLoc(InputArray src, CV_OUT double* minVal,
                           CV_OUT double* maxVal=0, CV_OUT Point* minLoc=0,
                           CV_OUT Point* maxLoc=0, InputArray mask=noArray());
                           参数:

 

src – 输入单通道矩阵(图像).
minVal – 指向最小值的指针, 如果未指定则使用NULL
maxVal – 指向最大值的指针, 如果未指定则使用NULL
minLoc – 指向最小值位置(2维情况)的指针, 如果未指定则使用NULL
maxLoc – 指向最大值位置(2维情况)的指针, 如果未指定则使用NULL
mask – 可选的蒙版,用于选择待处理子区域
 

// 求极值 最大、最小值及其位置
	Mat img = imread("Lena.jpg",0);
	imshow("original image",img);
 
	double minVal=0,maxVal=0;
	cv::Point minPt, maxPt;
	minMaxLoc(img,&minVal,&maxVal,&minPt,&maxPt);
	cout<<"min value  = "<<minVal<<endl;
	cout<<"max value  = "<<maxVal<<endl;
	cout<<"minPt = ("<<minPt.x<<","<<minPt.y<<")"<<endl;
	cout<<"maxPt = ("<<maxPt.x<<","<<maxPt.y<<")"<<endl;
	cout<<endl;
 
	cv::Rect rectMin(minPt.x-10,minPt.y-10,20,20);
	cv::Rect rectMax(maxPt.x-10,maxPt.y-10,20,20);
 
	cv::rectangle(img,rectMin,cv::Scalar(200),2);
	cv::rectangle(img,rectMax,cv::Scalar(255),2);
 
	imshow("image with min max location",img);
	cv::waitKey();

operation result:
insert image description here
insert image description here

GEMM General Matrix Multiplication

void cvGEMM( const CvArr* src1, const CvArr* src2, double alpha,
const CvArr* src3, double beta, CvArr* dst, int tABC=0 );
#define cvMatMulAdd( src1, src2, src3, dst ) cvGEMM( src1, src2, 1, src3, 1, dst, 0 )
#define cvMatMul( src1, src2, dst ) cvMatMulAdd( src1, src2, 0, dst )
src1
first input array
src2
second input array
src3
third input array (offset amount), or NULL if there is no offset.
dst
output array
tABC
T operation flag, which can be 0 or a combination of values ​​listed below:
CV_GEMM_A_T - transpose src1
CV_GEMM_B_T - transpose src2
CV_GEMM_C_T - transpose src3
For example, CV_GEMM_A_T+CV_GEMM_C_T corresponds to
alpha src1T src2 + beta*src3T
function cvGEMM execution General matrix multiplication:

dst = alpha*op(src1) op(src2) + beta op(src3), where op(X) is X or XT
All matrices should have the same data type and coordinated matrix size. Supports real floating-point matrices or complex floating-point matrices.

Transform performs a matrix transformation on each element of the array

void cvTransform( const CvArr* src, CvArr* dst, const CvMat* transmat, const CvMat* shiftvec=NULL );
src
input array
dst
output array
transmat
transformation matrix
shiftvec
optional shift vector
function cvTransform performs matrix on each element of array src Transform and store the result to dst:

dst(I)=transmat*src(I) + shiftvec
or

dst(I)k=sumj(transmat(k,j)*src(I)j) + shiftvec(k) Each
element of the N-channel array src is regarded as an N-element vector, using an M×N The transformation matrix transmat and the offset vector shiftvec transform it into an element of an M-channel array dst. Here you can choose to embed the offset vector shiftvec into transmat. In this case transmat should be a matrix of M×N+1, and the rightmost column is considered as the offset vector.

The input and output arrays should have the same depth and the same size or ROI size. transmat and shiftvec should be real floating-point matrices.

This function can be used for geometric transformation of ND point set, arbitrary linear color space transformation, channel transformation, etc.

MulTransposed Computes the product of an array and the transpose of an array

void cvMulTransposed( const CvArr* src, CvArr* dst, int order, const CvArr* delta=NULL );
src
input matrix
dst
destination matrix
order
multiplication order
delta
an optional array to subtract from src before multiplication.
The function cvMulTransposed calculates the product of src and its transpose.

Function evaluation formula:

if order=0

dst=(src-delta)*(src-delta)T
else

dst=(src-delta)T*(src-delta)

Trace returns the trace of the matrix

CvScalar cvTrace( const CvArr* mat );
mat
input matrix
The function cvTrace returns the sum of the diagonal elements of the matrix mat.

tr(src) = ∑ mat(i,i)
i

Transpose the transpose of the matrix

void cvTranspose( const CvArr* src, CvArr* dst );
#define cvT cvTranspose
src
input matrix
dst
target matrix
The function cvTranspose seeks the transposition of the matrix src:

dst(i,j)=src(j,i)
Note that if it is a complex matrix, the conjugate of the complex number will not be obtained. Conjugation should be independent: see the cvXorS example code.

Det returns the determinant value of the matrix

double cvDet( const CvArr* mat );
mat
input matrix
The function cvDet returns the determinant value of the square matrix mat. Calculate directly for small matrices, and use Gaussian (GAUSSIAN) elimination method for large matrices. For a symmetric positive-determined matrix, you can also use the SVD function, U=V=NULL, and then use the product of the diagonal elements of w to calculate the determinant.

Invert finds the inverse or pseudoinverse of a matrix

double cvInvert( const CvArr* src, CvArr* dst, int method=CV_LU );
#define cvInv cvInvert
src
input matrix
dst
target matrix
method
inversion method:
CV_LU - Gaussian elimination method for optimal pivot selection
CV_SVD - singular value decomposition method (SVD)
CV_SVD_SYM - SVD method for positive definite symmetric matrices
The function cvInvert inverts the matrix src and stores the result in dst.

If it is the LU method, the function returns the determinant value of src (src must be a square matrix). If 0, the matrix is ​​not inverted and dst is filled with 0s.

If the SVD method, the function returns the reciprocal of the condition number of src (the ratio of the smallest singular value to the largest singular value), and returns 0 if src is all 0. If src is singular, the SVD method computes a pseudo-inverse matrix.

Solve solves linear systems or least squares problems

int cvSolve( const CvArr* src1, const CvArr* src2, CvArr* dst, int method=CV_LU );

src1 input matrix
src2 right part of linear system
dst output solution method

Solution (matrix inversion) :
CV_LU - Gaussian Elimination for optimal pivot selection
CV_SVD - Singular Value Decomposition (SVD)
CV_SVD_SYM - SVD method for positive definite symmetric matrices
Function cvSolve for linear systems or least squares (the latter can be solved by SVD method):

/mbox{dst} = /arg /min_X |/mbox{src1}/cdot X-/mbox{src2}|
if using CV_LU method. The function returns 1 if src1 is nonsingular, and 0 otherwise, in which case dst is invalid.

SVD Singular value decomposition of real floating-point matrices

void cvSVD( CvArr* A, CvArr* W, CvArr* U=NULL, CvArr* V=NULL, int flags=0 );

AM×N input matrix W resulting singular value matrix (M×N or N×N) or vector (N×1). U Optional left orthogonal matrix (M×M or M×N). If CV_SVD_U_T
is Specifies that the number of rows and columns mentioned above should be swapped. V optional right orthogonal matrix (N×N)

flags
operation flag; can be 0 or a combination of the following values:
CV_SVD_MODIFY_A The matrix src1 can be modified by operation. In this way, the processing speed will be faster.
CV_SVD_U_T means that the transposed matrix U will be returned, specifying this flag will speed up the processing.
CV_SVD_V_T means that the transposed matrix V will be returned, specifying this flag will speed up the processing.

The function cvSVD decomposes matrix A into a product of a diagonal matrix and two orthogonal matrices:

/mathbf{/it A=U W V^T}

Here W is a diagonal matrix of singular values, which can be encoded as a one-dimensional vector of singular values, as well as U and V. All singular values ​​are non-negative and stored in descending order. (U and V
are also stored accordingly).

The SVD algorithm has been very stable in numerical processing, and its typical applications include:

Exactly solves eigenvalue problems when A is a square, symmetric, and positive matrix, eg, when A is a covariance matrix. In this case W will be a vector of eigenvalues, and
U=V is the eigenvector of the matrix (thus, only one of U and V needs to be calculated when eigenvectors need to be calculated). Exactly solves ill-conditioned linear systems.
Least squares solution of overdetermined linear systems. Both the previous problem and this problem can use the cvSolve method specifying CV_SVD.
Accurate calculation of different features of matrices such as rank (number of non-zero singular values), condition number (ratio of largest and smallest singular values), determinant
value (absolute value of determinant equals product of singular values). The above All these values ​​do not require the computation of the matrices U and V .

SVBkSb singular value back substitution algorithm (back substitution)

void cvSVBkSb( const CvArr* W, const CvArr* U, const CvArr* V,
const CvArr* B, CvArr* X, int flags );
W
Singular value matrix or vector
U
Left orthogonal matrix (possibly transposed)
V
Right-orthogonal matrix (possibly transposed)
B
The multiplicative matrix of the pseudo-inverse of the original matrix A. This is an optional parameter. If it is omitted it is assumed to be a suitably sized identity matrix (thus x will be the reconstruction of the pseudo-inverse of A).
X target matrix: The flags operation flag
of the result of the singular value back substitution algorithm , which is the same as the flag of the cvSVD just discussed. The function cvSVBkSb calculates the back substitution for decomposed matrix A and matrix B (see cvSVD description):


X=V W-1 UT*B
here

W-1(i,i)=1/W(i,i) if W(i,i) > epsilon•sumiW(i,i), otherwise: 0. epsilon is
a
small number. This function and the cvSVD function are used to execute cvInvert and cvSolve. The reason for using these functions (svd & bksb) is that the primary function (low-level) function can avoid the internally allocated temporary matrix in the calculation of the advanced function (inv & solve).

EigenVV computes the eigenvalues ​​and eigenvectors of a symmetric matrix

void cvEigenVV( CvArr* mat, CvArr* evects, CvArr* evals, double eps=0 );
mat
input symmetrical square matrix. will be changed during processing.
evects
eigenvector output matrix, store
evals
eigenvalue output matrix in descending order continuously (of course, the sorting of eigenvalues ​​and eigenvectors is synchronized).
Accuracy of eps
diagonalization (typically, DBL_EPSILON=≈10-15 is enough).
The function cvEigenVV calculates the eigenvalues ​​and eigenvectors of the matrix A:

mat*evects(i,:)' = evals(i)*evects(i,:)' (notation in MATLAB) The
data of matrix A will be modified by this function.

Currently this function is slower and less accurate than the function cvSVD. If A is known to be positive definite, (for example, it is a covariance matrix), it is usually handed over to the function cvSVD to calculate its eigenvalues ​​and eigenvectors, especially is without the need to calculate the eigenvectors

CalcCovarMatrix Computes the covariance matrix of a collection of vectors

void cvCalcCovarMatrix( const CvArr** vects, int count, CvArr* cov_mat, CvArr* avg, int flags );
vects
input vector. They must have the same data type and size. The vectors don't have to be one-dimensional, they can also be two-dimensional (eg, images) and so on.
count
the number of input vectors
cov_mat
outputs the covariance matrix, which is a square matrix of floating point type.
avg
input or output array (depending on flags ") - Average vector of input vectors.
flags
operation flags, a combination of the following values:
CV_COVAR_SCRAMBLED - the output covariance matrix is ​​calculated as follows:
scale * [vects[0] − avg,vects[1] − avg,…]T * [vects[0] − avg,vects[ 1] − avg,…], that is, the covariance matrix is ​​count×count. Such an unusual matrix is ​​used in fast PCA methods for a set of large vectors (for example, the EigenFaces technique for face recognition). The eigenvalues ​​of this scrambled ("scrambled") matrix will match the eigenvalues ​​of the true covariance matrix, and the true eigenvectors can be easily computed from the eigenvectors of the scrambled ("scrambled") covariance matrix.
CV_COVAR_NORMAL - the output covariance matrix is ​​computed as:
scale * [vects[0] − avg,vects[1] − avg,…] * [vects[0] − avg,vects[1] − avg,…]T, that is, cov_mat will be one and each The usual covariance matrix with the same linear size in the number of elements of the input vectors. CV_COVAR_SCRAMBLED and CV_COVAR_NORMAL can only specify one of them at the same time.
CV_COVAR_USE_AVG - If this flag is specified, the function will not calculate avg from the input vector, but use the past avg vector, which is useful if avg has been calculated somehow. Or if the covariance matrix is ​​partially computed - in this case, avg is not the average of a subset of the input vectors, but the average vector of the entire set.
CV_COVAR_SCALE - If this flag is specified, the covariance matrix is ​​scaled. the covariation matrix is ​​scaled. In "normal" mode the scaling is 1./count, in "scrambled" mode the scaling is the reciprocal of the sum of the elements of each input vector. By default (if no flag is specified) the covariance matrix is ​​not scaled (scale=1).
The function cvCalcCovarMatrix calculates the covariance matrix and mean vector of input vectors. This function can be used in principal component analysis (PCA), and Mahalanobis distance (Mahalanobis distance) comparison vector, etc.

Mahalanobis calculates the Mahalanobis distance between two vectors (Mahalanobis distance)

double cvMahalanobis( const CvArr* vec1, const CvArr* vec2, CvArr* mat );
vec1
the first one-dimensional input vector
vec2
the second one-dimensional input vector
mat
The inverse matrix function of the covariance matrix
cvMahalanobis calculates the difference between two vectors Weighted distance, the return result is:

d(vec1,vec2)=/sqrt{ /sum_{i,j} /{mat(i,j) (vec1(i)-vec2(i)) (vec1(j)-vec2(j))/} }
The covariance matrix can be calculated with the function cvCalcCovarMatrix, and the inverse matrix can be calculated with the function cvInvert (CV_SVD method is a better choice, because the matrix may be singular).

CalcPCA performs PCA transformation on a vector set

void cvCalcPCA( const CvArr* data, CvArr* avg,
CvArr* eigenvalues, CvArr* eigenvectors, int flags );
data
input data, each vector is a single row vector (CV_PCA_DATA_AS_ROW) or a single column vector (CV_PCA_DATA_AS_COL).
avg
average vector, in the function Internally calculated or provided by the caller to provide
the eigenvalues
​​of the covariance matrix output by
eigenvalues ​​The eigenvectors
(that is, the principal components) of the covariance matrix output by eigenvectors, each vector has a row of
flags
operation flags, which can be a combination of the following methods:
CV_PCA_DATA_AS_ROW - Vectors are stored in rows (that is, any vector is stored continuously)
CV_PCA_DATA_AS_COL - Vectors are stored in columns (that is, the value of a certain vector component is stored continuously)
(the above two signs are mutually exclusive Exclusive)
CV_PCA_USE_AVG - Use pre-calculated average value
. This function performs PCA transformation on a vector set. It first calculates the covariance matrix using cvCalcCovarMatrix and then calculates the eigenvalues ​​​​and eigenvectors of the covariance matrix. The output eigenvalues ​​​​/eigenvectors The number is less than or equal to MIN(rows(data), cols(data)).

ProjectPCA projects a vector onto a subspace

void cvProjectPCA( const CvArr* data, const CvArr* avg,
const CvArr* eigenvectors, CvArr* result )
data
input data, each vector can be a single-row or single-column
avg
average vector. Either it is a single-row vector which means that the input data is in rows The data is stored in the form of a single-column vector, which means that the input vector is stored in a column.
eigenvectors
eigenvectors (principal components), each vector has one row.
The result
output decomposition coefficient matrix, the number of rows of the matrix must be the same as The number of input vectors is equal, and the number of columns of the matrix must be less than the number of rows of the eigenvector.
This function projects the input vectors to an orthogonal system (eigenvectors). Before computing the dot product, the input vectors are subtracted from the mean vector:

result(i,:)=(data(i,:)-avg)*eigenvectors’ // for CV_PCA_DATA_AS_ROW layout.

[edit]
BackProjectPCA
reconstructs the original vector according to the projection coefficients

void cvBackProjectPCA( const CvArr* proj, const CvArr* avg,
const CvArr* eigenvects, CvArr* result );
proj
input data, consistent with the format of cvProjectPCA
avg
average vector. If it is a single row vector, it means that the output vector is Otherwise, it is a single-column vector, then the output vector is stored in a column.
eigenvectors eigenvectors
(principal components), each vector has one row. The reconstructed matrix output by the
result function reconstructs the original according to the projection coefficient vector:

result(i,:)=proj(i,:)*eigenvectors + avg // for CV_PCA_DATA_AS_ROW layout

attached

matrix operation

Allocate free matrix space

Summary:
OpenCV has C language functions for matrix operations. Many other methods provide more convenient C++ interfaces with the same efficiency as OpenCV. OpenCV handles vectors as
1D matrices.
The matrix is ​​stored in rows, each row has 4 bytes proofreading.

Allocate matrix space:
CvMat* cvCreateMat(int rows, int cols, int type);

    type: 矩阵元素类型. 格式为CV_<bit_depth>(S|U|F)C<number_of_channels>.  
    例如: CV_8UC1 表示8位无符号单通道矩阵, CV_32SC2表示32位有符号双通道矩阵.

    例程:
    CvMat* M = cvCreateMat(4,4,CV_32FC1);

Release matrix space:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
cvReleaseMat(&M);

Copy matrix:
CvMat* M1 = cvCreateMat(4,4,CV_32FC1);
CvMat* M2;
M2=cvCloneMat(M1);

Initialize matrix:
double a[] = { 1, 2, 3, 4, 5,
6, 7, 8,
9, 10, 11, 12 };

CvMat Ma=cvMat(3, 4, CV_64FC1, a);
Another way:

CvMat Ma;
cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);

Initialize the matrix as an identity matrix:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
cvSetIdentity(M); // There seems to be a problem here, unsuccessful

Access matrix elements

Suppose you need to access the (i,j)th element of a 2-dimensional floating-point matrix.

Indirect access to matrix elements:
cvmSet(M,i,j,2.0); // Set M(i,j)
t = cvmGet(M,i,j); // Get M(i,j)

Direct access, assuming 4-byte alignment:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
int n = M->cols;
float *data = M->data.fl;

data[i*n+j] = 3.0;

Direct access, correction byte arbitrary:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
int step = M->step/sizeof(float);
float *data = M->data.fl;

(data+i*step)[j] = 3.0;

Direct access to an initialized matrix element:
double a[16];
CvMat Ma = cvMat(3, 4, CV_64FC1, a);
a[i*4+j] = 2.0; // Ma(i,j)=2.0 ;

Matrix/Vector Operations

matrix-matrix operation:
CvMat *Ma, *Mb, Mc;
cvAdd(Ma, Mb, Mc); // Ma+Mb -> Mc
cvSub(Ma, Mb, Mc); // Ma-Mb -> Mc
cvMatMul(Ma, Mb, Mc); // Ma
Mb -> Mc

按电影的电影的matrix operation:
CvMat *Ma, *Mb, *Mc;
cvMul(Ma, Mb, Mc); // Ma.*Mb -> Mc
cvDiv(Ma, Mb, Mc); // Ma./Mb -> Mc
cvAddS(Ma, cvScalar(-10.0), Mc); // Ma.-10 -> Mc

Vector product:
double va[] = {1, 2, 3};
double vb[] = {0, 0, 1};
double vc[3];

CvMat Va=cvMat(3, 1, CV_64FC1, va);
CvMat Vb=cvMat(3, 1, CV_64FC1, vb);
CvMat Vc=cvMat(3, 1, CV_64FC1, vc);

double res=cvDotProduct(&Va,&Vb); // dot product: Va . Vb -> res cvCrossProduct
(&Va, &Vb, &Vc); // vector product: Va x Vb -> Vc
end{verbatim}
Note Va, Vb, Vc must have the same number of vector elements in the vector product.

Single matrix operation:
CvMat *Ma, *Mb;
cvTranspose(Ma, Mb); // transpose(Ma) -> Mb (cannot transpose itself)
CvScalar t = cvTrace(Ma); // trace(Ma) - > t.val[0]
double d = cvDet(Ma); // det(Ma) -> d
cvInvert(Ma, Mb); // inv(Ma) -> Mb

Non-homogeneous linear system solution:
CvMat* A = cvCreateMat(3,3,CV_32FC1);
CvMat* x = cvCreateMat(3,1,CV_32FC1);
CvMat* b = cvCreateMat(3,1,CV_32FC1);
cvSolve(&A, &b, &x); // solve (Ax=b) for x

Eigenvalue analysis (for symmetric matrices):
CvMat* A = cvCreateMat(3,3,CV_32FC1);
CvMat* E = cvCreateMat(3,3,CV_32FC1);
CvMat* l = cvCreateMat(3,1,CV_32FC1);
cvEigenVV( &A, &E, &l); // l = eigenvalues ​​of A (in descending order)
// E = corresponding eigenvectors (per row)

Singular value decomposition SVD:
CvMat* A = cvCreateMat(3,3,CV_32FC1);
CvMat* U = cvCreateMat(3,3,CV_32FC1);
CvMat* D = cvCreateMat(3,3,CV_32FC1);
CvMat* V = cvCreateMat( 3,3,CV_32FC1);
cvSVD(A, D, U, V, CV_SVD_U_T|CV_SVD_V_T); // A = UDV^T
label makes U and V transposed when returning (if there is no transpose label, there is no problem success!!!).

video sequence manipulation

Grab a frame from a video sequence

OpenCV supports grabbing images from webcam or video files (AVI).

Get initialization from camera:
CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0

Get initialization from video file:
CvCapture* capture = cvCaptureFromAVI(“infile.avi”);

Grab a frame:
IplImage* img = 0;
if(!cvGrabFrame(capture)){ // grab a frame
printf("Could not grab a frame/n/7");
exit(0);
}
img=cvRetrieveFrame( capture); // Restore captured frame images
To acquire images from multiple cameras at the same time, first capture a frame from each camera. Restore frame images after all capture actions are completed.

Release the capture source:
cvReleaseCapture(&capture);
Note that the image captured by the device is automatically allocated and released by the capture function. Do not try to release it yourself.

Get/set frame information

Get device properties:
cvQueryFrame(capture); // this call is necessary to get correct
// capture properties
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_W IDTH);
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
All frame numbers seem to be only related to video files. Wrong when using a camera, strange!!!.

Get frame information:
float posMsec = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC);
int posFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES);
float posRatio = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO) ;
Get the position of the captured frame in the video sequence, from The first frame is counted in milliseconds. Or start from the first frame with a number of 0 to get the number of the captured frame. Or take the relative position, the first frame is 0, and the last frame is 1, which is only valid for video files.

Set the label of the first captured frame:
// Capture from the relative position 0.9 of the video file
cvSetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO, (double)0.9);
is only valid for capturing from video files. But it seems to be unsuccessful! !!

store video files

Initialize video memory:
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; // 744 for firewire cameras
int frameH = 480; // 480 for firewire cameras
writer=cvCreateVideoWriter( "out.avi", CV_FOURCC('P','I','M','1'),
fps, cvSize(frameW,frameH),isColor);
other valid encodings:

CV_FOURCC('P','I','M','1') = MPEG-1 codec
CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well)
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec CV_FOURCC
('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC( 'D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2 ', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec
If the video codec is set to -1, a codec selection window will open ( under windows system).

Store video files:
IplImage* img = 0;
int nFrames = 50;
for(i=0;i<nFrames;i++){ cvGrabFrame(capture); // capture frame img=cvRetrieveFrame(capture); // restore image cvWriteFrame (writer,img); //Add frames to video file } To view the captured image while capturing, add the following code in the loop:




cvShowImage(“mainWin”, img);
key=cvWaitKey(20); // wait 20 ms
If there is no 20[milliseconds] delay, the video sequence will not be displayed correctly.

Release video memory:
cvReleaseVideoWriter(&writer);

List of other matrix operation functions

add

矩阵加法,A+B的更高级形式,支持mask

scaleAdd

矩阵加法,一个带有缩放因子dst(I) = scale * src1(I) + src2(I)

addWeighted

矩阵加法,两个带有缩放因子dst(I) = saturate(src1(I) * alpha + src2(I) * beta + gamma)

subtract

矩阵减法,A-B的更高级形式,支持mask

multiply

矩阵逐元素乘法,同Mat::mul()函数,与A*B区别,支持mask

gemm

一个广义的矩阵乘法操作

divide

矩阵逐元素除法,与A/B区别,支持mask

abs

对每个元素求绝对值

absdiff

两个矩阵的差的绝对值

exp

求每个矩阵元素 src(I) 的自然数 e 的 src(I) 次幂 dst[I] = esrc(I)

pow

求每个矩阵元素 src(I) 的 p 次幂 dst[I] = src(I)p

log

求每个矩阵元素的自然数底 dst[I] = log|src(I)| (if src != 0)

sqrt

求每个矩阵元素的平方根

min, max

求每个元素的最小值或最大值返回这个矩阵 dst(I) = min(src1(I), src2(I)), max同

minMaxLoc

定位矩阵中最小值、最大值的位置

compare

返回逐个元素比较结果的矩阵

bitwise_and, bitwise_not, bitwise_or, bitwise_xor

每个元素进行位运算,分别是和、非、或、异或

cvarrToMat

旧版数据CvMat,IplImage,CvMatND转换到新版数据Mat

extractImageCOI

从旧版数据中提取指定的通道矩阵给新版数据Mat

randu

以Uniform分布产生随机数填充矩阵,同 RNG::fill(mat, RNG::UNIFORM)

randn

以Normal分布产生随机数填充矩阵,同 RNG::fill(mat, RNG::NORMAL)

randShuffle

随机打乱一个一维向量的元素顺序

theRNG()

返回一个默认构造的RNG类的对象

 theRNG()::fill(...)

reduce

矩阵缩成向量

repeat

矩阵拷贝的时候指定按x/y方向重复

split

多通道矩阵分解成多个单通道矩阵

merge

多个单通道矩阵合成一个多通道矩阵

mixChannels

矩阵间通道拷贝,如Rgba[]到Rgb[]和Alpha[]

sort, sortIdx

为矩阵的每行或每列元素排序

setIdentity

设置单元矩阵

completeSymm

矩阵上下三角拷贝

inRange

检查元素的取值范围是否在另两个矩阵的元素取值之间,返回验证矩阵

checkRange

检查矩阵的每个元素的取值是否在最小值与最大值之间,返回验证结果bool

sum

求矩阵的元素和

mean

求均值

meanStdDev

均值和标准差

countNonZero

统计非零值个数

cartToPolar, polarToCart

笛卡尔坐标与极坐标之间的转换

flip

矩阵翻转

transpose

矩阵转置,比较 Mat::t() AT

trace

矩阵的迹

determinant

行列式 |A|, det(A)

eigen

矩阵的特征值和特征向量

invert

矩阵的逆或者伪逆,比较 Mat::inv()

magnitude

向量长度计算 dst(I) = sqrt(x(I)2 + y(I)2)

Mahalanobis

Mahalanobis距离计算

phase

相位计算,即两个向量之间的夹角

norm

求范数,1-范数、2-范数、无穷范数

normalize

标准化

mulTransposed

矩阵和它自己的转置相乘 AT * A, dst = scale(src - delta)T(src - delta)

convertScaleAbs

先缩放元素再取绝对值,最后转换格式为8bit型

calcCovarMatrix

计算协方差阵

solve

求解1个或多个线性系统或者求解最小平方问题(least-squares problem)

solveCubic

求解三次方程的根

solvePoly

求解多项式的实根和重根

dct, idct

正、逆离散余弦变换,idct同dct(src, dst, flags | DCT_INVERSE)

dft, idft

正、逆离散傅立叶变换, idft同dft(src, dst, flags | DTF_INVERSE)

LUT

查表变换

getOptimalDFTSize

返回一个优化过的DFT大小

mulSpecturms

两个傅立叶频谱间逐元素的乘法

The above table is quoted from: http://blog.sina.com.cn/s/blog_7908e1290101i97z.html

Please indicate the source of the reprint (this article updates the link): http://blog.csdn.net/iracer/article/details/51296631

Guess you like

Origin blog.csdn.net/jiyanghao19/article/details/130887926