【opencv人脸识别】从图片中检测人脸
1. 从图片中检测人脸
2. 利用眼睛定位检测出倾斜的人脸
注意:本实验平台是VS2017+opencv3.4
1. 从图片中检测人脸
利用opencv3.4库中\opencv3_4\opencv\sources\data\haarcascades\文件夹下的haarcascade_frontalface_alt.xml文件,可以实现人脸检测。
haarcascade_frontalface_alt.xml是已经训练好的haar+adaboost人脸检测模型。其中,opencv提供了4种haar的人脸检测模型,具体可戳: https://blog.csdn.net/u012679707/article/details/80377387
从图片中检测人脸的代码:
// face_recog_from_picture.cpp: 定义控制台应用程序的入口点。 #include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; int main(int argc,char *argv[]) { Mat img = imread("she.jpg"); namedWindow("display"); imshow("display", img); /*********************************** 1.加载人脸检测器 ******************************/ // 建立级联分类器 CascadeClassifier cascade; // 加载训练好的 人脸检测器(.xml) const string path = "./xml/haarcascade_frontalface_alt.xml"; if ( ! cascade.load(path)) { cout << "cascade load failed!\n"; } //计时 double t = 0; t = (double)getTickCount(); /*********************************** 2.人脸检测 ******************************/ vector<Rect> faces(0); cascade.detectMultiScale(img, faces, 1.1, 2, 0 ,Size(30,30)); cout << "detect face number is :" << faces.size() << endl; /******************************** 3.显示人脸矩形框 ******************************/ if (faces.size() > 0) { for (size_t i = 0;i < faces.size();i++) { rectangle(img, faces[i], Scalar(150, 0, 0), 3, 8, 0); } } else cout << "未检测到人脸" << endl; t = (double)getTickCount() - t; //getTickCount(): Returns the number of ticks per second. cout << "检测人脸用时:" << t * 1000 / getTickFrequency() << "ms (不计算加载模型的时间)" << endl; namedWindow("face_detect"); imshow("face_detect", img); while(waitKey(0)!='k') ; destroyWindow("display"); destroyWindow("face_detect"); return 0; }
运行结果:
会发现,Selina的脸没有被识别出来。这可能是由于Selina的脸倾斜过大。下边我们想办法实现检测有一定倾斜度的脸。
附:
detectMultiScale源码解析
*/ CV_WRAP void detectMultiScale( InputArray image, CV_OUT std::vector<Rect>& objects, double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0, Size minSize = Size(), Size maxSize = Size() ); /** @overload @param image Matrix of the type CV_8U containing an image where objects are detected. 输入图像 @param objects Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image. 输出检测到的人脸矩形 @param numDetections Vector of detection numbers for the corresponding objects. An object's number of detections is the number of neighboring positively classified rectangles that were joined together to form the object. @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.缩放因子,此处为每次缩小10% @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have to retain it. 最小检测邻域 @param flags Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.此参数用于老的版本中的cascade @param minSize Minimum possible object size. Objects smaller than that are ignored. 最小检测目标的尺寸,小于这个尺寸的不检测 @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
最大检测目标的尺寸
*/