1. 程式人生 > >opencv 仿射變換 根據眼睛座標進行人臉對齊 計算變換後對應座標

opencv 仿射變換 根據眼睛座標進行人臉對齊 計算變換後對應座標

  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.     double dy = (landmarks[36].y - landmarks[31].y);
  15.     double dx = (landmarks[36].x - landmarks[31].x);
  16.     double angle = atan2(dy, dx) * 180.0/CV_PI; // Convert from radians to degrees.
  17.     //由eyesCenter, andle, scale按照公式計算仿射變換矩陣,此時1.0表示不進行縮放
  18.     Mat rot_mat = getRotationMatrix2D(eyesCenter, angle, 1.0);
  19.     Mat rot;
  20.     // 進行仿射變換,變換後大小為src的大小
  21.     warpAffine(src, rot, rot_mat, src.size());
  22.     vector<Point2f> marks;
  23.     //按照仿射變換矩陣,計算變換後各關鍵點在新圖中所對應的位置座標。
  24.     for (int n = 0; n<landmarks.size(); n++)
  25.     {
  26.         Point2f p = Point2f(0, 0);
  27.         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];
  28.         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];
  29.         marks.push_back(p);
  30.     }
  31.     //標出關鍵點
  32.     for (int j = 0; j < landmarks.size(); j++)
  33.     {
  34.         circle(rot, marks[j], 2, Scalar(0, 0, 255));
  35.     }
  36.     return rot;
  37. }