3eb50b5260b78687641513ca97e707a19caa0079
[profile/ivi/opencv.git] / apps / traincascade / cascadeclassifier.h
1 #ifndef _OPENCV_CASCADECLASSIFIER_H_
2 #define _OPENCV_CASCADECLASSIFIER_H_
3
4 #include <ctime>
5 #include "traincascade_features.h"
6 #include "haarfeatures.h"
7 #include "lbpfeatures.h"
8 #include "HOGfeatures.h" //new
9 #include "boost.h"
10 #include "cv.h"
11 #include "cxcore.h"
12
13 #define CC_CASCADE_FILENAME "cascade.xml"
14 #define CC_PARAMS_FILENAME "params.xml"
15
16 #define CC_CASCADE_PARAMS "cascadeParams"
17 #define CC_STAGE_TYPE "stageType"
18 #define CC_FEATURE_TYPE "featureType"
19 #define CC_HEIGHT "height"
20 #define CC_WIDTH  "width"
21
22 #define CC_STAGE_NUM    "stageNum"
23 #define CC_STAGES       "stages"
24 #define CC_STAGE_PARAMS "stageParams"
25
26 #define CC_BOOST            "BOOST"
27 #define CC_BOOST_TYPE       "boostType"
28 #define CC_DISCRETE_BOOST   "DAB"
29 #define CC_REAL_BOOST       "RAB"
30 #define CC_LOGIT_BOOST      "LB"
31 #define CC_GENTLE_BOOST     "GAB"
32 #define CC_MINHITRATE       "minHitRate"
33 #define CC_MAXFALSEALARM    "maxFalseAlarm"
34 #define CC_TRIM_RATE        "weightTrimRate"
35 #define CC_MAX_DEPTH        "maxDepth"
36 #define CC_WEAK_COUNT       "maxWeakCount"
37 #define CC_STAGE_THRESHOLD  "stageThreshold"
38 #define CC_WEAK_CLASSIFIERS "weakClassifiers"
39 #define CC_INTERNAL_NODES   "internalNodes"
40 #define CC_LEAF_VALUES      "leafValues"
41
42 #define CC_FEATURES       FEATURES
43 #define CC_FEATURE_PARAMS "featureParams"
44 #define CC_MAX_CAT_COUNT  "maxCatCount"
45 #define CC_FEATURE_SIZE   "featSize"
46
47 #define CC_HAAR        "HAAR"
48 #define CC_MODE        "mode"
49 #define CC_MODE_BASIC  "BASIC"
50 #define CC_MODE_CORE   "CORE"
51 #define CC_MODE_ALL    "ALL"
52 #define CC_RECTS       "rects"
53 #define CC_TILTED      "tilted"
54
55 #define CC_LBP  "LBP"
56 #define CC_RECT "rect"
57
58 #define CC_HOG "HOG"
59
60 #ifdef _WIN32
61 #define TIME( arg ) (((double) clock()) / CLOCKS_PER_SEC)
62 #else
63 #define TIME( arg ) (time( arg ))
64 #endif
65
66 class CvCascadeParams : public CvParams
67 {
68 public:
69     enum { BOOST = 0 };
70     static const int defaultStageType = BOOST;
71     static const int defaultFeatureType = CvFeatureParams::HAAR;
72
73     CvCascadeParams();
74     CvCascadeParams( int _stageType, int _featureType );
75     void write( FileStorage &fs ) const;
76     bool read( const FileNode &node );
77
78     void printDefaults() const;
79     void printAttrs() const;
80     bool scanAttr( const std::string prmName, const std::string val );
81
82     int stageType;
83     int featureType;
84     Size winSize;
85 };
86
87 class CvCascadeClassifier
88 {
89 public:
90     bool train( const std::string _cascadeDirName,
91                 const std::string _posFilename,
92                 const std::string _negFilename,
93                 int _numPos, int _numNeg,
94                 int _precalcValBufSize, int _precalcIdxBufSize,
95                 int _numStages,
96                 const CvCascadeParams& _cascadeParams,
97                 const CvFeatureParams& _featureParams,
98                 const CvCascadeBoostParams& _stageParams,
99                 bool baseFormatSave = false );
100 private:
101     int predict( int sampleIdx );
102     void save( const std::string cascadeDirName, bool baseFormat = false );
103     bool load( const std::string cascadeDirName );
104     bool updateTrainingSet( double& acceptanceRatio );
105     int fillPassedSamples( int first, int count, bool isPositive, int64& consumed );
106
107     void writeParams( FileStorage &fs ) const;
108     void writeStages( FileStorage &fs, const Mat& featureMap ) const;
109     void writeFeatures( FileStorage &fs, const Mat& featureMap ) const;
110     bool readParams( const FileNode &node );
111     bool readStages( const FileNode &node );
112
113     void getUsedFeaturesIdxMap( Mat& featureMap );
114
115     CvCascadeParams cascadeParams;
116     Ptr<CvFeatureParams> featureParams;
117     Ptr<CvCascadeBoostParams> stageParams;
118
119     Ptr<CvFeatureEvaluator> featureEvaluator;
120     std::vector< Ptr<CvCascadeBoost> > stageClassifiers;
121     CvCascadeImageReader imgReader;
122     int numStages, curNumSamples;
123     int numPos, numNeg;
124 };
125
126 #endif