cascadeclassifier.h 3.9 KB

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