人脸旋转归一化 (根据特征点定位,dlib+opencv)
来源:互联网 发布:seo的意思 编辑:程序博客网 时间:2024/06/08 07:28
找工作好无趣,所以来写几篇博客玩一玩
惯例先放结果:
思路:
人脸定位——特征点——旋转
//具体实现看代码,还是有一定提升空间的,233
下面就是简单粗暴的代码了:
环境参考博客: Dlib提取人脸特征点(68点,opencv画图)
#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> #include<algorithm>using namespace dlib;using namespace std;static cv::Rect dlibRectangleToOpenCV(dlib::rectangle r){return cv::Rect(cv::Point2i(r.left(), r.top()), cv::Point2i(r.right() + 1, r.bottom() + 1));}static dlib::rectangle openCVRectToDlib(cv::Rect r){return dlib::rectangle((long)r.tl().x, (long)r.tl().y, (long)r.br().x - 1, (long)r.br().y - 1);}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()), 2, cv::Scalar(255, 0, 0), -1);// shapes[0].part(i).x();//68个 //}int width = temp.cols;int length = temp.rows;double x = shapes[0].part(36).x() - shapes[0].part(45).x();double y = shapes[0].part(36).y() - shapes[0].part(45).y();double angle = x == 0 ? 0 : atan(y / x);cv::Mat rotation = cv::getRotationMatrix2D(cvPoint(shapes[0].part(30).x(), shapes[0].part(30).y()), angle / 3.14 * 180, 1.0);cv::warpAffine(temp, temp, rotation, cv::Size(width, length));}cimg = cv_image<bgr_pixel>(temp);faces = detector(cimg);if (faces.size() != 0) {cv::Rect rect = dlibRectangleToOpenCV(faces[0]);full_object_detection points = pose_model(cimg, faces[0]);long xmax = points.part(0).x();long xmin = points.part(0).x();long ymin = points.part(0).y();long ymax = points.part(0).y();;for (int i = 1; i < 68; i++) {xmax = std::max(xmax, points.part(i).x());xmin = std::min(xmin, points.part(i).x());ymax = std::max(ymax, points.part(i).y());ymin = std::min(ymin, points.part(i).y());}rect.x = xmin ;rect.width = xmax - xmin ;rect.y = ymin;rect.height = ymax - ymin ;cv::Mat imCrop = temp(rect);cv::resize(imCrop, imCrop, cv::Size(200, 200));imshow("Image", imCrop);cv::rectangle(temp, rect, cv::Scalar(255, 0, 0), 2);}imshow("人脸矫正", 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;system("pause");}catch (exception& e){cout << e.what() << endl;system("pause");}}
阅读全文
0 0
- 人脸旋转归一化 (根据特征点定位,dlib+opencv)
- dlib、opencv - 人脸关键点定位
- 基础知识(十三)dlib python人脸检测 特征点定位
- dlib python人脸检测 特征点定位
- Dlib + python + opencv 实时人脸68特征点提取
- opencv结合dlib进行人脸特征点的检测
- Dlib提取人脸特征点(68点,opencv画图)
- Dlib提取人脸特征点(68点,opencv画图)
- Dlib人脸特征点检测(速度优化)
- Dlib人脸特征点检测(速度优化)
- python dlib学习(二):人脸特征点标定
- python dlib学习(七):人脸特征点对齐
- dlib人脸特征点对齐
- 【Python】使用openCV与dlib实现人脸68特征点的检测与手动修改
- 结合opencv配置dlib的人脸关键点定位算法
- 利用Dlib进行人脸特征局部定位
- python︱利用dlib和opencv实现简单换脸、人脸对齐、关键点定位与画图
- Dlib机器学习库学习系列三----人脸对齐(特征点检测)
- Centos7使用yum安装程序软件时提示未找到安装包解决方法
- 分布式与集群
- Eclipse默认用外部浏览器打开Web应用
- js去空格多种方式
- 基于Redis实现分布式锁
- 人脸旋转归一化 (根据特征点定位,dlib+opencv)
- 替换空格
- 阿里云Server2012 IIS8.0 404.17错误 解决方案
- 学生成绩管理系统(C语言实现)
- 4117:简单的整数划分问题
- 路径中“/” "\" "\\"的区别
- 欢迎使用CSDN-markdown编辑器
- MaxCompute常见错误汇总(更新ing)
- 阈值分割笔记