Dlib_人脸检测,代码加注释
opencv cv::Mat 彩色图像转为dlib::array2d
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/opencv/cv_image.h>
#include <iostream>
#include <opencv2/opencv.hpp>
void face_detect(const cv::Mat& img)
{
//定义人脸检测器
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
//定义窗口
dlib::image_window win;
dlib::array2d<dlib::rgb_pixel> src;
//opencv mat转换为dlib array2d
dlib::assign_image(src, dlib::cv_image<dlib::bgr_pixel>(img));
//上采样
dlib::pyramid_up(src);
//获取检测结果
std::vector<dlib::rectangle> dets = detector(src);
std::cout << "Number of faces detected: " << dets.size() << std::endl;
// Now we show the image on the screen and the face detections as
// red overlay boxes.
win.clear_overlay();
win.set_image(src);
win.add_overlay(dets, dlib::rgb_pixel(255,0,0));
std::cout << "Hit enter to process the next image..." << std::endl;
std::cin.get();
}
int main()
{
cv::Mat src = cv::imread("faces/2007_007763.jpg");
face_detect(src);
return 0;
}
每天快乐敲代码,快乐生活