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

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

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

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

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

惯例先放效果图吧:

动图如下:

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

//@zmdsjtu@163.com
//2016-12-4
//http://blog.csdn.net/zmdsjtu/article/details/53454071
#include <dlib/opencv.h>
#include <opencv2/opencv.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>

using namespace dlib;
using namespace std;

int main()
{
	try
	{
		cv::VideoCapture cap(0);
		if (!cap.isOpened())
		{
			cerr << "Unable to connect to camera" << endl;
			return 1;
		}

		//image_window win;

		// Load face detection and pose estimation models.
		frontal_face_detector detector = get_frontal_face_detector();
		shape_predictor pose_model;
		deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

		// Grab and process frames until the main window is closed by the user.
		while (cv::waitKey(30) != 27)
		{
			// Grab a frame
			cv::Mat temp;
			cap >> temp;

			cv_image<bgr_pixel> cimg(temp);
			// Detect faces 
			std::vector<rectangle> faces = detector(cimg);
			// Find the pose of each face.
			std::vector<full_object_detection> shapes;
			for (unsigned long i = 0; i < faces.size(); ++i)
				shapes.push_back(pose_model(cimg, faces[i]));
	
			if (!shapes.empty()) {
				for (int i = 0; i < 68; i++) {
					circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
					//	shapes[0].part(i).x();//68个
				}
			}
			//Display it all on the screen
			imshow("Dlib特征点", temp);

		}
	}
	catch (serialization_error& e)
	{
		cout << "You need dlib's default face landmarking model file to run this example." << endl;
		cout << "You can get it from the following URL: " << endl;
		cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
		cout << endl << e.what() << endl;
	}
	catch (exception& e)
	{
		cout << e.what() << endl;
	}
}


来看下上面那段代码,所有的需要的特征点都存储在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

最后祝大家开发愉快:)

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


评论 102
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zmdsjtu

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值