opencv 仿射变换 根据眼睛坐标进行人脸对齐 计算变换后对应坐标



使用opencv对人脸图像进行仿射变换, 实现人脸对齐功能 
 
 
 
 
人脸图像及68个面部关键点                             仿射变换后人脸图像及关键点 
 
 
仿射变换将原坐标(x, y)变换为新坐标(x', y')的计算方法: 
 
 
通过上面的公式, 可计算出原图像经过变换后的新图像。 
 
 
Opencv仿射变换函数warpAffine: 

  1. //! warps the image using affine transformation
  2. void warpAffine( InputArray src, OutputArray dst,
  3.                               InputArray M, Size dsize,
  4.                               int flags=INTER_LINEAR,
  5.                               int borderMode=BORDER_CONSTANT,
  6.                               const Scalar& borderValue=Scalar());

参数:src - 输入图像 
          dst - 输出图像,图像大小为dsize 
          M - 2X3变换矩阵 
          dsize - 输出图像的大小 
         flags - 差值方法 
         borderMode - 像素填充方法,BORDER_CONSTANT表示固定值填充 
      borderValue - 填充的值 
 
 
计算仿射变换矩阵函数getRotationMatrix2D 

  1. Mat getRotationMatrix2D( Point2f center, double angle, double scale );

根据旋转中心, 旋转角度,缩放因子计算仿射变换矩阵。 
计算方法: 
 
 
实现代码: 
 

  1. //根据眼睛坐标对图像进行仿射变换
  2. //src - 原图像
  3. //landmarks - 原图像中68个关键点
  4. Mat getwarpAffineImg(Mat &src, vector<Point2f> &landmarks)
  5. {
  6.     Mat oral;src.copyTo(oral);
  7.     for (int j = 0; j < landmarks.size(); j++)
  8.     {
  9.         circle(oral, landmarks[j], 2, Scalar(255, 0, 0));
  10.     }
  11.     //计算两眼中心点,按照此中心点进行旋转, 第31个为左眼坐标,36为右眼坐标
  12.     Point2f eyesCenter = Point2f( (landmarks[31].x + landmarks[36].x) * 0.5f, (landmarks[31].y + landmarks[36].y) * 0.5f );
  13.     
  14.     // 计算两个眼睛间的角度
  15.     double dy = (landmarks[36].y - landmarks[31].y);
  16.     double dx = (landmarks[36].x - landmarks[31].x);
  17.     double angle = atan2(dy, dx) * 180.0/CV_PI; // Convert from radians to degrees.
  18.     
  19.     //由eyesCenter, andle, scale按照公式计算仿射变换矩阵,此时1.0表示不进行缩放
  20.     Mat rot_mat = getRotationMatrix2D(eyesCenter, angle, 1.0);
  21.     Mat rot;
  22.     // 进行仿射变换,变换后大小为src的大小
  23.     warpAffine(src, rot, rot_mat, src.size());
  24.     vector<Point2f> marks;
  25.     
  26.     //按照仿射变换矩阵,计算变换后各关键点在新图中所对应的位置坐标。
  27.     for (int n = 0; n<landmarks.size(); n++)
  28.     {
  29.         Point2f p = Point2f(0, 0);
  30.         p.x = rot_mat.ptr<double>(0)[0] *landmarks[n].x + rot_mat.ptr<double>(0)[1] * landmarks[n].y + rot_mat.ptr<double>(0)[2];
  31.         p.y = rot_mat.ptr<double>(1)[0] * landmarks[n].x + rot_mat.ptr<double>(1)[1] * landmarks[n].y + rot_mat.ptr<double>(1)[2];
  32.         marks.push_back(p);
  33.     }
  34.     //标出关键点
  35.     for (int j = 0; j < landmarks.size(); j++)
  36.     {
  37.         circle(rot, marks[j], 2, Scalar(0, 0, 255));
  38.     }
  39.     return rot;
  40. }

 
 
将缩放因子scale = 0.5,仿射变换后结果为: 
 
     原图像                                                      按照眼睛坐标进行校正,缩放因子0.5 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值