haarfeatures.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef _OPENCV_HAARFEATURES_H_
  2. #define _OPENCV_HAARFEATURES_H_
  3. #include "traincascade_features.h"
  4. #define CV_HAAR_FEATURE_MAX 3
  5. #define HFP_NAME "haarFeatureParams"
  6. class CvHaarFeatureParams : public CvFeatureParams
  7. {
  8. public:
  9. enum { BASIC = 0, CORE = 1, ALL = 2 };
  10. /* 0 - BASIC = Viola
  11. * 1 - CORE = All upright
  12. * 2 - ALL = All features */
  13. CvHaarFeatureParams();
  14. CvHaarFeatureParams( int _mode );
  15. virtual void init( const CvFeatureParams& fp );
  16. virtual void write( cv::FileStorage &fs ) const;
  17. virtual bool read( const cv::FileNode &node );
  18. virtual void printDefaults() const;
  19. virtual void printAttrs() const;
  20. virtual bool scanAttr( const std::string prm, const std::string val);
  21. int mode;
  22. };
  23. class CvHaarEvaluator : public CvFeatureEvaluator
  24. {
  25. public:
  26. virtual void init(const CvFeatureParams *_featureParams,
  27. int _maxSampleCount, cv::Size _winSize );
  28. virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
  29. virtual float operator()(int featureIdx, int sampleIdx) const;
  30. virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
  31. void writeFeature( cv::FileStorage &fs, int fi ) const; // for old file fornat
  32. protected:
  33. virtual void generateFeatures();
  34. class Feature
  35. {
  36. public:
  37. Feature();
  38. Feature( int offset, bool _tilted,
  39. int x0, int y0, int w0, int h0, float wt0,
  40. int x1, int y1, int w1, int h1, float wt1,
  41. int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F );
  42. float calc( const cv::Mat &sum, const cv::Mat &tilted, size_t y) const;
  43. void write( cv::FileStorage &fs ) const;
  44. bool tilted;
  45. struct
  46. {
  47. cv::Rect r;
  48. float weight;
  49. } rect[CV_HAAR_FEATURE_MAX];
  50. struct
  51. {
  52. int p0, p1, p2, p3;
  53. } fastRect[CV_HAAR_FEATURE_MAX];
  54. };
  55. std::vector<Feature> features;
  56. cv::Mat sum; /* sum images (each row represents image) */
  57. cv::Mat tilted; /* tilted sum images (each row represents image) */
  58. cv::Mat normfactor; /* normalization factor */
  59. };
  60. inline float CvHaarEvaluator::operator()(int featureIdx, int sampleIdx) const
  61. {
  62. float nf = normfactor.at<float>(0, sampleIdx);
  63. return !nf ? 0.0f : (features[featureIdx].calc( sum, tilted, sampleIdx)/nf);
  64. }
  65. inline float CvHaarEvaluator::Feature::calc( const cv::Mat &_sum, const cv::Mat &_tilted, size_t y) const
  66. {
  67. const int* img = tilted ? _tilted.ptr<int>((int)y) : _sum.ptr<int>((int)y);
  68. float ret = rect[0].weight * (img[fastRect[0].p0] - img[fastRect[0].p1] - img[fastRect[0].p2] + img[fastRect[0].p3] ) +
  69. rect[1].weight * (img[fastRect[1].p0] - img[fastRect[1].p1] - img[fastRect[1].p2] + img[fastRect[1].p3] );
  70. if( rect[2].weight != 0.0f )
  71. ret += rect[2].weight * (img[fastRect[2].p0] - img[fastRect[2].p1] - img[fastRect[2].p2] + img[fastRect[2].p3] );
  72. return ret;
  73. }
  74. #endif