Update to 2.0.0 tree from current Fremantle build
[opencv] / apps / traincascade / haarfeatures.h
1 #ifndef _OPENCV_HAARFEATURES_H_
2 #define _OPENCV_HAARFEATURES_H_
3
4 #include "features.h"
5
6 #define CV_HAAR_FEATURE_MAX      3
7
8 #define HFP_NAME "haarFeatureParams"
9 class CvHaarFeatureParams : public CvFeatureParams
10 {
11 public:
12     enum { BASIC = 0, CORE = 1, ALL = 2 };
13      /* 0 - BASIC = Viola
14      *  1 - CORE  = All upright
15      *  2 - ALL   = All features */
16     
17     CvHaarFeatureParams();
18     CvHaarFeatureParams( int _mode );
19     
20     virtual void init( const CvFeatureParams& fp );
21     virtual void write( FileStorage &fs ) const;
22
23     virtual void printDefaults() const;  
24     virtual void printAttrs() const;
25     virtual bool scanAttr( const String prm, const String val);
26     
27     int mode;
28 };
29
30 class CvHaarEvaluator : public CvFeatureEvaluator
31 {
32 public:
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
39 protected:
40     virtual void generateFeatures();
41
42     class Feature
43     {
44     public:
45         Feature();
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;
52
53         bool  tilted;
54         struct
55         {
56             Rect r;
57             float weight;
58         } rect[CV_HAAR_FEATURE_MAX];
59
60         struct                      
61         {
62             int p0, p1, p2, p3;
63         } fastRect[CV_HAAR_FEATURE_MAX];
64     }; 
65
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 */
70 };
71
72 inline float CvHaarEvaluator::operator()(int featureIdx, int sampleIdx) const
73 {
74     float nf = normfactor.at<float>(0, sampleIdx);
75     return !nf ? 0.0f : (features[featureIdx].calc( sum, tilted, sampleIdx)/nf); 
76 }
77
78 inline float CvHaarEvaluator::Feature::calc( const Mat &_sum, const Mat &_tilted, size_t y) const
79 {
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] );
85     return ret;
86 }
87
88 #endif