Skip to content

Commit

Permalink
fixes in UI, adding possible equations for least squares
Browse files Browse the repository at this point in the history
  • Loading branch information
pkmital committed May 29, 2011
1 parent 6497782 commit cd4c0d9
Show file tree
Hide file tree
Showing 17 changed files with 7,526 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/aam-library/AAM_Basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void AAM_Basic::Fit(const IplImage* image, AAM_Shape& Shape,
__cam.CalcShape(__s, __current_c_q);
Shape.Mat2Point(__s);
t = AAM_GetTime() - t;
printf("AAM-Basic Fitting time cost: %.3f millisec\n", t);
//printf("AAM-Basic Fitting time cost: %.3f millisec\n", t);
}


Expand Down
24 changes: 24 additions & 0 deletions src/aam-library/AAM_Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ class AAM_EXPORTS AAM_Basic:public AAM

// Init search parameters
virtual void InitParams(const IplImage* image);

virtual void PrintAppearanceVector()
{
printf("appearance vector: \n");
print_matrix(__c);
}

virtual CvMat* GetAppearanceVector()
{
return __c;
}

void print_matrix(CvMat *mat)
{
int i,j;
for (j = 0; j < mat->rows; j++) {
for (i = 0; i < mat->cols; i++) {
double thisVal = cvmGet(mat, j, i);
printf("%02.4lf ", thisVal);
}
printf("\n");
}
printf("\n\n");
}

private:
// Calculates the pixel difference from a model instance and an image
Expand Down
2 changes: 2 additions & 0 deletions src/aam-library/AAM_CAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ void ontrackcam(int pos)
cvSet(image, cvScalar(128, 128, 128));
g_cam->DrawAppearance(image, aam_s, t);


cvNamedWindow("Combined Appearance Model",1);
cvShowImage("Combined Appearance Model", image);

Expand All @@ -431,6 +432,7 @@ void ontrackcam(int pos)
cvDestroyWindow("Parameters");
cvDestroyWindow("Combined Appearance Model");
}

}

//============================================================================
Expand Down
8 changes: 5 additions & 3 deletions src/aam-library/AAM_IC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,13 @@ void AAM_IC::Fit(const IplImage* image, AAM_Shape& Shape,
}

Shape.Mat2Point(__current_s);

// update appearance vector
cvGEMM(__error_t, __texture.GetBases(), 1, NULL, 1, __lamda, CV_GEMM_B_T);
__texture.CalcTexture(__lamda, __warp_t);

t = AAM_GetTime() - t;
printf("AAM IC Fitting time cost %.3f millisec\n", t);
//printf("AAM IC Fitting time cost %.3f millisec\n", t);

cvReleaseImage(&Drawimg);
}
Expand Down Expand Up @@ -548,8 +552,6 @@ void AAM_IC::Draw(IplImage* image, const AAM_Shape& Shape, int type)
else if(type == 1) AAM_Common::DrawTriangles(image, Shape, __paw.__tri);
else if(type == 2)
{
cvGEMM(__error_t, __texture.GetBases(), 1, NULL, 1, __lamda, CV_GEMM_B_T);
__texture.CalcTexture(__lamda, __warp_t);
AAM_PAW paw;
double minV, maxV;
cvMinMaxLoc(__warp_t, &minV, &maxV);
Expand Down
24 changes: 24 additions & 0 deletions src/aam-library/AAM_IC.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ class AAM_EXPORTS AAM_IC : public AAM
// Compute the Hessian matrix using modified steepest descent image.
void CalcHessian(CvMat* H, const CvMat* SD);

virtual void PrintAppearanceVector()
{
printf("appearance vector: \n");
print_matrix(__lamda);
}

virtual CvMat* GetAppearanceVector()
{
return __warp_t;//__lamda;
}

void print_matrix(CvMat *mat)
{
int i,j;
for (j = 0; j < mat->rows; j++) {
for (i = 0; i < mat->cols; i++) {
double thisVal = cvmGet(mat, j, i);
printf("%02.4lf ", thisVal);
}
printf("\n");
}
printf("\n\n");
}

private:

//these variables are used for train PAW
Expand Down
8 changes: 7 additions & 1 deletion src/aam-library/AAM_Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ void AAM_Pyramid::Fit(const IplImage* image,
// for each level in the image pyramid
for (int iLev = startlev; iLev >= 0; iLev--)
{
printf("Level %d: ", iLev);
//printf("Level %d: ", iLev);

IplImage* fitimage = cvCreateImage(
cvSize((int)(w0/PyrScale), (int)(h0/PyrScale)),
Expand Down Expand Up @@ -496,6 +496,12 @@ void AAM_Pyramid::WriteModel(const std::string& filename)
((AAM_Basic*)__model[0])->__cam.ShowVariation();
}

void AAM_Pyramid::ShowModes()
{
if(__model[0]->GetType() == TYPE_AAM_BASIC)
((AAM_Basic*)__model[0])->__cam.ShowVariation();
}

//============================================================================
void AAM_Pyramid::ReadModel(const std::string& filename)
{
Expand Down
21 changes: 21 additions & 0 deletions src/aam-library/AAM_Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class AAM_EXPORTS AAM
// Get Mean Shape of model
virtual const AAM_Shape GetMeanShape()const = 0;
virtual const AAM_Shape GetReferenceShape()const = 0;

virtual void PrintAppearanceVector() = 0;
virtual CvMat* GetAppearanceVector() = 0;
};

class AAM_EXPORTS AAM_Pyramid
Expand Down Expand Up @@ -97,13 +100,31 @@ class AAM_EXPORTS AAM_Pyramid

// Read aam from file
void ReadModel(const std::string& filename);

void ShowModes();

// Draw the image according search result
void Draw(IplImage* image, const AAM_Shape& Shape, int type);

// Get Mean Shape of model
const AAM_Shape GetMeanShape()const;

void PrintAppearanceVector()
{
if(__model.size() > 0)
{
__model[0]->PrintAppearanceVector();
}
}

CvMat* GetAppearanceVector()
{
if(__model.size() > 0)
{
return __model[0]->GetAppearanceVector();
}
}

private:
std::vector<AAM*> __model;
AAM_Shape __VJDetectShape;
Expand Down
97 changes: 91 additions & 6 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ void app::update()

if(bPoseModelBuilt) {
faceModeler->update(frame);
Mat av = faceModeler->getAppearanceVector();
Mat pose_x, pose_y;
poseCalibrator->getPose(av, pose_x, pose_y);
}
else if(bAppearanceModelBuilt) {
faceModeler->update(frame);
Expand All @@ -80,9 +77,66 @@ void app::draw()
{
if (bPoseModelBuilt) {
faceModeler->draw(frame);
Mat av = faceModeler->getAppearanceVector();
Mat pose_x, pose_y;
poseCalibrator->getPose(av, pose_x, pose_y);
int w2 = width/2;
int h2 = height/2;
double x = pose_x.at<double>(0) * w2 + w2;
double y = pose_y.at<double>(0) * h2 + h2;
poseFilter.addExample(x,y);
x = poseFilter.getX();
y = poseFilter.getY();
circle(frame, Point2d(x,y), 10, Scalar(255,0,255), 5, CV_FILLED);

char buf[256];
sprintf(buf, "[\'t\']: Start training AAM with calibration points\n");
putText(frame,
buf,
Point(10,60),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
sprintf(buf, "[\'0\' - \'9\']: Add training example for calibration point\n");
putText(frame,
buf,
Point(10,80),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
sprintf(buf, "[\'d\']: Change drawing mode");
putText(frame,
buf,
Point(10,100),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
}
else if(bAppearanceModelBuilt) {
faceModeler->draw(frame);

char buf[256];
sprintf(buf, "[\'t\']: Start training AAM with calibration points\n");
putText(frame,
buf,
Point(10,60),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
sprintf(buf, "[\'0\' - \'9\']: Add training example for calibration point\n");
putText(frame,
buf,
Point(10,80),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
sprintf(buf, "[\'d\']: Change drawing mode");
putText(frame,
buf,
Point(10,100),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
}
else {
faceTracker->drawShapeModel(frame);
Expand All @@ -92,8 +146,31 @@ void app::draw()
putText(frame,
buf,
Point(10,20),
CV_FONT_HERSHEY_SIMPLEX,
0.5,
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));


sprintf(buf, "[\'l\']: Load an existing AAM Model");
putText(frame,
buf,
Point(10,60),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
sprintf(buf, "[\'r\']: Re-initialize the face tracker");
putText(frame,
buf,
Point(10,80),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));
sprintf(buf, "[\' \']: Train an AAM model");
putText(frame,
buf,
Point(10,100),
CV_FONT_HERSHEY_PLAIN,
1.0,
CV_RGB(255,255,255));

}
Expand All @@ -104,7 +181,7 @@ void app::draw()
void app::keyPressed(int c)
{
// reset the shape model
if(c == 'd')
if(c == 'r')
faceTracker->reset();
// start training the AAM
else if(c == ' ')
Expand All @@ -130,4 +207,12 @@ void app::keyPressed(int c)
{
bPoseModelBuilt = poseCalibrator->modelPose();
}
else if(c == 's')
{
faceModeler->showModes();
}
else if(c == 'd')
{
faceModeler->changeDrawingMode();
}
}
3 changes: 2 additions & 1 deletion src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "pkmFaceTracker.h"
#include "pkmFaceModeler.h"
#include "pkmPoseCalibrator.h"

#include "pkmPoseFilter.h"
#include <opencv2/opencv.hpp>

class app
Expand All @@ -28,6 +28,7 @@ class app
pkmFaceTracker *faceTracker;
pkmFaceModeler *faceModeler;
pkmPoseCalibrator *poseCalibrator;
pkmPoseFilter poseFilter;

CvCapture *camera;
IplImage *cameraImage;
Expand Down
6 changes: 4 additions & 2 deletions src/pkmFaceModeler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pkmFaceModeler::pkmFaceModeler(int n)
currentExample = 0; // current example to train
numExamples = n; // number of examples before model is built

drawMode = 1;

bModelLoaded = false;
bDraw = false;
}
Expand Down Expand Up @@ -64,7 +66,7 @@ bool pkmFaceModeler::addExample(Mat &img, Mat &shape)
// we've learned enough examples, now train an AAM, storing to "model.aam"
bool pkmFaceModeler::buildModel()
{
int type = TYPE_AAM_IC;
int type = TYPE_AAM_IC;//TYPE_AAM_BASIC;//
int level = 1;
int color = 3;
aamModel.Build(pointFiles, imageFiles, type, level, color);
Expand Down Expand Up @@ -121,7 +123,7 @@ void pkmFaceModeler::draw(Mat &frame)
{
// draw the appearance model in the image
IplImage image = frame;
aamModel.Draw(&image, aamShape, 2);
aamModel.Draw(&image, aamShape, drawMode);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/pkmFaceModeler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class pkmFaceModeler
// return the matrix of appearance values
Mat getAppearanceVector();

void showModes()
{
aamModel.ShowModes();
}

void changeDrawingMode()
{
drawMode = (drawMode + 1) % 3;
}


private:
Mat currentAppearanceVector;
Expand All @@ -56,6 +66,8 @@ class pkmFaceModeler

int numExamples,
currentExample;

int drawMode;

AAM_Pyramid aamModel;
AAM_Shape aamShape;
Expand Down
10 changes: 5 additions & 5 deletions src/pkmFaceTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class pkmFaceTracker
wSize1.resize(1);
wSize2.resize(3);
wSize1[0] = 5; // 7
wSize2[0] = 7; // 11
wSize2[1] = 5; // 9
wSize2[2] = 3; // 7
wSize2[0] = 11; // 11
wSize2[1] = 9; // 9
wSize2[2] = 7; // 7

nIter = 50; // 5
clamp = 3; // 3
nIter = 3; // 5
clamp = 5; // 3
fTol = 0.99; // 0.01

fpd = -1; // -1
Expand Down
Loading

0 comments on commit cd4c0d9

Please sign in to comment.