Steps:
1. Use Opencv’s own classifier to detect human faces
Preliminary knowledge: Haar feature classifier
The Haar feature classifier is an XML file that describes the Haar feature values of various parts of the human body. This includes faces, eyes, lips, and so on. Haar feature classifier storage address: D:\ opencV \ Opencv4.0 \ Opencv4.0.0 \sources\data\ Haarcascades D:\opencv\opencv4.0\opencv4.0.0\build\etc\haarcascades
haarcascade_eye.xml ; Only open eyes can be detected haarcascade_eye_tree_eyeglasses.xml; Haarcascade_frontalcatface.xml haarCascade_frontalcatface_extende.xml haarCascade_frontalface_alt.xml haarcascade_frontalface_alt_tree.xml haarcascade_frontalface_alt2.xml; XML haarCascade_fullbody. XML haarCascade_lefteye_2Montes.xml haarcascade_licence_plate_rus_16stages.xml haarcascade_lowerbody.xml haarcascade_profileface.xml haarcascade_righteye_2splits.xml haarcascade_russian_plate_number.xml haarcascade_smile.xml haarcascade_upperbody.xmlCopy the code
DetectMultiScale in detail
void detectMultiScale(
const Mat& image,
CV_OUT vector<Rect>& objects,
double scaleFactor = 1.1.int minNeighbors = 3.int flags = 0,
Size minSize = Size(),
Size maxSize = Size()
);
Copy the code
Function description: Parameter 1: image — the image to be detected, generally grayscale image to speed up detection; Parameter 2: objects – a rectangular box vector set of detected objects; Parameter 3: scaleFactor – represents the scaling factor of the search window in two successive scans. The default is 1.1, which means that each search window is expanded by 10%. Parameter 4: minNeighbors – Indicates the minimum number of adjacent rectangles that make up the detection target (default: 3). If the number of small rectangles that make up the detection target is smaller than min_neighbors – 1, both are excluded. If min_neighbors is 0, it returns all candidate rectangles without doing anything. This setting is used in user-defined combinators for detection results. Parameter 5: Flags – Either use the default value or CV_HAAR_DO_CANNY_PRUNING. If CV_HAAR_DO_CANNY_PRUNING is set to CV_HAAR_DO_CANNY_PRUNING, then the function will use Canny edge detection to exclude areas with too many or too few edges. So these areas are not usually where the face is; Parameters 6, 7: minSize and maxSize are used to limit the range of the resulting target region. Code implementation:
#include <opencv2/opencv.hpp>
#include <iostream>
#include "windows.h"
#include <stdio.h>
#include "My_ImageProssing_base.h"
using namespace cv;
using namespace std;
// Training file path
string xmlPath = "D: \ \ opencv \ \ opencv4.0 \ \ opencv4.0.0 \ \ build \ \ etc \ \ haarcascades \ \ haarcascade_frontalface_alt2 XML";
int main(int argc, char** atgv) {
Mat img = imread("D:\ opencv_picture_test\\beauty\ Pei Jiaxin.png".0);
//Mat img = imread("D:\ opencv_picture_test\\ test2.jpg");
imshow("input image", img);
// [1] load classifiers
CascadeClassifier detector;
detector.load(xmlPath);
if(! detector.load(xmlPath)) // Load the training file
{
cout << "Cannot load the specified XML file" << endl;
return - 1;
}
[2] Detect faces and store information in rectangular faces
vector<Rect> faces;
//OpenCV Rect class usage
// If you create a Rect object, Rect has the following functions:
//rect.area(); // Return the area of rect
//rect.size(); // Return the size of rect
//rect.tl(); // Return the coordinates of the upper left vertex of rect
//rect.br(); // Return the coordinates of the lower right vertex of rect
//rect.width(); // Return the width of rect
//rect.height(); // Return the height of rect
//rect.contains(Point(x, y)); // Return a Boolean variable to determine whether rect contains a Point(x, y)
detector.detectMultiScale(img,faces, 1.1.3.0);// Call the classifier object
// [3] frame the face
for (size_t t = 0; t < faces.size(a); t++) {rectangle(img, faces[t], Scalar(0.0.255), 2.8);
// Faces [t] represents the rectangle class of the TTH face
//2 indicates the width of the line. 8 is linear. The default value is 8
}
namedWindow("Result", WINDOW_NORMAL); // Define window display properties
imshow("Result", img);
waitKey(0);
return 0;
}
Copy the code
2, some processing in the rectangular box of the face (only grayscale image processing here)
1. Mosaic (here we use mean filtering)
// create a Mosaic function
void mosaic(Mat& srcImage, Mat& dstImage, int times)
{
blur(srcImage,dstImage, Size(times*2+1, times * 2 + 1));
}
Copy the code
2, Change to another image (use resize, resize according to the area of the face) // Transform into Joe Biro
void paste(Mat& srcImage, Mat& dstImage)
{
resize(srcImage,dstImage, dstImage.size());
}
Copy the code
3. Skin smoothing (bilateral filtering)
void clearFreckle(Mat& srcImage, Mat& dstImage, int times)
{
bilateralFilter(srcImage, dstImage,times, times*2, times/2);
}
Copy the code
4. Video processing (template)
VideoCapture capture(0);
/ / VideoCapture capture (" D: \ \ opencv_picture_test \ \ videos \ \ face. Avi ");
while (1)
{
Mat img;
capture >> img; // Reads the current frame
//imshow(" original video ", img); // Displays the current frame
cvtColor(img,img, COLOR_BGR2GRAY); // Convert to grayscale
Mat dstImage = img.clone(a);[2] Detect faces and store information in rectangular faces
vector<Rect> faces;
detector.detectMultiScale(img, faces, 1.1.3.0);// Call the classifier object
// [3] Modify region information.// Complete
imshow("Processed video.", dstImage); // Displays the current frame
if(waitKey(10) > =0 ) break; / / delay 10 ms
}
Copy the code
Complete code (video + texture) :
#include <opencv2/opencv.hpp>
#include <iostream>
#include "windows.h"
#include <stdio.h>
#include "My_ImageProssing_base.h"
using namespace cv;
using namespace std;
// Training file path
string xmlPath = "D: \ \ opencv \ \ opencv4.0 \ \ opencv4.0.0 \ \ build \ \ etc \ \ haarcascades \ \ haarcascade_frontalface_alt2 XML";
// create a Mosaic function
void mosaic(Mat& srcImage, Mat& dstImage, int times)
{
blur(srcImage,dstImage, Size(times*2+1, times * 2 + 1));
}
void clearFreckle(Mat& srcImage, Mat& dstImage, int times)
{
bilateralFilter(srcImage, dstImage,times, times*2, times/2);
}
void paste(Mat& srcImage, Mat& dstImage)
{
resize(srcImage,dstImage, dstImage.size());
}
int main(a)
{
//Mat img = imread("D:\ opencv_picture_test\\beauty\\ pei-gia.png ",0);
Mat img2 = imread("D:\ opencv_picture_test\\ interesting picture \\ pig.jpg".0);
//imshow("input image", img);
// [1] load classifiers
CascadeClassifier detector;
detector.load(xmlPath);
if(! detector.load(xmlPath))// Load the training file
{
cout << "Cannot load the specified XML file" << endl;
return - 1;
}
// Call the camera
VideoCapture capture(0); Int a=1;
/ / VideoCapture capture (" D: \ \ opencv_picture_test \ \ videos \ \ face. Avi "); Int a=1;
while (1)
{
Mat img;
capture >> img; // Reads the current frame
//imshow(" original video ", img); // Displays the current frame
cvtColor(img,img, COLOR_BGR2GRAY); // Convert to grayscale
Mat dstImage = img.clone();
[2] Detect faces and store information in rectangular faces
vector<Rect> faces;
detector.detectMultiScale(img, faces, 1.1.3.0);// Call the classifier object
// [3] Modify region information
for (size_t t = 0; t < faces.size(); t++)
{
int rows = faces[t].height;
int cols = faces[t].width;
int start_y = faces[t].y;
int start_x = faces[t].x;
Mat ROI(rows, cols, CV_8UC1, Scalar(0));
Mat dstROI(rows, cols, CV_8UC1, Scalar(0));
for (int j = 0; j < rows; j++)/ / line cycle
{
for (int i = 0; i < cols; i++)/ / cycle
{
//------- [start processing each pixel] ---------------
ROI.at<uchar>(j, i) = img.at<uchar>(j + start_y, i + start_x);
//------- [End] ---------------}}// create a Mosaic
//mosaic(ROI,dstROI,10);
/ / map
paste(img2,dstROI);
// Remove freckles (skin)
//clearFreckle(ROI, dstROI, 10);
for (int j = 0; j < rows; j++)/ / line cycle
{
for (int i = 0; i < cols; i++)/ / cycle
{
//------- [start processing each pixel] ---------------
dstImage.at<uchar>(j + start_y, i + start_x) = dstROI.at<uchar>(j, i);
//------- [End] ---------------
}
}
}
imshow("Processed video.", dstImage); // Displays the current frame
if(waitKey(10) > =0 ) break; / / delay 10 ms
}
return 0;
}
Copy the code
Results show
1. Mosaic effect
2, skin grinding effect (more white face!!)
3. Be Jorbiello
The video didn’t come up, so I put up a GIF.
Refer to the link
XML, Eye_tree_eyeglasses. XML, and HaarCascade_Lefteye_2Montes.xml are distinguished by OpenCV face recognition — detectMultiScale function