forked from guanghaoyin/face_asm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapemodel.h
184 lines (146 loc) · 4.56 KB
/
shapemodel.h
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once
#pragma execution_character_set("utf-8")
#ifndef SHAPEMODEL_H
#define SHAPEMODEL_H
#include "modelimage.h"
#include "base function.h"
#include <vector>
namespace modelshare {
struct FitResult {
//! Parameters for the model
Mat_< double > params;
//! The similarity transformation needed to recover shape
simtrans transformation;
};
class ShapeModel {
protected:
//! PCA model for shapes.
PCA *pcaShape;
//! Number of eigen vectors reserved for shape model.
int nShapeParams;
//贝叶斯切线模型
//! Data for BTSM: \f$\sigma^2\f$残差平方和系数
double sigma2;
//! Data for BTSM: Full \f$\phi\f$
PCA *pcaFullShape;
//! level for the image pyramid.
int pyramidLevel;
//! Number of landmark points in a image.
int nMarkPoints;
//! Number of training images.
int nTrain;
//! All the images, with labelled markpoints.
vector<ModelImage> imageSet;
//! Path info for shapes;
ShapeInfo shapeInfo;
//! Mean shape after aligning
ShapeVec meanShape;
//! r.y -= r.height*?
double searchYOffset;
//! r.x -= r.width*?
double searchXOffset;
//! r.width *= ?
double searchWScale;
//! r.height *= ?
double searchHScale;
//! step: ?*100/sqrt(area)
double searchStepAreaRatio;
//! init scale ratio when searching
double searchScaleRatio;
//! init X offset when searching
double searchInitXOffset;
//! init Y offset when searching
double searchInitYOffset;
//! Refine a parameter vector by clamping.
void clampParamVec(Mat_< double > ¶mVec) {
// Todo: Change "3" to a configurable variable.
for (int i = 0;i<this->nShapeParams;i++) {
double ei = sqrt(pcaShape->eigenvalues.at<double>(i, 0));
if (paramVec(i, 0) > 3 * ei)
paramVec(i, 0) = 3 * ei;
else if (paramVec(i, 0) < -3 * ei)
paramVec(i, 0) = -3 * ei;
}
}
//! Align the shapes and build a model.
/**
* \param shapeDefFile Shape definition file.
* \param ptsListFile File containting a list of pts files.
*/
void buildModel(const string& shapeDefFile, const string& ptsListFile) {
loadShapeInfo(shapeDefFile.c_str());
readTrainDataFromList(ptsListFile.c_str());
this->alignShapes();
this->buildPCA();
}
public:
// For viewing the model
//! Used for viewing model
struct ModelViewInfo
{
vector< int > vList;
int curParam;
void *pModel;
};
ShapeModel();
//! Save the model into a file
virtual void saveToFile(ModelFile &file);
//! Load the model from a file
virtual void loadFromFile(ModelFile &file);
//! File names are stored in the list file
void readTrainDataFromList(const char *listFileName);
//! Load shape information
void loadShapeInfo(const char *shapeFileName);
//! Set the level for the image pyramid
/*!
\param l Image from level 0 to l will be considered during training
and searching.
*/
void setPyramidLevel(int l) { pyramidLevel = l; }
//! Project a parameter vector to a shape
/*!
\param paramVec parameter vector.
\param shapeVec the shape corresponding to the parameter vector
*/
void projectParamToShape(const Mat_<double> & paramVec, ShapeVec &shapeVec) {
this->pcaShape->backProject(paramVec, shapeVec);
}
//! Project a shape to a parameter vector
/*!
\param shapeVec the shape corresponding to the parameter vector
\param paramVec parameter vector.
*/
void projectShapeToParam(const ShapeVec & shapeVec,
Mat_<double> & paramVec)
{
this->pcaShape->project(shapeVec, paramVec);
}
//! Normalize an parameter vector(0..1)
Mat_< double > normalizeParam(const Mat_<double> &p) {
Mat_<double> n = p.clone();
for (int i = 0; i<p.rows; i++)
n(i, 0) /= 3 * sqrt(pcaShape->eigenvalues.at<double>(i, 0));
return n;
}//
//! Reconstruct parameter vector from normalized vector
Mat_< double > reConFromNorm(const Mat_<double> &p) {
Mat_<double> n = p.clone();
for (int i = 0; i<p.rows; i++)
n(i, 0) *= 3 * sqrt(pcaShape->eigenvalues.at<double>(i, 0));
return n;
}
ShapeInfo & getShapeInfo() { return shapeInfo; }
//! An interactive UI for viewing the statistical model.
void viewShapeModel();
//! Update viewing UI with new parameters. (called by callbacks)
void viewShapeModelUpdate(ModelViewInfo *pInfo);
private:
//! Build PCA model for shapes
void buildPCA();
//! Align shapes iteratively
void alignShapes();
//! Find patterns~
void preparePatterns();
};
}
#endif // !SHAPEMODEL_H