Dlib中对 One Millisecond Face Alignment with an Ensemble of Regression Trees 中的人脸对齐算法进行了实现,以下是对其源码的学习笔记
模型使用
在Dlib/example目录下的face_landmark_detection_ex文件实现了人脸对齐的简单使用方法,这一程序需要传入两个参数,一个是模型文件,一个是需要进行处理的图片文件。
流程

实现
读入两个参数
//模型序列化读入
deserialize(argv[1]) >> sp;
//图片读入
array2d<rgb_pixel> img;
load_image(img, argv[i]);
首先进行面部检测:
//建立人脸检测器
frontal_face_detector detector = get_frontal_face_detector();
//使用人脸检测器处理图片,返回矩形框位置确定人脸位置
std::vector<rectangle> dets = detector(img);
之后进行关键点检测
//建立脸部关键点检测器
shape_predictor sp;
//关键点存储方式为full_object_detection,这是一种包含矩形框和关键点位置的数据格式
std::vector<full_object_detection> shapes;
//对多个人脸的关键点进行检测,数据存入shapes中
for (unsigned long j = 0; j < dets.size(); ++j)
{
full_object_detection shape = sp(img, dets[j]);
shapes.push_back(shape);
}
其中比较关键的人脸对齐部分主要是使用了Shape-Predictor类。
这一类中的私有数据成员包括,
//一维向量形式储存的初始形状
matrix<float,0,1> initial_shape;
//多级回归所形成的回归树的森林
std::vector<std::vector<impl::regression_tree> > forests;
//存储着每一步迭代的关键点相对位置坐标,分别是关键点序号,x,y相对距离
std