IplImage* FacePredict::predict(const AAM_Shape& Shape, const IplImage& curImage, const AAM_Shape& ShapeF, const IplImage& ImageF, double RatioF, const AAM_Shape& ShapeM, const IplImage& ImageM, double RatioM, int curAgeG, int newAgeG, bool save) { if (newAgeG > NGROUPS || curAgeG > NGROUPS) { fprintf(stderr, "ERROE(%s, %d): Age group larger than %d\n", __FILE__, __LINE__, NGROUPS); exit(0); } if(curImage.nChannels != 3 || curImage.depth != 8) { fprintf(stderr, "ERROR(%s: %d): The image channels must be 3, " "and the depth must be 8!\n", __FILE__, __LINE__); exit(0); } /*get the current shape parameters*/ AAM_Shape curShape = Shape; curShape.Centralize(); double thisfacewidth = curShape.GetWidth(); if(stdwidth < thisfacewidth) curShape.Scale(stdwidth / thisfacewidth); curShape.AlignTo(__AAMRefShape); CvMat* p = cvCreateMat(1, __nShapeModes, CV_64FC1); CvMat* pq = cvCreateMat(1, 4+__nShapeModes, CV_64FC1); __shape.CalcParams(curShape, pq); cvGetCols(pq, p, 4, 4+__nShapeModes); /*get the current texture parameters*/ CvMat* curTexture = cvCreateMat(1, __paw.nPix() * 3, CV_64FC1); __paw.FasterGetWarpTextureFromShape(Shape, &curImage, curTexture, false); __texture.AlignTextureToRef(__MeanT, curTexture); CvMat* lamda = cvCreateMat(1, __nTextureModes, CV_64FC1); __texture.CalcParams(curTexture, lamda); //father CvMat* pF = cvCreateMat(1, __nShapeModes, CV_64FC1); CvMat* lamdaF = cvCreateMat(1, __nTextureModes, CV_64FC1); if (RatioF == 0) { cvZero(pF); cvZero(lamdaF); } else { AAM_Shape shapeF = ShapeF; shapeF.Centralize(); thisfacewidth = ShapeF.GetWidth(); if(stdwidth < thisfacewidth) shapeF.Scale(stdwidth / thisfacewidth); shapeF.AlignTo(__AAMRefShape); CvMat* pqF = cvCreateMat(1, 4+__nShapeModes, CV_64FC1); __shape.CalcParams(shapeF, pqF); cvGetCols(pqF, pF, 4, 4+__nShapeModes); CvMat* TextureF = cvCreateMat(1, __paw.nPix() * 3, CV_64FC1); __paw.FasterGetWarpTextureFromShape(ShapeF, &ImageF, TextureF, false); __texture.AlignTextureToRef(__MeanT, TextureF); __texture.CalcParams(TextureF, lamdaF); } //mother CvMat* pM = cvCreateMat(1, __nShapeModes, CV_64FC1); CvMat* lamdaM = cvCreateMat(1, __nTextureModes, CV_64FC1); if (RatioM == 0) { cvZero(pM); cvZero(lamdaM); } else { AAM_Shape shapeM = ShapeM; shapeM.Centralize(); thisfacewidth = ShapeM.GetWidth(); if(stdwidth < thisfacewidth) shapeM.Scale(stdwidth / thisfacewidth); shapeM.AlignTo(__AAMRefShape); CvMat* pqM = cvCreateMat(1, 4+__nShapeModes, CV_64FC1); __shape.CalcParams(shapeM, pqM); cvGetCols(pqM, pM, 4, 4+__nShapeModes); CvMat* TextureM = cvCreateMat(1, __paw.nPix() * 3, CV_64FC1); __paw.FasterGetWarpTextureFromShape(ShapeM, &ImageM, TextureM, false); __texture.AlignTextureToRef(__MeanT, TextureM); __texture.CalcParams(TextureM, lamdaM); } /*caculate new shape and texture parameters*/ CvMat newShapeParams; CvMat* newpq = cvCreateMat(1, 4+__nShapeModes, CV_64FC1); cvmSet(newpq, 0, 0, cvmGet(pq, 0, 0)); cvmSet(newpq, 0, 1, cvmGet(pq, 0, 1)); cvmSet(newpq, 0, 2, cvmGet(pq, 0, 2)); cvmSet(newpq, 0, 3, cvmGet(pq, 0, 3)); cvGetCols(newpq, &newShapeParams, 4, 4+__nShapeModes); CvMat* newSP = cvCreateMat(1, __nShapeModes, CV_64FC1); FacePredict::CalcNewShapeParams(p, newSP, curAgeG, newAgeG); FacePredict::CalcParamsByRatio(newSP, pF, RatioF, pM, RatioM, &newShapeParams); CvMat* newTP = cvCreateMat(1, __nTextureModes, CV_64FC1); FacePredict::CalcNewTextureParams(lamda, newTP, curAgeG, newAgeG); CvMat* newTextureParams = cvCreateMat(1, __nTextureModes, CV_64FC1); FacePredict::CalcParamsByRatio(newTP, lamdaF, RatioF, lamdaM, RatioM, newTextureParams); /*calculate the new shape and texture*/ AAM_Shape newShape; __shape.CalcShape(newpq, newShape); CvMat* newTexture = cvCreateMat(1, __paw.nPix() * 3, CV_64FC1); __texture.CalcTexture(newTextureParams, newTexture); /*systhetize the shape and texture*/ IplImage* newImage = cvCreateImage(cvSize(stdwidth, stdwidth / newShape.GetWidth() * newShape.GetHeight()), IPL_DEPTH_8U, 3); FacePredict::FaceSynthesis(newShape, newTexture, newImage); if(save) cvSaveImage("facial prediction.jpg", newImage); cvReleaseMat(&p); cvReleaseMat(&pq); cvReleaseMat(&curTexture); cvReleaseMat(&lamda); cvReleaseMat(&newTextureParams); cvReleaseMat(&newpq); cvReleaseMat(&newTexture); return newImage; }
IplImage* FacePredict::predict(const AAM_Shape& shape, const IplImage& curImage, int curAgeG, int newAgeG, bool save) { if (newAgeG > NGROUPS || curAgeG > NGROUPS) { fprintf(stderr, "ERROE(%s, %d): Age group larger than %d\n", __FILE__, __LINE__, NGROUPS); exit(0); } if(curImage.nChannels != 3 || curImage.depth != 8) { fprintf(stderr, "ERROR(%s: %d): The image channels must be 3, " "and the depth must be 8!\n", __FILE__, __LINE__); exit(0); } //get the current shape parameters AAM_Shape curShape = shape; curShape.Centralize(); double thisfacewidth = curShape.GetWidth(); if(stdwidth < thisfacewidth) curShape.Scale(stdwidth / thisfacewidth); curShape.AlignTo(__AAMRefShape); CvMat* p = cvCreateMat(1, __nShapeModes, CV_64FC1); CvMat* pq = cvCreateMat(1, 4+__nShapeModes, CV_64FC1); __shape.CalcParams(curShape, pq); cvGetCols(pq, p, 4, 4+__nShapeModes); //get the current texture parameters CvMat* curTexture = cvCreateMat(1, __paw.nPix() * 3, CV_64FC1); __paw.FasterGetWarpTextureFromShape(shape, &curImage, curTexture, false); /*IplImage *meanImg = cvCreateImage(cvGetSize(&curImage), curImage.depth, curImage.nChannels); __paw.GetWarpImageFromVector(meanImg,curTexture); cvShowImage("org Texture", meanImg);*/ __texture.AlignTextureToRef(__MeanT, curTexture); CvMat* lamda = cvCreateMat(1, __nTextureModes, CV_64FC1); __texture.CalcParams(curTexture, lamda); //caculate new shape and texture parameters CvMat newShapeParams; CvMat* newpq = cvCreateMat(1, 4+__nShapeModes, CV_64FC1); cvmSet(newpq, 0, 0, cvmGet(pq, 0, 0)); cvmSet(newpq, 0, 1, cvmGet(pq, 0, 1)); cvmSet(newpq, 0, 2, cvmGet(pq, 0, 2)); cvmSet(newpq, 0, 3, cvmGet(pq, 0, 3)); cvGetCols(newpq, &newShapeParams, 4, 4+__nShapeModes); FacePredict::CalcNewShapeParams(p, &newShapeParams, curAgeG, newAgeG); CvMat* newTextureParams = cvCreateMat(1, __nTextureModes, CV_64FC1); FacePredict::CalcNewTextureParams(lamda, newTextureParams, curAgeG, newAgeG) ; //calculate the new shape and texture AAM_Shape newShape; __shape.CalcShape(newpq, newShape); /*CvSize newsize; AAM_Shape temNS = newShape; temNS.Translate(-temNS.MinX(), -temNS.MinY()); newsize.width = 128; newsize.height = 128; IplImage *shapeNImg = cvCreateImage(newsize, curImage.depth, curImage.nChannels); cvSet(shapeNImg, CV_RGB(0,0,0)); temNS.Sketch(shapeNImg); cvShowImage("new shape", shapeNImg); cvReleaseImage(&shapeNImg);*/ CvMat* newTexture = cvCreateMat(1, __paw.nPix() * 3, CV_64FC1); __texture.CalcTexture(newTextureParams, newTexture); /*IplImage *meanNImg = cvCreateImage(cvGetSize(&curImage), curImage.depth, curImage.nChannels); __paw.GetWarpImageFromVector(meanNImg,newTexture); cvShowImage("Texture", meanNImg);*/ //systhetize the shape and texture IplImage* newImage = cvCreateImage(cvSize(stdwidth, stdwidth / newShape.GetWidth() * newShape.GetHeight()), IPL_DEPTH_8U, 3); FacePredict::FaceSynthesis(newShape, newTexture, newImage); if(save) cvSaveImage("facial prediction.jpg", newImage); cvReleaseMat(&p); cvReleaseMat(&pq); cvReleaseMat(&curTexture); cvReleaseMat(&lamda); cvReleaseMat(&newTextureParams); cvReleaseMat(&newpq); cvReleaseMat(&newTexture); return newImage; }