Update to 2.0.0 tree from current Fremantle build
[opencv] / apps / traincascade / features.h
1 #ifndef _OPENCV_FEATURES_H_
2 #define _OPENCV_FEATURES_H_
3
4 #include "imagestorage.h"
5 #include "cxcore.h"
6 #include "cv.h"
7 #include "ml.h"
8 #include <stdio.h>
9
10 #define FEATURES "features"
11
12 #define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step )                      \
13     /* (x, y) */                                                          \
14     (p0) = (rect).x + (step) * (rect).y;                                  \
15     /* (x + w, y) */                                                      \
16     (p1) = (rect).x + (rect).width + (step) * (rect).y;                   \
17     /* (x + w, y) */                                                      \
18     (p2) = (rect).x + (step) * ((rect).y + (rect).height);                \
19     /* (x + w, y + h) */                                                  \
20     (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);
21
22 #define CV_TILTED_OFFSETS( p0, p1, p2, p3, rect, step )                   \
23     /* (x, y) */                                                          \
24     (p0) = (rect).x + (step) * (rect).y;                                  \
25     /* (x - h, y + h) */                                                  \
26     (p1) = (rect).x - (rect).height + (step) * ((rect).y + (rect).height);\
27     /* (x + w, y + w) */                                                  \
28     (p2) = (rect).x + (rect).width + (step) * ((rect).y + (rect).width);  \
29     /* (x + w - h, y + w + h) */                                          \
30     (p3) = (rect).x + (rect).width - (rect).height                        \
31            + (step) * ((rect).y + (rect).width + (rect).height);
32
33 float calcNormFactor( const Mat& sum, const Mat& sqSum );
34
35 template<class Feature>
36 void _writeFeatures( const vector<Feature> features, FileStorage &fs, const Mat& featureMap )
37 {
38     fs << FEATURES << "[";
39     const Mat_<int>& featureMap_ = (const Mat_<int>&)featureMap;
40     for ( int fi = 0; fi < featureMap.cols; fi++ )
41         if ( featureMap_(0, fi) >= 0 )
42         {
43             fs << "{";
44             features[fi].write( fs );
45             fs << "}";
46         }
47     fs << "]";
48 }
49
50 class CvParams
51 {
52 public:
53     CvParams();
54     virtual ~CvParams() {}
55     // from|to file
56     virtual void write( FileStorage &fs ) const = 0;
57     virtual bool read( const FileNode &node ) = 0;
58     // from|to screen
59     virtual void printDefaults() const;
60     virtual void printAttrs() const;
61     virtual bool scanAttr( const String prmName, const String val );
62     String name;
63 };
64
65 class CvFeatureParams : public CvParams
66 {
67 public:
68     enum { HAAR = 0, LBP = 1 };
69     CvFeatureParams();
70     virtual void init( const CvFeatureParams& fp );
71     virtual void write( FileStorage &fs ) const;
72     virtual bool read( const FileNode &node );
73     static Ptr<CvFeatureParams> create( int featureType );
74     int maxCatCount; // 0 in case of numerical features
75 };
76
77 class CvFeatureEvaluator
78 {
79 public:
80     virtual ~CvFeatureEvaluator() {}
81     virtual void init(const CvFeatureParams *_featureParams,
82                       int _maxSampleCount, Size _winSize );
83     virtual void setImage(const Mat& img, uchar clsLabel, int idx);
84     virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const = 0;
85     virtual float operator()(int featureIdx, int sampleIdx) const = 0;
86     static Ptr<CvFeatureEvaluator> create(int type);
87
88     int getNumFeatures() const { return numFeatures; }
89     int getMaxCatCount() const { return featureParams->maxCatCount; }
90     const Mat& getCls() const { return cls; }
91     float getCls(int si) const { return cls.at<float>(si, 0); }
92 protected:
93     virtual void generateFeatures() = 0;
94
95     int npos, nneg;
96     int numFeatures;
97     Size winSize;
98     CvFeatureParams *featureParams;
99     Mat cls;
100 };
101
102 #endif