人脸对齐旋转人脸(opencv)

根据对齐点将人脸进行旋转

#include <cv.h>
#include <highgui.h>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat_<double> LoadGroundTruthShape(string filename);


int main0()
{
    Mat img = imread("D:\\image_070.jpg"); // read img
    Mat_<double> shape = LoadGroundTruthShape("D:\\image_070.pts"); //read shape
    for (int j = 0; j <68; j++){
        circle(img, Point2d(shape(j, 0), shape(j, 1)), 3, Scalar(255, 255, 255), -1, 8, 0);
    }
    // two points on the position of eyes
    circle(img, Point2d(shape(38, 0), shape(38, 1)), 3, Scalar(0, 255, 255), -1, 8, 0);
    circle(img, Point2d(shape(44, 0), shape(44, 1)), 3, Scalar(0, 255, 255), -1, 8, 0);

    // the eye_center point of two eyes
    Point2d eyesCenter = Point2d((shape(38, 0) + shape(44, 0)) * 0.5f, (shape(38, 1) + shape(44, 1)) * 0.5f);
    // the nose center
    Point2d noseCenter = Point2d(shape(33, 0), shape(33, 1));
    circle(img, noseCenter, 3, Scalar(0, 0, 255), -1, 8, 0);
    // calculate the angle
    double dy = shape(44, 1) - shape(38, 1);
    double dx = shape(44, 0) - shape(38, 0);
    double angle = atan2(dy, dx) * 180.0 / CV_PI; 
    std::cout << "angle=" << angle << endl;
    // rotation matrix
    Mat rot_mat = getRotationMatrix2D(noseCenter, angle, 0.5);
    Mat rot;
    // affine tranform
    warpAffine(img, rot, rot_mat, img.size());
    // compute new landmark
    vector<Point2f> marks;
    for (int n = 0; n<68; n++)
    {
        Point2f p = Point2f(0, 0);
        p.x = rot_mat.ptr<double>(0)[0] * shape(n, 0) + rot_mat.ptr<double>(0)[1] * shape(n, 1) + rot_mat.ptr<double>(0)[2];
        p.y = rot_mat.ptr<double>(1)[0] * shape(n, 0) + rot_mat.ptr<double>(1)[1] * shape(n, 1) + rot_mat.ptr<double>(1)[2];
        marks.push_back(p);
    }
    // plot new landmark
    for (int j = 0; j < 68; j++)
    {
        circle(rot, marks[j], 2, Scalar(255, 0, 0));
    }

    imwrite("D:\\res.jpg", rot);
    imshow("img", img);
    imshow("rot", rot);
    waitKey(0);
    destroyAllWindows();
    return 0;
}

Mat_<double> LoadGroundTruthShape(string filename){
    Mat_<double> shape(68, 2);
    ifstream fin;
    string temp;
    fin.open(filename);
    getline(fin, temp);
    getline(fin, temp);
    getline(fin, temp);
    for (int i = 0; i<68; i++){
        fin >> shape(i, 0) >> shape(i, 1);
    }
    fin.close();
    return shape;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
人脸对齐是在人脸识别中的一个重要预处理步骤,可以提高人脸识别的准确率和稳定性。在早期的OpenCV版本中,由于不支持人脸Landmark检测,通常通过对人脸进行分割,然后通过角点检测来寻找眼睛的两个角点,根据它们的水平线角度进行旋转,实现人脸对齐。\[1\] 而在OpenCV3.x版本开始,支持获取Landmark数据,最常见的Landmark数据是人脸的68个标准点位。通过这些关键点,可以进行仿射变换,找出变换矩阵,然后将原始图像中的所有像素点变换到标准人脸上,实现人脸对齐。反对齐则是将对齐后的人脸再次进行仿射变换,使其回到原始图像的位置。\[2\] 具体的像素变换计算公式如下: 设仿射矩阵为M,像素点为(pixel\[0\], pixel\[1\]),变换后的像素点为(x, y),则有: x = M\[0,0\]*pixel\[0\] + M\[0,1\]*pixel\[1\] + M\[0,2\] y = M\[1,0\]*pixel\[0\] + M\[1,1\]*pixel\[1\] + M\[1,2\] 通过这样的计算,可以实现对齐和反对齐操作。\[2\] 在实际操作中,可以通过Landmark数据提取眼睛的位置坐标,计算眼睛之间的角度,然后通过几何变换实现人脸对齐。具体的代码可以参考引用\[3\]中的示例。\[3\] 总结来说,人脸对齐是通过Landmark数据或角点检测来找到眼睛的位置,然后进行旋转或仿射变换,使得人脸在图像中的位置和角度更加一致,从而提高人脸识别的准确性和稳定性。 #### 引用[.reference_title] - *1* *3* [OpenCV实现人脸对齐](https://blog.csdn.net/qq_42722197/article/details/121668671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python-opencv-人脸对齐和反对齐](https://blog.csdn.net/Coding_Ann/article/details/104693126)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值