OpenCV basic Facial Recognition Knowledge details, opencv basic Facial Recognition Knowledge

Source: Internet
Author: User

OpenCV basic Facial Recognition Knowledge details, opencv basic Facial Recognition Knowledge
Preface

OpenCV has added a FaceRecognizer class since version 2.4. It can be used for face recognition conveniently (source code, in opencv_contrib/modules/face/src of opencv_contrib ).

Currently, three algorithms are supported:
1. Eigen Faces feature face: EigenFaceRecognizer
2. Fisher Faces: FisherFaceRecognizer
3. Local Binary Pattern Histograms (partial Binary histogram): LBPHFaceRecognizer

Eigen Faces feature face

Feature face EigenFace is equivalent to converting a face from the pixel space to another space, and calculating similarity in another space. In this case, the basic concepts of image recognition are the same. First, select an appropriate sub-space and convert all the images to this sub-space, then, we can measure similarity or perform classification learning in this sub-space. Transform to another space. images of the same category will converge, and images of different categories will be far away from each other, or in the original pixel space, it is difficult for different types of images to be separated by a simple line or plane. If they are then transformed to another space, they can be separated. Sometimes linear classifier can easily separate them.

The space transformation method selected by EigenFace is PCA, which is the famous principal component analysis. It is widely used in Preprocessing to eliminate the correlation between sample feature dimensions. The EigenFace method uses PCA to obtain the main component of face distribution. The specific implementation is to decompose the covariance matrix of all face images in the training set, and obtain the corresponding feature vector, these feature vectors are "feature faces ". Each feature vector or feature face is equivalent to capturing or describing a change or feature between faces. This means that each face can be expressed as a linear combination of these feature faces. After PCA, the space is based on each feature face or feature vector. In this space (or coordinate axis), each face is a point, the coordinates of this point are the projection coordinates of the face under each feature base.

The following describes how to implement Face Recognition Based on Feature faces:

Step 1: Obtain the set S containing M face images. In our example, there are 25 face images, as shown in. Each image can be converted into an n-dimensional vector (yes, yes, just arrange one pixel and one pixel in a line, so you can obtain the original image's pixels horizontally or vertically, as you can, and put the M vectors in a set of S, as shown in the following formula.

Step 2: After obtaining the face Vector Set S, calculate the average image watermark. The formula for calculating the average image is as follows. It is to traverse the vectors in the Set S and accumulate them, and then take the average value. The resulting attention is actually quite interesting. Attention is actually an n-dimensional vector. If we restore it back to the image form, we can get the following "average face ".

Step 3: calculate the difference between each image and the average image, that is, subtract the average value of step 2 from each element in the S set.

Step 4: Find the M orthogonal unit vector un, which is used to describe the distribution of PHI (difference value in step 3. In the un, the k (k = 1, 2, 3... M) the vector uk is calculated in the following formula,

When this λ k (a feature value in the original text) is used to obtain the minimum value, the uk is basically determined. Just now, I also said that the M vectors are orthogonal to each other and the unit length. Therefore, uk must satisfy the following formula:

The above equation makes uk a unit orthogonal vector. The above uk is actually the feature vector for calculating the following covariance matrix:

Where

For an NxN (such as 100x100) dimension image, the calculation of the feature vector directly calculated above is too large (the covariance matrix can reach 10000x10000 ), therefore, the following simple calculations are available.

Step 4: if the number of training images is smaller than the image dimension, for example

OpenCV source code eigen_faces.cpp

Void Eigenfaces: train (InputArrayOfArrays _ src, InputArray _ local_labels)

Void Eigenfaces: predict (InputArray _ src, Ptr collector)

Obviously, 10 feature vectors (Note: One feature vector can be transformed into a feature face, and the feature vectors and feature face concepts are somewhat similar here) are not enough, 50 feature vectors can effectively encode important facial features. In the AT&T database, when 300 feature vectors are used, a better and reconstructed result can be obtained. A theorem can be used to determine the number of face features required for reconstruction, but it is heavily dependent on input data.

Fisher Faces

The LDA (Linear Discriminant Analysis, Linear Discriminant Analysis) Theory Based on Fisherface has similarities with PCA used in feature faces. It is a method to reduce the overall dimensionality of original data to low-dimensional space, LDA and PCA both start from the data as a whole, but are different from the Local Texture Feature Extraction by using LDA.

Dataset type 2

Generally, the face to be matched must match multiple faces in the face database, so this is a case of multiclass classification. For the sake of simplicity, We can first introduce the second-class situation and then expand it to multiple categories. Assume that there are two point sets x on the two-dimensional plane (x is a two-dimensional vector containing the horizontal and vertical coordinates), and their distribution is as follows (1) (the data is represented by blue and red points respectively ):

The original data is a two-dimensional data distributed on the plane. What should we do if we want to use a one-dimensional volume (such as the distance to the DOT) to reasonably represent and partition the data? An effective method is to find a suitable vector w (with the same dimension as the data) and project the data to w (a scalar is obtained, an intuitive understanding is the distance between the projection point and the coordinate origin). The original data is expressed and distinguished based on the projection point. Returns the distance from the projection point to the origin using a mathematical formula: y = wTx. Figure (1) shows two w schemes. w is represented by a straight line starting from the origin. The point on the straight line is the projection point of the original data. It is better to intuitively judge w on the right side, and the projection points on it can reasonably distinguish the original two datasets. But the computer does not know this, so there must be a definite method to calculate this w.

First, calculate the mean (center point) of each type of data ):

Here I is the number of data categories. Ni represents the number of data points under a specific category. For example, u1 represents the center of the red dot, and u2 represents the center of the blue dot.

The center where data points are projected to w is:

How to judge the best vector w can be considered from two aspects: 1. The projection points obtained from different classifications should be separated as much as possible; 2. The points obtained from projection of the same classification should be aggregated as much as possible. The following formula can be defined:

J (w) represents the distance between different classification projection centers. The larger the value, the better.

The above formula is called the scatter matrixs, which represents the hash value after the projection of the same classification, that is, the degree of polymerization of the projection point. The smaller the value, the more the projection point is aggregated.

Combining the two formulas, the first one is used as the denominator of the numerator:

The above formula is the w function, and the larger the value, the better the w dimension reduction performance, so the following problem is to solve the problem of making the above formula take the maximum w.

Expand the hash function:

We can find that, except w and w ^ T, the rest can be defined:

This is actually the hash matrix of the original data, right. For a fixed dataset, its hash matrix is also determined.

Other definitions:

Sw is called Within-class scatter matrix.

Return to and use the two definitions above for replacement:

Expand the J (w) molecule and define SB. SB is called Between-class scatter.

In this way, the final representation of J (w) is obtained:

The maximum value obtained above can be obtained by using the Laplace multiplier method, but you need to limit the value of the denominator. Otherwise, the denominator of the numerator changes. How can we determine the best w. The following result can be obtained by using the Laplace multiplier method:

W is a matrix, so it can be used as a guide. (I do not understand this)

The two sides of the above formula share the multiplication to get:

We can see that w is actually the feature vector of the matrix, right.

Solving w through the above formula is still somewhat difficult, and w has multiple solutions. Consider the following formula:

Bring it to the following formula:

Where λ w is the value of w as the variable, because (U1-U2) ^ T and w are of the same dimension, the former is the row vector and the column vector. Continue with the previous formula:

As w expands to any number of times without affecting the results, the unknown constants λ and λ w can be gone about twice (suspect ):

Here, w can be a simple solution.

2. multiple types of datasets

This part is the core of this blog. Assume that a person has a face image of C, and each person can have multiple images. Therefore, the image can be divided into Class C by person. This section is about how to identify the class C. Before discrimination, we need to process the image first. Each image is obtained row by row and column to form a vector. Similar to the first section, we set the vector to x and the vector dimension to n, set x as the column vector (n rows and 1 column ).

Different from the simple two-dimensional data classification in Section 1, n may have thousands or thousands. For example, the vector obtained from an image of 10000 X is dimension, therefore, the method for projection x into a vector in Section 1 may not be applicable, for example:

Figure (2)

A proper vector cannot be found in the plane, and all data can be projected to this vector and different classes are reasonably separated. So we need to increase the number of projection vector w (of course, the dimension of each vector is the same as the data, otherwise how can we project it), set w:

W1 and w2 are n-dimensional column vectors, So w is a matrix of n rows and k columns. Here k can be selected as needed, as long as the original data can be reasonably characterized. The projection of x on w can be expressed:

So here y is the column vector of k dimension.

As in the previous section, we will consider the optimal w from the projection of the class hash and the class hash, and consider the graph (2) two-dimensional data is divided into three categories. Similar to Section 1, μ I still represents the center of Category I, and Sw is defined as follows:

Where:

Class I represents the class hash degree, which is a nxn matrix.

The central μ of all x is defined:

The definition of hash between classes is quite different from that in the previous section:

Represents the sum of the distances from each category to μ. Note that Ni represents the number of x in Category I, that is, the number of face images of a person.

The above discussion is about the various data between projections, and the calculation of J (w) actually relies on the data distribution after projection, so there are:

The center of Class I after projection, the center of all data, the intra-class hash matrix, and the inter-class hash matrix. Similar to the preceding section, J (w) can be defined:

Recall the formula J (w) in the previous section. The numerator is two kinds of center distance, and the denominator is the hash degree of each class. Now the Projection Direction is multi-dimensional (several straight lines), and the numerator needs to make some changes. We do not calculate the sum of the center distance of the two samples (this is useless to describe the degree of dispersion between classes ), instead, calculate the sum of hash degrees between each type of center and the entire sample center. Get:

Finally:

Or solve the feature vector of the matrix, and then obtain the feature vector with the largest first k feature values as required.

Note:

Because the (μ I-μ) rank of SB is 1, most of the SB is C (the rank of the matrix is less than or equal to the sum of the sums ). And because of knowing the first C-1 μ I, the last μ c can be linearly expressed with the front of the μ I, so the rank of SB is mostly C-1, therefore, the number of feature vectors in the matrix is mostly C-1. Since C is the category of the dataset, assuming there are N photos, you can get at most N-1 feature vectors to characterize the original data. (Suspect)

If you have read the PCA theoretical analysis in the previous article, you will know that the feature vectors obtained in PCA are orthogonal, but what we get here is not symmetric, therefore, the obtained K feature vectors are not necessarily orthogonal, which is the biggest difference between LDA and PCA.

As mentioned above, if k feature vectors are obtained in a face set and restored to a face image, it is like the following:

After obtaining k feature vectors, how can we match a person's face and whether the face in the database is similar? The method is to project the face on k feature vectors, obtain the column vector or row vector in k dimension, obtain the Euclidean distance from the existing projection, and determine whether to match according to the threshold value. The specific methods are available in the typical Face Recognition Algorithm 1: feature face method (Eigenface). You can view them. It should be noted that the LDA and PCA methods are sensitive to illumination. If you use images with even illumination as a basis to identify non-uniformity, it will be miserable.

OpenCV source code: fisher_faces.cpp

Void Fisherfaces: train (InputArrayOfArrays _ src, InputArray _ local_labels)

Void Fisherfaces: predict (InputArray _ src, Ptr collector)

Local Binary Pattern Histograms

Eigenfaces and Fisherfaces use the overall method for face recognition. You treat your data as a high-dimensional vector of the image space. We all know that high-dimensional data is poor, so a low-dimensional sub-space is identified and information may be well preserved. Eigenfaces is used to maximize the total divergence, which may cause that when the variance is generated by external conditions, the principal component of the maximum variance is not suitable for classification. Therefore, we use the LDA method to optimize the use of some identification analysis. The Fisherfaces method works well, at least when we assume that the model is finite.

Real life is imperfect. You cannot guarantee that the lighting conditions in your images are perfect, or that 10 photos of a person are taken. So what if each person only has one photo? The covariance estimation method of our sub-spaces may be completely incorrect, so recognition may also be wrong. Do you remember that Eigenfaces achieved a 96% recognition rate in the AT&T database? How many training images do we need for such an effective estimation? It is the preferred recognition rate of Eigenfaces and Fisherfaces in the AT&T database. This is a simple database:

Therefore, if you want a good recognition rate, you need 8 (7 ~ 9) images, but Fisherfaces does not help. The above experiment shows the cross-validation results of 10 images, using the facerec framework: https://github.com/bytefish/facerec.
Local Binary Patterns Histograms (Local Binary Pattern) is used to extract Local features as the basis for discrimination. The significant advantage of the lmalb method is that it is not sensitive to light, but it still does not solve the problem of posture and expression. However, compared with the feature face method, the recognition rate of lmalb has been greatly improved. Some face databases have a recognition rate of over 98%.

KNN Feature Extraction

The initial lmalb is defined in the 3x3 neighborhood of the pixel. The gray value of the adjacent 8 pixels is compared with the neighboring center pixel as the threshold value, if the surrounding pixel value is greater than the center pixel value, the position of the pixel is marked as 1; otherwise, the value is 0. In this way, the 8 points in the 3x3 neighborhood can generate 8-bit binary numbers (usually converted to a decimal number, that is, the lhuge number, a total of 256 types ), then, we can obtain the lhuge value of the center pixel in the neighborhood, and use this value to reflect the texture information of the region. As shown in:

Use a more formal formula to define the statement:

It represents the central element in the 3x3 neighborhood, its pixel value is ic, and ip represents the value of other pixels in the neighborhood. S (x) is a symbolic function and is defined as follows:

Improved version

(1) Circular HSV Operator

The biggest drawback of the basic lmalb operator is that it only covers a small area within a fixed radius, which obviously cannot meet the needs of texture of different sizes and frequencies. In order to adapt to texture features of different scales and meet the grayscale and rotation immutability requirements, Ojala and others have improved the KNN operator and extended the 3 × 3 neighbor to any neighbor, the circular neighbor is used to replace the square neighborhood. The improved KNN operator allows any number of pixels in the circular neighborhood with a radius of R. In this way, we obtain the HSV operator with P sampling points in the circular area with a radius of R. For example, a 5x5 neighbor is set:

There are eight black sampling points. The values of each sampling point can be calculated as follows:

It is the center of the neighboring area, which is a sample point. The preceding formula can be used to calculate the coordinates of any sampling point, but the obtained coordinates are not necessarily integers. Therefore, the pixel values of the sampling point can be obtained through bilinear interpolation:

(2) The equivalence Mode

Different binary modes can be generated for a single HSV operator. For a round area with a radius of R containing P sampling points, a 2 ^ P mode will be generated. Obviously, with the increase of the number of sampling points in the neighborhood set, the binary pattern type increases sharply. For example, there are 20 sampling points in the 5*5 neighborhood, and there are 220 = 1,048,576 binary modes. Such a large number of binary modes are not good for texture extraction, texture recognition, classification, and information access. At the same time, too many pattern types are unfavorable for texture expression. For example, when we use the lboperator for texture classification or face recognition, we often use the statistical histogram of the lbmode to express the image information. A large number of pattern types will make the data volume too large, and the histogram is too sparse. Therefore, we need to reduce the dimensionality of the original HSV mode so that the image information is best represented when the data volume is reduced.
In order to solve the problem of too many binary modes and improve the statistical performance, Ojala proposes to use an "Uniform Pattern" to reduce the dimension of the Pattern type of the HSV operator. Ojala and others believe that the vast majority of image models in the image can only contain two hops from 1 to 0 or from 0 to 1 at most. Therefore, Ojala defines the "equivalent mode" as: when the number of cyclic binary values corresponding to a piece of BPS ranges from 0 to 1 or from 1 to 0, there can be a maximum of two hops, the binary value corresponding to this GUID is called an Equivalent schema class. For example, 00000000 (0 hops), 00000111 (only one jump from 0 to 1), 10001111 (first jump from 1 to 0, then jump from 0 to 1, two hops in total) are equivalent mode classes. All modes except the equivalent mode class are classified into another type, called the hybrid mode class, for example, 10010111 (four hops in total ). For example, several equivalent modes are provided.

With this improvement, the binary mode is greatly reduced without any information loss. The number of modes is reduced from the original 2 P to P (P-1) + 2, where P indicates the number of sampling points in the neighborhood set. For 8 sampling points in a 3 × 3 neighborhood, the binary mode is reduced from the original 256 to 58, which reduces the dimension of the feature vector and reduces the impact of high-frequency noise.

By using the preceding method, each pixel obtains a guid Based on the neighbor information. If it is displayed as an image, it is obvious that the image is robust to illumination.

KNN Feature Matching

If we use the previously obtained HSV value directly for face recognition, there is actually no difference between it and the feature extraction, which will cause a series of problems such as computational accuracy. In [1] of the document, a face image is divided into sub-areas (for example, 7x7), and its histogram is calculated based on the HSV value in the sub-area, and the histogram is used as its discriminant feature. The advantage of doing so is to avoid incomplete image targeting within a certain range, and also perform dimensionality reduction for the feature.

There are multiple ways to identify the similarity of the obtained histogram feature. If we know that the face histogram is Mi and the face histogram to be matched is Si, we can use:

(1) histogram crossover Kernel Method

(2) chi-square Statistical Method

OpenCV source code lbph_faces.cpp

Void lalb: train (InputArrayOfArrays _ in_src, InputArray _ in_labels, bool preserveData)

Void lalb: predict (InputArray _ src, Ptr collector)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.