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