Opencv does face recognition opencv face matching analysis

  • 2020-04-01 21:31:29
  • OfStack

Machine learning
The purpose of machine learning is to transform data into information.
Machine learning turns data into information by extracting rules or patterns from the data.

Face recognition
Face recognition determines whether it is a face through the hierarchical filtering of features by cascade classifier.
The correct recognition rate of each node is high, but the correct rejection rate is low.
If any node determines that there is no face feature, the operation ends and it is declared not to be a face.
If all nodes pass, it is declared to be a face.
In industry, face recognition technology is commonly used to recognize objects.

Identify the picture
#include "opencv2/core/core.hpp" 
#include "opencv2/objdetect/objdetect.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 

#include <iostream> 
#include <stdio.h> 

using namespace std; 
using namespace cv; 

string face_cascade_name = "haarcascade_frontalface_alt.xml"; 
CascadeClassifier face_cascade; 
string window_name = " Face recognition "; 

void detectAndDisplay( Mat frame ); 

int main( int argc, char** argv ){ 
Mat image; 
image = imread( argv[1]); 

if( argc != 2 || ! ){ 
printf("[error]  No picture n"); 
return -1; 

if( !face_cascade.load( face_cascade_name ) ){ 
printf("[error]  Unable to load cascade classifier file! n"); 
return -1; 



void detectAndDisplay( Mat frame ){ 
std::vector<Rect> faces; 
Mat frame_gray; 

cvtColor( frame, frame_gray, CV_BGR2GRAY ); 
equalizeHist( frame_gray, frame_gray ); 

face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); 

for( int i = 0; i < faces.size(); i++ ){ 
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); 
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); 

imshow( window_name, frame ); 

CascadeClassifier class

The class CascadeClassifier

Cascade classifier class for detecting objects.

CascadeClassifier: : CascadeClassifier
Reads the classifier from a file.

C + + : CascadeClassifier: : CascadeClassifier (const string & filename)
The filename of the classifier file to be read with the parameter filename
parameter filename The filename of the classifier file to read
CascadeClassifier: : empty
Check if the classifier has been loaded.

C + + : bool CascadeClassifier: : empty () const
CascadeClassifier: : load
Reads the classifier from a file.

C++: bool CascadeClassifier::load(const string& filename)
The filename of the classifier file to be read with the parameter filename. The file can be an old version of the HAAR classifier model or a new version of the classifier model.
parameter filename The filename of the classifier file to read. The file can be an old version of the HAAR classifier model or a new version of the classifier model.
CascadeClassifier: : read
A classifier that reads a file storage node.

C++: bool CascadeClassifier::read(const FileNode& node)

CascadeClassifier: : detectMultiScale
Object recognition is performed on input images of different sizes, and a matrix list of recognized objects is returned.

C + + : void CascadeClassifier: : detectMultiScale (const Mat & image, vector< Rect> & objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size()) 
parameter The image The CV_8U input matrix that the wok needs to detect. objects The output vector container is used to hold the identified object matrix. scaleFactor The scroll specifies the parameters for reducing the size of each image. minNeighbors The stream specifies at least the number of adjacent elements that each candidate matrix contains. flags The message is the same as the flags for the older cascading classifier model function cvHaarDetectObjects. This parameter is not used with the new model. minSize The size of the smallest possible object. Objects smaller than that are ignored. maxSize The size of the largest possible object. Objects larger than that are ignored.
CascadeClassifier: : setImage
Sets the image to be used for detection.

C + + : bool CascadeClassifier: : setImage (Ptr< FeatureEvaluator> Feval, const Mat& image)
parameter feval The pointer to the feature evaluator used in the feature computation. The image �   CV_8U input matrix for feature detection.
This function will be CascadeClassifier: in each image: detectMultiScale () calls automatically. But if you want to manually use the CascadeClassifier::runAt() in different places, you need to call the function first so that the image is evaluated integrally.

CascadeClassifier: : runAt
Run the detection at the specified point.

C + + : int CascadeClassifier: : runAt (Ptr< FeatureEvaluator> Feval, Point pt, double& weight) 

feval A feature evaluator for feature computation.

pt The stream specifies the dot in the upper left corner of the detection window. The size of the window is the same as the size of the detected image.

If the cascade classifier detects an object in a given position, this function returns 1. Otherwise, it returns a negative index of the stage at which the candidate region has been rejected.

Using CascadeClassifier: : setImage () set up the image detection work.

Code comments :

//Cascading classifier files that need to be loaded
string face_cascade_name = "haarcascade_frontalface_alt.xml"; 
//Cascading classifier classes
CascadeClassifier face_cascade; 

// ...  

//Loads the cascade classifier and determines if the load was successful, and prints a prompt if not
if( !face_cascade.load( face_cascade_name ) ){ 
printf("[error]  Unable to load cascade classifier file! n"); 
return -1; 

// ...  

//Identify the frame of the image
face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); 

Convert to grayscale

Since the CascadeClassifier class only supports CV_8U matrix data, we need to change the image to grayscale.

CvtColor API:

Move the image from one color space to another.

C++: void cvtColor(InputArray SRC, OutputArray DST, int code, int dstCn=0)
parameter The SRC Waddled image: 8-bit unsigned, 16-bit unsigned (CV_16UC...) , or a single precision floating point data type. DST The output image is the same size and depth as the input image. code Color space conversion code. dstCn The channel number of the target image, when the parameter is 0, the channel tree is automatically calculated by SRC and code.
This function transfers the input image from one color space to another. When transforming from the RGB color space, the order of channels (RGB or BGR) should be specified explicitly. It is worth noting that in OpenCV's default color format, it is often referred to as RGB, but is actually BGR (the bytes are reversed). Thus, the first byte in a standard (24-bit) color image is an 8-bit blue component, the second byte will be green, and the third byte will be red. The fourth, fifth, and sixth bytes are the second pixel (blue, then green, then red), and so on.

Channels R, G, and B usually range in channel values :

CV_8U: 0 -- 255
CV_16U: 0 -- 65535
CV_32F: 0 -- 1
In the case of linear transformation, it doesn't matter whether there is a range or not. However, in the case of a nonlinear transformation, the input RGB image should be placed in the appropriate value range to get the correct result. For example, if you have a 32-bit floating point image directly converted to an 8-bit image without any scaling, then it will have a numeric range from 0 to 255, which is not exactly 0.. 1 the value of all floating point Numbers. So, you need to call cvtColor earlier to scale the image.

Code comments:
//Converts the frame to grayscale and outputs it to frame_gray
cvtColor( frame, frame_gray, CV_BGR2GRAY ); 

Histogram equalization

Histogram is a graphic representation of pixel intensity distribution in an image.
It counts the number of pixels in each intensity value.
< img border = 0 class = the align - center alt= / _images Histogram_Equalization_Theory_0. JPG SRC = "/ /" >  
Histogram equalization is a method to enhance image contrast by stretching the distribution range of pixel intensity.
Speak more clearly, more than the histogram, for example, you can see some pixels are mainly concentrated in the middle of the intensity values. Histogram equalization to do is to stretch the range. See below left: out of the little green circle distribution of the intensity values of pixels. Its application after equalization, got the figure in the middle of the histogram. See below equalization image on the right.
< img border = 0 class = the align - center alt= / _images Histogram_Equalization_Theory_1. JPG SRC = "/ /" >  
We use histogram equalization to enhance the contrast of the image to facilitate the cascade classifier analysis.

EqualizeHist API:

Histogram equalization is carried out for grayscale image.

C++: void equalizeHist(InputArray SRC, OutputArray DST)

The histogram equalization function USES the following values:

The source file of histogram < img border = 0 class = math Alt = H SRC = "/ /" >   .

Adjust the histogram so that the total number of squares is 255.

Integrate the histogram:

H'_i = \sum _{0 \le j

Using the < img border = 0 class = math transform images, the mapping function is: < img Alt = "border = 0 class = math \ texttt {DST} (x, y) = H '(\ texttt {SRC} (x, y))" SRC = "/ /" >.

The algorithm normalizes the brightness and increases the contrast of the image.

Related articles: