asmlib-opencv
an ASM(Active Shape Model) implementation by C++ & OpenCV
/media/Data/chenxing/myProj/asmlib-opencv/src/shapemodel.h
Go to the documentation of this file.
00001 #ifndef SHAPEMODEL_H
00002 #define SHAPEMODEL_H
00003 
00004 #include "modelimage.h"
00005 #include "cv.h"
00006 #include "modelfile.h"
00007 #include <vector>
00008 using std::vector;
00009 using cv::PCA;
00010 
00011 namespace StatModel {
00012 
00014 struct FitResult{
00016     Mat_< double > params;
00017 
00019     SimilarityTrans transformation;
00020 };
00021 
00023 
00030 class ShapeModel
00031 {
00032 public:
00033     ShapeModel();
00034 
00036     virtual void saveToFile(ModelFile &file);
00037 
00039     virtual void loadFromFile(ModelFile &file);
00040 
00042     void readTrainData(const char *listFileName);
00043 
00045     void loadShapeInfo(const char *shapeFileName);
00046 
00048 
00052     void setPyramidLevel(int l){ pyramidLevel = l; }
00053 
00055 
00059     void projectParamToShape(const Mat_<double> & paramVec, ShapeVec &shapeVec);
00060 
00062 
00066     void projectShapeToParam(const ShapeVec & shapeVec, Mat_<double> &paramVec);
00067 
00068     ShapeInfo & getShapeInfo(){ return shapeInfo;}
00069 
00071     Mat_< double > normalizeParam(const Mat_<double> &p){
00072         Mat_<double> n = p.clone();
00073         for (int i=0; i<p.rows; i++)
00074             n(i, 0)/= 3*sqrt(pcaShape->eigenvalues.at<double>(i, 0));
00075         return n;
00076     }
00077 
00079     Mat_< double > reConFromNorm(const Mat_<double> &p){
00080         Mat_<double> n = p.clone();
00081         for (int i=0; i<p.rows; i++)
00082             n(i, 0)*= 3*sqrt(pcaShape->eigenvalues.at<double>(i, 0));
00083         return n;
00084     }
00085 
00086 
00087     // For viewing the model
00089     struct ModelViewInfo
00090     {
00091         vector< int > vList;
00092         int curParam;
00093         void *pModel;
00094     };
00095 
00097     void viewShapeModel();
00098 
00100     void viewShapeModelUpdate(ModelViewInfo *pInfo);
00101 protected:
00103     int pyramidLevel;
00104 
00106     int nMarkPoints;
00107 
00109     int nTrain;
00110 
00112     vector<ModelImage> imageSet;
00113 
00115     ShapeInfo shapeInfo;
00116 
00118     ShapeVec meanShape;
00119 
00121     void clampParamVec( Mat_< double > &paramVec );
00122 
00124     PCA *pcaShape;
00126     int nShapeParams;
00127 
00129     double sigma2;
00130 
00132     PCA *pcaFullShape;
00133 
00135 
00139     void buildModel(const string& shapeDefFile, const string& ptsListFile);
00140 private:
00142     void buildPCA();
00143 
00145     void alignShapes();
00146 
00148     void preparePatterns();
00149 
00150 protected:
00152     double searchYOffset;
00153 
00155     double searchXOffset;
00156 
00158     double searchWScale;
00160     double searchHScale;
00161 
00163     double searchStepAreaRatio;
00164 
00166     double searchScaleRatio;
00168     double searchInitXOffset;
00170     double searchInitYOffset;
00171 };
00172 } // Namespace
00173 #endif // SHAPEMODEL_H
 All Classes Namespaces Files Functions Variables Typedefs Defines