c++ opencv4.6 做人脸矫正的代码
时间: 2023-12-31 10:05:48 浏览: 108
opencv4.6 windows c++ exe
以下是一个使用 OpenCV 4.6 实现人脸矫正的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
try
{
// 加载人脸检测器和人脸关键点检测器
String face_detector_path = "haarcascade_frontalface_default.xml";
CascadeClassifier face_detector;
face_detector.load(face_detector_path);
String landmarks_detector_path = "shape_predictor_68_face_landmarks.dat";
dlib::shape_predictor landmarks_detector;
dlib::deserialize(landmarks_detector_path) >> landmarks_detector;
// 加载需要处理的图像
Mat img = imread("test.jpg");
// 检测人脸
vector<Rect> faces;
face_detector.detectMultiScale(img, faces, 1.1, 3, 0, Size(100, 100));
// 对每个人脸进行处理
for (auto&& face : faces)
{
// 提取人脸关键点
Mat face_roi = img(face);
dlib::cv_image<dlib::bgr_pixel> dlib_face(face_roi);
dlib::full_object_detection shape = landmarks_detector(dlib_face);
// 计算人脸矩阵
Point2f src_points[3];
Point2f dst_points[3];
src_points[0] = Point2f(shape.part(36).x(), shape.part(36).y());
src_points[1] = Point2f(shape.part(45).x(), shape.part(45).y());
src_points[2] = Point2f(shape.part(30).x(), shape.part(30).y());
dst_points[0] = Point2f(0, img.rows - 1);
dst_points[1] = Point2f(img.cols - 1, img.rows - 1);
dst_points[2] = Point2f(img.cols / 2, img.rows / 2);
Mat M = getAffineTransform(src_points, dst
阅读全文