cpp
复制代码
// 包含OpenCV库中有关对象检测的头文件
#include "opencv2/objdetect.hpp"
// 包含OpenCV库中有关高层GUI函数的头文件
#include "opencv2/highgui.hpp"
// 包含OpenCV库中有关图片处理的头文件
#include "opencv2/imgproc.hpp"
// 包含输入输出流库
#include <iostream>
// 包含C标准输入输出库
#include <cstdio>
// 包含向量容器库
#include <vector>
// 包含算法库
#include <algorithm>
// 使用标准库命名空间
using namespace std;
// 使用OpenCV命名空间
using namespace cv;
// 用于人脸特征点检测的函数声明
static void help(char** argv);
static void detectFaces(Mat&, vector<Rect_<int> >&, string);
static void detectEyes(Mat&, vector<Rect_<int> >&, string);
static void detectNose(Mat&, vector<Rect_<int> >&, string);
static void detectMouth(Mat&, vector<Rect_<int> >&, string);
static void detectFacialFeaures(Mat&, const vector<Rect_<int> >, string, string, string);
// 保存输入图片路径和各个特征的Haarcascade分类器路径的字符串变量
string input_image_path;
string face_cascade_path, eye_cascade_path, nose_cascade_path, mouth_cascade_path;
// 主函数
int main(int argc, char** argv)
{
// 解析命令行参数
cv::CommandLineParser parser(argc, argv,
"{eyes|data/haarcascades/haarcascade_eye.xml|}{nose||}{mouth||}{help h||}{@image|lena.jpg|}{@facexml|data/haarcascades/haarcascade_frontalface_alt2.xml|}");
// 如果有help参数,则显示帮助信息
if (parser.has("help"))
{
help(argv);
return 0;
}
// 获取从命令行中解析出来的参数值,赋值给相应的变量
input_image_path = parser.get<string>("@image");
face_cascade_path = parser.get<string>("@facexml");
eye_cascade_path = parser.has("eyes") ? parser.get<string>("eyes") : "";
nose_cascade_path = parser.has("nose") ? parser.get<string>("nose") : "";
mouth_cascade_path = parser.has("mouth") ? parser.get<string>("mouth") : "";
// 检查必要的参数是否已经被指定
if (input_image_path.empty() || face_cascade_path.empty())
{
cout << "IMAGE or FACE_CASCADE are not specified";
return 1;
}
// 加载图片和级联分类器文件
Mat image;
image = imread(samples::findFile(input_image_path));
// 检测人脸和人脸特征点
vector<Rect_<int> > faces;
detectFaces(image, faces, face_cascade_path);
detectFacialFeaures(image, faces, eye_cascade_path, nose_cascade_path, mouth_cascade_path);
// 显示检测结果
imshow("Result", image);
// 等待任意按键的按下
waitKey(0);
return 0;
}
// 帮助函数,在有help参数的情况下输出帮助信息
static void help(char** argv)
{
cout << "\nThis file demonstrates facial feature points detection using Haarcascade classifiers.\n"
"The program detects a face and eyes, nose and mouth inside the face."
"The code has been tested on the Japanese Female Facial Expression (JAFFE) database and found"
"to give reasonably accurate results. \n";
cout << "\nUSAGE: " << argv[0] << " [IMAGE] [FACE_CASCADE] [OPTIONS]\n"
"IMAGE\n\tPath to the image of a face taken as input.\n"
"FACE_CASCSDE\n\t Path to a haarcascade classifier for face detection.\n"
"OPTIONS: \nThere are 3 options available which are described in detail. There must be a "
"space between the option and it's argument (All three options accept arguments).\n"
"\t-eyes=<eyes_cascade> : Specify the haarcascade classifier for eye detection.\n"
"\t-nose=<nose_cascade> : Specify the haarcascade classifier for nose detection.\n"
"\t-mouth=<mouth-cascade> : Specify the haarcascade classifier for mouth detection.\n";
cout << "EXAMPLE:\n"
"(1) " << argv[0] << " image.jpg face.xml -eyes=eyes.xml -mouth=mouth.xml\n"
"\tThis will detect the face, eyes and mouth in image.jpg.\n"
"(2) " << argv[0] << " image.jpg face.xml -nose=nose.xml\n"
"\tThis will detect the face and nose in image.jpg.\n"
"(3) " << argv[0] << " image.jpg face.xml\n"
"\tThis will detect only the face in image.jpg.\n";
cout << "\n\nThe classifiers for face and eyes can be downloaded from : "
" \nhttps://github.com/opencv/opencv/tree/4.x/data/haarcascades";
cout << "\n\nThe classifiers for nose and mouth can be downloaded from : "
" \nhttps://github.com/opencv/opencv_contrib/tree/4.x/modules/face/data/cascades\n";
}
// 检测人脸的函数
static void detectFaces(Mat& img, vector<Rect_<int> >& faces, string cascade_path)
{
// 创建级联分类器对象
CascadeClassifier face_cascade;
// 加载用于人脸检测的级联分类器文件
face_cascade.load(samples::findFile(cascade_path));
// 如果分类器没有加载失败,使用它来检测人脸
if (!face_cascade.empty())
face_cascade.detectMultiScale(img, faces, 1.15, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
return;
}
// 检测人脸特征的函数(包括眼睛、鼻子、嘴巴)
static void detectFacialFeaures(Mat& img, const vector<Rect_<int> > faces, string eye_cascade,
string nose_cascade, string mouth_cascade)
{
// 遍历每一个检测到的人脸区域
for(unsigned int i = 0; i < faces.size(); ++i)
{
// 获取单个人脸的边界框
Rect face = faces[i];
// 画出人脸边界框
rectangle(img, Point(face.x, face.y), Point(face.x+face.width, face.y+face.height),
Scalar(255, 0, 0), 1, 4);
// 眼睛、鼻子和嘴巴将会在人脸这个区域(感兴趣区域)内检测
Mat ROI = img(Rect(face.x, face.y, face.width, face.height));
// 判断是否进行完整的特征点检测
bool is_full_detection = false;
if( (!eye_cascade.empty()) && (!nose_cascade.empty()) && (!mouth_cascade.empty()) )
is_full_detection = true;
// 如果用户提供了眼睛分类器,则检测眼睛
if(!eye_cascade.empty())
{
vector<Rect_<int> > eyes;
detectEyes(ROI, eyes, eye_cascade);
// 画出眼睛的圆形区域点
for(unsigned int j = 0; j < eyes.size(); ++j)
{
Rect e = eyes[j];
circle(ROI, Point(e.x+e.width/2, e.y+e.height/2), 3, Scalar(0, 255, 0), -1, 8);
// 下面被注释的部分是画出眼睛矩形区域
/* rectangle(ROI, Point(e.x, e.y), Point(e.x+e.width, e.y+e.height),
Scalar(0, 255, 0), 1, 4); */
}
}
// 如果用户提供了鼻子分类器,则检测鼻子
double nose_center_height = 0.0;
if(!nose_cascade.empty())
{
vector<Rect_<int> > nose;
detectNose(ROI, nose, nose_cascade);
// 画出鼻子的圆形区域点
for(unsigned int j = 0; j < nose.size(); ++j)
{
Rect n = nose[j];
circle(ROI, Point(n.x+n.width/2, n.y+n.height/2), 3, Scalar(0, 255, 0), -1, 8);
nose_center_height = (n.y + n.height/2);
}
}
// 如果用户提供了嘴巴分类器,则检测嘴巴
double mouth_center_height = 0.0;
if(!mouth_cascade.empty())
{
vector<Rect_<int> > mouth;
detectMouth(ROI, mouth, mouth_cascade);
// 画出嘴巴的矩形区域
for(unsigned int j = 0; j < mouth.size(); ++j)
{
Rect m = mouth[j];
mouth_center_height = (m.y + m.height/2);
// 如果是进行完整的特征检测,嘴巴应该位于鼻子下面
if( (is_full_detection) && (mouth_center_height > nose_center_height) )
{
rectangle(ROI, Point(m.x, m.y), Point(m.x+m.width, m.y+m.height), Scalar(0, 255, 0), 1, 4);
}
else if( (is_full_detection) && (mouth_center_height <= nose_center_height) )
continue;
else
rectangle(ROI, Point(m.x, m.y), Point(m.x+m.width, m.y+m.height), Scalar(0, 255, 0), 1, 4);
}
}
}
return;
}
// 检测眼睛的函数
static void detectEyes(Mat& img, vector<Rect_<int> >& eyes, string cascade_path)
{
// 创建级联分类器对象
CascadeClassifier eyes_cascade;
// 加载用于眼睛检测的级联分类器文件
eyes_cascade.load(samples::findFile(cascade_path, !cascade_path.empty()));
// 如果分类器没有加载失败,使用它来检测眼睛
if (!eyes_cascade.empty())
eyes_cascade.detectMultiScale(img, eyes, 1.20, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
return;
}
// 检测鼻子的函数
static void detectNose(Mat& img, vector<Rect_<int> >& nose, string cascade_path)
{
// 创建级联分类器对象
CascadeClassifier nose_cascade;
// 加载用于鼻子检测的级联分类器文件
nose_cascade.load(samples::findFile(cascade_path, !cascade_path.empty()));
// 如果分类器没有加载失败,使用它来检测鼻子
if (!nose_cascade.empty())
nose_cascade.detectMultiScale(img, nose, 1.20, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
return;
}
// 检测嘴巴的函数
static void detectMouth(Mat& img, vector<Rect_<int> >& mouth, string cascade_path)
{
// 创建级联分类器对象
CascadeClassifier mouth_cascade;
// 加载用于嘴巴检测的级联分类器文件
mouth_cascade.load(samples::findFile(cascade_path, !cascade_path.empty()));
// 如果分类器没有加载失败,使用它来检测嘴巴
if (!mouth_cascade.empty())
mouth_cascade.detectMultiScale(img, mouth, 1.20, 5, 0|CASCADE_SCALE_IMAGE, Size(30, 30));
return;
}