1 #ifndef _OPENCV_HAARFEATURES_H_
2 #define _OPENCV_HAARFEATURES_H_
4 #include "traincascade_features.h"
6 #define CV_HAAR_FEATURE_MAX 3
8 #define HFP_NAME "haarFeatureParams"
9 class CvHaarFeatureParams : public CvFeatureParams
12 enum { BASIC = 0, CORE = 1, ALL = 2 };
14 * 1 - CORE = All upright
15 * 2 - ALL = All features */
17 CvHaarFeatureParams();
18 CvHaarFeatureParams( int _mode );
20 virtual void init( const CvFeatureParams& fp );
21 virtual void write( cv::FileStorage &fs ) const;
22 virtual bool read( const cv::FileNode &node );
24 virtual void printDefaults() const;
25 virtual void printAttrs() const;
26 virtual bool scanAttr( const std::string prm, const std::string val);
31 class CvHaarEvaluator : public CvFeatureEvaluator
34 virtual void init(const CvFeatureParams *_featureParams,
35 int _maxSampleCount, cv::Size _winSize );
36 virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
37 virtual float operator()(int featureIdx, int sampleIdx) const;
38 virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
39 void writeFeature( cv::FileStorage &fs, int fi ) const; // for old file fornat
41 virtual void generateFeatures();
47 Feature( int offset, bool _tilted,
48 int x0, int y0, int w0, int h0, float wt0,
49 int x1, int y1, int w1, int h1, float wt1,
50 int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F );
51 float calc( const cv::Mat &sum, const cv::Mat &tilted, size_t y) const;
52 void write( cv::FileStorage &fs ) const;
59 } rect[CV_HAAR_FEATURE_MAX];
64 } fastRect[CV_HAAR_FEATURE_MAX];
67 std::vector<Feature> features;
68 cv::Mat sum; /* sum images (each row represents image) */
69 cv::Mat tilted; /* tilted sum images (each row represents image) */
70 cv::Mat normfactor; /* normalization factor */
73 inline float CvHaarEvaluator::operator()(int featureIdx, int sampleIdx) const
75 float nf = normfactor.at<float>(0, sampleIdx);
76 return !nf ? 0.0f : (features[featureIdx].calc( sum, tilted, sampleIdx)/nf);
79 inline float CvHaarEvaluator::Feature::calc( const cv::Mat &_sum, const cv::Mat &_tilted, size_t y) const
81 const int* img = tilted ? _tilted.ptr<int>((int)y) : _sum.ptr<int>((int)y);
82 float ret = rect[0].weight * (img[fastRect[0].p0] - img[fastRect[0].p1] - img[fastRect[0].p2] + img[fastRect[0].p3] ) +
83 rect[1].weight * (img[fastRect[1].p0] - img[fastRect[1].p1] - img[fastRect[1].p2] + img[fastRect[1].p3] );
84 if( rect[2].weight != 0.0f )
85 ret += rect[2].weight * (img[fastRect[2].p0] - img[fastRect[2].p1] - img[fastRect[2].p2] + img[fastRect[2].p3] );