1 #ifndef _OPENCV_HAARFEATURES_H_
2 #define _OPENCV_HAARFEATURES_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( FileStorage &fs ) const;
23 virtual void printDefaults() const;
24 virtual void printAttrs() const;
25 virtual bool scanAttr( const String prm, const String val);
30 class CvHaarEvaluator : public CvFeatureEvaluator
33 virtual void init(const CvFeatureParams *_featureParams,
34 int _maxSampleCount, Size _winSize );
35 virtual void setImage(const Mat& img, uchar clsLabel, int idx);
36 virtual float operator()(int featureIdx, int sampleIdx) const;
37 virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const;
38 void writeFeature( FileStorage &fs, int fi ) const; // for old file fornat
40 virtual void generateFeatures();
46 Feature( int offset, bool _tilted,
47 int x0, int y0, int w0, int h0, float wt0,
48 int x1, int y1, int w1, int h1, float wt1,
49 int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F );
50 float calc( const Mat &sum, const Mat &tilted, size_t y) const;
51 void write( FileStorage &fs ) const;
58 } rect[CV_HAAR_FEATURE_MAX];
63 } fastRect[CV_HAAR_FEATURE_MAX];
66 vector<Feature> features;
67 Mat sum; /* sum images (each row represents image) */
68 Mat tilted; /* tilted sum images (each row represents image) */
69 Mat normfactor; /* normalization factor */
72 inline float CvHaarEvaluator::operator()(int featureIdx, int sampleIdx) const
74 float nf = normfactor.at<float>(0, sampleIdx);
75 return !nf ? 0.0f : (features[featureIdx].calc( sum, tilted, sampleIdx)/nf);
78 inline float CvHaarEvaluator::Feature::calc( const Mat &_sum, const Mat &_tilted, size_t y) const
80 const int* img = tilted ? _tilted.ptr<int>((int)y) : _sum.ptr<int>((int)y);
81 float ret = rect[0].weight * (img[fastRect[0].p0] - img[fastRect[0].p1] - img[fastRect[0].p2] + img[fastRect[0].p3] ) +
82 rect[1].weight * (img[fastRect[1].p0] - img[fastRect[1].p1] - img[fastRect[1].p2] + img[fastRect[1].p3] );
83 if( rect[2].weight != 0.0f )
84 ret += rect[2].weight * (img[fastRect[2].p0] - img[fastRect[2].p1] - img[fastRect[2].p2] + img[fastRect[2].p3] );