1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| IplImage* Leila::cropFace(IplImage*image, CvPoint eye_left, CvPoint eye_right, double offset_pct[2], CvSize dest_sz)
{
//calculate offsets in original image
double offset_h = offset_pct[0]* dest_sz.width;
double offset_v = offset_pct[1]*dest_sz.height;
//get the direction
double eye_directionX,eye_directionY;
eye_directionX= double(eye_right.x - eye_left.x);
eye_directionY=double(eye_right.y - eye_left.y);
double rotation;
rotation = double(-atan2(eye_directionY,eye_directionX )*180/PI);
// distance between them
double dist = distancePoints(eye_left, eye_right);
//calculate the reference eye-width
double reference = dest_sz.width - 2.0*offset_h;
//scale factor
double scale = dist/reference;
//rotation par rapport à l'oeil gauche
//matrice de rotation
cv::Mat affine_matrix;
affine_matrix =cv::getRotationMatrix2D( eye_left, rotation, scale );
//mtx est la conversion de image IplImage* en matrice mtx
cv::Mat mtx=cv::Mat(image,true);
cv::Mat mtx2;
cv::warpAffine(mtx, mtx2, affine_matrix,mtx.size(),cv::INTER_LINEAR, cv::BORDER_CONSTANT,cv::Scalar::all(255));
//mtx est la conversion de matrice mtx2 en image IplImage*
IplImage image1 =mtx2;
IplImage* im22=(IplImage*)&image1;
//crop the rotated image
double crop_x =double(eye_left.x) - scale*offset_h;
double crop_y =double(eye_left.y) - scale*offset_v;
double crop_size0 = (double)dest_sz.width*scale;
double crop_size1= (double) dest_sz.height*scale;
CvRect region;
region.x=cvRound(crop_x);
region.y=cvRound(crop_y);
region.width=cvRound (crop_size0);
region.height=cvRound(crop_size1);
IplImage* im44=cropImage(im22,region);
//crop((int(crop_xy[0]), int(crop_xy[1]), int(crop_xy[0]+crop_size[0]), int(crop_xy[1]+crop_size[1])))
// # resize it
// image = image.resize(dest_sz, Image.ANTIALIAS)
IplImage* image3=resizeImage(im44, dest_sz.width, dest_sz.width);
return image3;
} |
Partager