예제 #1
0
void FacePredict::FaceSynthesis(AAM_Shape &shape, CvMat* texture, IplImage* newImage)
{
	double thisfacewidth = shape.GetWidth();
	shape.Scale(stdwidth / thisfacewidth);
	shape.Translate(-shape.MinX(), -shape.MinY());

	AAM_PAW paw;
	CvMat* points = cvCreateMat (1, __shape.nPoints(), CV_32FC2);
	CvMemStorage* storage = cvCreateMemStorage(0);
	paw.Train(shape, points, storage, __paw.GetTri(), false);  //the actual shape

	__AAMRefShape.Translate(-__AAMRefShape.MinX(), -__AAMRefShape.MinY());  //refShape, central point is at (0,0);translate the min to (0,0)
	double minV, maxV;
	cvMinMaxLoc(texture, &minV, &maxV);
	cvConvertScale(texture, texture, 1/(maxV-minV)*255, -minV*255/(maxV-minV));

	cvZero(newImage);

	int x1, x2, y1, y2, idx1 = 0, idx2 = 0;
	int tri_idx, v1, v2, v3;
	int minx, miny, maxx, maxy;
	minx = shape.MinX(); miny = shape.MinY();
	maxx = shape.MaxX(); maxy = shape.MaxY();
	for(int y = miny; y < maxy; y++)
	{
		y1 = y-miny;
		for(int x = minx; x < maxx; x++)
		{
			x1 = x-minx;
			idx1 = paw.Rect(y1, x1);
			if(idx1 >= 0)
			{
				tri_idx = paw.PixTri(idx1);
				v1 = paw.Tri(tri_idx, 0);
				v2 = paw.Tri(tri_idx, 1);
				v3 = paw.Tri(tri_idx, 2);
		
				x2 = paw.Alpha(idx1)*__AAMRefShape[v1].x + paw.Belta(idx1)*__AAMRefShape[v2].x +  
					paw.Gamma(idx1)*__AAMRefShape[v3].x;
				y2 = paw.Alpha(idx1)*__AAMRefShape[v1].y + paw.Belta(idx1)*__AAMRefShape[v2].y +  
					paw.Gamma(idx1)*__AAMRefShape[v3].y;
				
				idx2 = __paw.Rect(y2, x2);
				if(idx2 < 0) continue;

				CV_IMAGE_ELEM(newImage, byte, y, 3*x) = cvmGet(texture, 0, 3*idx2);
				CV_IMAGE_ELEM(newImage, byte, y, 3*x+1) = cvmGet(texture, 0, 3*idx2+1);
				CV_IMAGE_ELEM(newImage, byte, y, 3*x+2) = cvmGet(texture, 0, 3*idx2+2);
			}
		}
	}
	cvReleaseMat(&points);
	cvReleaseMemStorage(&storage);
}
예제 #2
0
//============================================================================
void ontrackcam(int pos)
{
	if(c == 0)
	{
		c = cvCreateMat(1, g_cam->nModes(), CV_64FC1);cvZero(c);
		s = cvCreateMat(1, g_cam->__shape.nPoints()*2, CV_64FC1);
		t = cvCreateMat(1, g_cam->__texture.nPixels(), CV_64FC1);
	}

	double var;
	//registrate appearance parameters
	for(int i = 0; i < n; i++)
	{
		var = 3*sqrt(g_cam->Var(i))*(double(b_c[i])/offset-1.0);
		cvmSet(c, 0, i, var);
	}

	//generate shape and texture instance
	g_cam->CalcLocalShape(s, c);
	g_cam->CalcTexture(t, c);
	
	//warp texture instance from base mesh to current shape instance
	aam_s.Mat2Point(s);
	int w = aam_s.GetWidth(), h = aam_s.MaxY()-aam_s.MinY();
	 aam_s.Translate(w, h);
	if(image == 0)image = cvCreateImage(cvSize(w*2,h*2), 8, 3);
	cvSet(image, cvScalar(128, 128, 128));
	g_cam->DrawAppearance(image, aam_s, t);
	
	cvNamedWindow("Combined Appearance Model",1);
	cvShowImage("Combined Appearance Model", image);
	
	if(cvWaitKey(10) == '27')
	{
		cvReleaseImage(&image);
		cvReleaseMat(&s);
		cvReleaseMat(&t);
		cvReleaseMat(&c);
		cvDestroyWindow("Parameters");
		cvDestroyWindow("Combined Appearance Model");
	}
}
예제 #3
0
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;
}
예제 #4
0
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;
}