Dlib提取人脸特征点(68点,opencv画图)

主要在官网给的Demo基础之上用OpenCV把特征点描绘出来了。


很早之前写过一篇配置Dlib环境的博客,现在来稍微梳理下提取特征点的使用方法。

上一篇配置环境博客地址:http://blog.csdn.net/zmdsjtu/article/details/52422847


惯例先放效果图吧:


动图如下:





接着就是简单粗暴的代码:

  1. //@zmdsjtu@163.com  
  2. //2016-12-4  
  3. //http://blog.csdn.net/zmdsjtu/article/details/53454071  
  4. #include <dlib/opencv.h>  
  5. #include <opencv2/opencv.hpp>  
  6. #include <dlib/image_processing/frontal_face_detector.h>  
  7. #include <dlib/image_processing/render_face_detections.h>  
  8. #include <dlib/image_processing.h>  
  9. #include <dlib/gui_widgets.h>  
  10.   
  11. using namespace dlib;  
  12. using namespace std;  
  13.   
  14. int main()  
  15. {  
  16.     try  
  17.     {  
  18.         cv::VideoCapture cap(0);  
  19.         if (!cap.isOpened())  
  20.         {  
  21.             cerr << "Unable to connect to camera" << endl;  
  22.             return 1;  
  23.         }  
  24.   
  25.         //image_window win;  
  26.   
  27.         // Load face detection and pose estimation models.  
  28.         frontal_face_detector detector = get_frontal_face_detector();  
  29.         shape_predictor pose_model;  
  30.         deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;  
  31.   
  32.         // Grab and process frames until the main window is closed by the user.  
  33.         while (cv::waitKey(30) != 27)  
  34.         {  
  35.             // Grab a frame  
  36.             cv::Mat temp;  
  37.             cap >> temp;  
  38.   
  39.             cv_image<bgr_pixel> cimg(temp);  
  40.             // Detect faces   
  41.             std::vector<rectangle> faces = detector(cimg);  
  42.             // Find the pose of each face.  
  43.             std::vector<full_object_detection> shapes;  
  44.             for (unsigned long i = 0; i < faces.size(); ++i)  
  45.                 shapes.push_back(pose_model(cimg, faces[i]));  
  46.       
  47.             if (!shapes.empty()) {  
  48.                 for (int i = 0; i < 68; i++) {  
  49.                     circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);  
  50.                     //  shapes[0].part(i).x();//68个  
  51.                 }  
  52.             }  
  53.             //Display it all on the screen  
  54.             imshow("Dlib特征点", temp);  
  55.   
  56.         }  
  57.     }  
  58.     catch (serialization_error& e)  
  59.     {  
  60.         cout << "You need dlib's default face landmarking model file to run this example." << endl;  
  61.         cout << "You can get it from the following URL: " << endl;  
  62.         cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;  
  63.         cout << endl << e.what() << endl;  
  64.     }  
  65.     catch (exception& e)  
  66.     {  
  67.         cout << e.what() << endl;  
  68.     }  
  69. }  

来看下上面那段代码,所有的需要的特征点都存储在Shapes里。仔细看看下面这行代码:
  1. circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);  


可以看到shpes[0]代表的是第一个人(可以同时检测到很多个人),part(i)代表的是第i个特征点,x()和y()是访问特征点坐标的途径。


每个特征点的编号如下:

在上述画图的基础上加了如下一行代码:

  1. putText(temp, to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0),1,4);  


效果图:


对照着上图,比如说想获取鼻尖的坐标,那么横坐标就是shapes[0].part[30].x(),其余的类似。


在这个的基础上就可以做很多有意思的事情啦,2333

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值