virtual int extract(const cv::Mat &img, std::vector<cv::Point> &pt) const { // originally: 51 landmarks. static int lut[20] = { 0,2,4, 5,7,9, // eyebrows 19,22, 25,28, // eyecorners 11,13,14, // nose 16,18,31,37,42,38,49 // mouth }; std::vector<cv::Point2d> landmarks = face_x.Alignment(img, cv::Rect(0,0,img.cols,img.rows)); pt.clear(); for (size_t i=0; i<20; ++i) { pt.push_back(cv::Point(landmarks[lut[i]])); } return int(pt.size()); }
/*---- circleCutFace函数 ------- 在原图上标记出关键点,得到人脸图中关键点坐标,切割人脸图片 ------------------------------------------------------*/ void circleCutFace(Mat img_grayzone, Mat img, Rect face, const FaceX & face_x, int mode) { int count = 0; int squreSize; Point2d leftEye, rightEye, Nose, leftMouth, rightMouth; // 标记点 vector<Point2d> landmarks = face_x.Alignment(img_grayzone, face); for (Point2d landmark : landmarks) { //circle(img, landmark, 1, Scalar(0, 255, 0), 2); switch (count++){ case 0: leftEye.x = (int) landmark.x; leftEye.y = (int) landmark.y; //cout << "leftEye: (" << leftEye.x << ", " << leftEye.y << ")" << endl; break; case 1: rightEye.x = (int) landmark.x; rightEye.y = (int) landmark.y; //cout << "rightEye: (" << rightEye.x << ", " << rightEye.y << ")" << endl; break; default: break; } } if (mode == 1 || mode == 2){ squreSize = 200; } else if (mode == 3){ squreSize = 100; } Mat img_size = cropFaceBasedOnEye(img, leftEye, rightEye, 0.3, squreSize, squreSize); imwrite(FacePath + TestImage, img_size); Mat img_sizeGray; Rect face1; face1.x = 0; face1.y = 0; face1.width = squreSize; face1.height = squreSize; cvtColor(img_size, img_sizeGray, COLOR_BGR2GRAY); vector<Point2d> landmarks1 = face_x.Alignment(img_sizeGray, face1); int count1 = 0; for (Point2d landmark : landmarks1) { //circle(img, landmark, 1, Scalar(0, 255, 0), 2); // 将原图中标记点的坐标改成200*200下的点坐标 switch (count1++){ case 0: leftEye.x = (int) landmark.x; leftEye.y = (int) landmark.y; cout << "leftEye: (" << leftEye.x << ", " << leftEye.y << ")" << endl; break; case 1: rightEye.x = (int) landmark.x; rightEye.y = (int) landmark.y; cout << "rightEye: (" << rightEye.x << ", " << rightEye.y << ")" << endl; break; case 2: Nose.x = (int) landmark.x; Nose.y = (int) landmark.y; //cout << "Nose: (" << Nose.x << ", " << Nose.y << ")" << endl; break; case 3: leftMouth.x = (int) landmark.x; leftMouth.y = (int) landmark.y; //cout << "leftMouth: (" << leftMouth.x << ", " << leftMouth.y << ")" << endl; break; case 4: rightMouth.x = (int) landmark.x; rightMouth.y = (int) landmark.y; //cout << "rightMouth: (" << rightMouth.x << ", " << rightMouth.y << ")" << endl; break; default: break; } } if (mode == 1){ Mat img_lefteye = img_size(Rect((leftEye.x - 30), (leftEye.y - 16), 60, 40)); imwrite(leftEyePath + TestImage, img_lefteye); Mat img_righteye = img_size(Rect((rightEye.x - 30), (rightEye.y - 16), 60, 40)); imwrite(rightEyePath + TestImage, img_righteye); Mat img_eyes = img_size(Rect((leftEye.x - 30), (leftEye.y - 16), 140, 40)); imwrite(EyesPath + TestImage, img_eyes); Mat img_leftbrow = img_size(Rect((leftEye.x - 30), (leftEye.y - 40), 50, 30)); imwrite(leftBrowPath + TestImage, img_leftbrow); Mat img_rightbrow = img_size(Rect((rightEye.x - 30), (rightEye.y - 40), 50, 30)); imwrite(rightBrowPath + TestImage, img_rightbrow); Mat img_brows = img_size(Rect((leftEye.x - 30), (leftEye.y - 40), 140, 30)); imwrite(BrowsPath + TestImage, img_brows); Mat img_nose = img_size(Rect((leftEye.x + 15), (leftEye.y + 2), 50, 70)); imwrite(NosePath + TestImage, img_nose); Mat img_mouth = img_size(Rect((leftMouth.x - 10), (leftMouth.y - 20), 80, 40)); imwrite(MouthPath + TestImage, img_mouth); } else if (mode == 2){ Mat img_lefteye = img_size(Rect((leftEye.x - 32), (leftEye.y - 20), 64, 44)); imwrite(leftEyePath + TestImage, img_lefteye); Mat img_righteye = img_size(Rect((rightEye.x - 32), (rightEye.y - 20), 64, 44)); imwrite(rightEyePath + TestImage, img_righteye); Mat img_eyes = img_size(Rect((leftEye.x - 32), (leftEye.y - 20), 144, 44)); imwrite(EyesPath + TestImage, img_eyes); Mat img_leftbrow = img_size(Rect((leftEye.x - 32), (leftEye.y - 44), 54, 34)); imwrite(leftBrowPath + TestImage, img_leftbrow); Mat img_rightbrow = img_size(Rect((rightEye.x - 32), (rightEye.y - 44), 54, 34)); imwrite(rightBrowPath + TestImage, img_rightbrow); Mat img_brows = img_size(Rect((leftEye.x - 32), (leftEye.y - 44), 144, 34)); imwrite(BrowsPath + TestImage, img_brows); Mat img_nose = img_size(Rect((leftEye.x + 13), (leftEye.y), 54, 74)); imwrite(NosePath + TestImage, img_nose); Mat img_mouth = img_size(Rect((leftMouth.x - 12), (leftMouth.y - 20), 84, 44)); imwrite(MouthPath + TestImage, img_mouth); } else if (mode == 3){ Mat img_lefteye = img_size(Rect((leftEye.x - 16), (leftEye.y - 8), 34, 24)); imwrite(leftEyePath + TestImage, img_lefteye); Mat img_righteye = img_size(Rect((rightEye.x - 16), (rightEye.y - 8), 34, 24)); imwrite(rightEyePath + TestImage, img_righteye); Mat img_eyes = img_size(Rect((leftEye.x - 16), (leftEye.y - 8), 74, 24)); imwrite(EyesPath + TestImage, img_eyes); Mat img_leftbrow = img_size(Rect((leftEye.x - 16), (leftEye.y - 23), 29, 19)); imwrite(leftBrowPath + TestImage, img_leftbrow); Mat img_rightbrow = img_size(Rect((rightEye.x - 16), (rightEye.y - 23), 29, 19)); imwrite(rightBrowPath + TestImage, img_rightbrow); Mat img_brows = img_size(Rect((leftEye.x - 16), (leftEye.y - 23), 74, 19)); imwrite(BrowsPath + TestImage, img_brows); Mat img_nose = img_size(Rect((leftEye.x + 7), (leftEye.y), 29, 39)); imwrite(NosePath + TestImage, img_nose); Mat img_mouth = img_size(Rect((leftMouth.x - 6), (leftMouth.y - 9), 44, 24)); imwrite(MouthPath + TestImage, img_mouth); } }