Introduction
In order to recognize the face, especially the face recognition in complex environment, it is necessary to preprocess the image after capturing the face image, such as the normalization of the size and grayscale, the correction of the head posture, the image segmentation and so on. The aim is to improve image quality, eliminate noise, unify image gray value and size, and lay a good foundation for post-order feature extraction and classification recognition.
This paper will study the cutting and normalization of human face, and make a spare note.
Specific steps
Because OPENCV face recognition needs to input pictures with the same size and gray level, so it is important to recognize the first step of the work, is to face recognition needs to adjust the face picture, the specific can be divided into the following steps:
- Picture de-noising
- Grayscale of pictures
- Human face shearing (crop)
- Resize Picture (resize)
- Attitude rotation (rotate)
The strategy of human face shearing
The most basic human face shearing method I have envisioned here is the rotation and correction of the human face based on the position of the human eye.
The basic ideas are as follows:
- Rotation correction of the image according to the tilt angle of the two eyes
- Zoom the picture based on the actual distance in the picture and the custom offset
Here is the cut, the final picture is 100*100 pixels, the offset is 0.3
Detecting face
Cropping Result
Its code is:
Cv::mat Faceclassifer::cropfacesbasedoneye (Cv::rect facerect, CV::P oint LEFTEYE,CV::P oi NT Righteye, float offset,int outputwidth,int outputheight) {int offset_h = floor (of Fset * outputwidth); int offset_v = Floor (offset * outputheight); int eyegap_h = righteye.x-lefteye.x; int eyegap_v = RIGHTEYE.Y-LEFTEYE.Y; float eye_distance = sqrt (Pow (eyegap_h,2) +pow (eyegap_v,2)); float eye_reference = outputwidth-2*offset_h; float scale = eye_distance/eye_reference; Rotate original around the left eye Cv::mat Rotatedimage; if (Eyegap_v! = 0) {Double rotation = atan2f ((float) eyegap_v, (float) eyegap_h); Double degree = ROTATION*180/CV_PI; Rotateface (_image, Lefteye, Degree, rotatedimage); }//crop The rotated image cv::P oint crop_xy (lefteye.x-scale*offset_h,lefteye.y-scale*offset_v); Cv::size crop_size (Outputwidth*scale, Outputheight*scale); Cv::rect Crop_area(Crop_xy, crop_size); Cv::mat Cropface; if (Eyegap_v = = 0) Cropface = _image (Crop_area); else Cropface = Rotatedimage (Crop_area); Resize the Face Cv::resize (Cropface,cropface,cv::size (outputwidth,outputheight)); Cv::mat Croppedgray; Cv::cvtcolor (Cropface,croppedgray,cv_bgr2gray); Cv::equalizehist (Croppedgray, Croppedgray); return Croppedgray;}
Explain the code:
- The offset is the distance between the eye and the left and right edge of the image, such as 0.3 of the offset, then left eye to left edge, right eye to starboard edge of the length of the cut face picture width of 0.3, the same as the distance to the upper boundary also accounted for the height of the image 0.3.
- The image is scaled according to the actual distance of the two eyes and the ratio of the two eyes to the width of the image, such as the actual distance is a Euclidean distance eye_distance, the reference distance eye_reference is the output width outputwidth minus the left eye to the left edge of 0.3 Outputwidth, minus the right eye to the right edge of the 0.3 outputwidth.
- Because the final face recognition, need to output grayscale, so, the final return value is grayscale and histogram equalization of the picture
Image Rotation API
Here the picture is rotated using the OPENCV function, Warpaffine ().
This involves affine transformations, which can be referred to OPENCV official documents, radiation transformations.
void FaceClassifer::rotateFace(cv::Mat& src,cv::Point& pt,double angle,cv::Mat& dst){ cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0); cv::warpAffine(src, dst, r, cv::Size(src.cols,src.rows));}
Here I take the center point of the left eye as the input origin of the rotation, and cv::getrotationmatrix2d () generates a 2 * 3 matrix representing the affine transformation.
Reprint please indicate the author Jason Ding and its provenance
GitHub home page (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
"Computer vision" cuts and normalization of human faces detected