当前位置:   article > 正文

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

face 68点图
//女票被学妹约出去看电影了,所以有点无聊的我来写博客了。

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

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

上一篇配置环境博客地址:Dlib+opencv实时提取人脸轮廓(windows环境下//Dlib配置入门)_朱铭德的博客-CSDN博客_人脸轮廓

惯例先放效果图吧:

动图如下:

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

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


来看下上面那段代码,所有的需要的特征点都存储在Shapes里。仔细看看下面这行代码:

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()是访问特征点坐标的途径。

每个特征点的编号如下:

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

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

最后祝大家开发愉快:)

//顺便祝女票大人和学妹看电影愉快(摊手)


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/100605
推荐阅读
相关标签
  

闽ICP备14008679号