1 #ifndef _OPENCV_LBPFEATURES_H_
2 #define _OPENCV_LBPFEATURES_H_
3 
4 #include "traincascade_features.h"
5 
6 #define LBPF_NAME "lbpFeatureParams"
7 struct CvLBPFeatureParams : CvFeatureParams
8 {
9     CvLBPFeatureParams();
10 
11 };
12 
13 class CvLBPEvaluator : public CvFeatureEvaluator
14 {
15 public:
~CvLBPEvaluator()16     virtual ~CvLBPEvaluator() {}
17     virtual void init(const CvFeatureParams *_featureParams,
18         int _maxSampleCount, cv::Size _winSize );
19     virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
operator()20     virtual float operator()(int featureIdx, int sampleIdx) const
21     { return (float)features[featureIdx].calc( sum, sampleIdx); }
22     virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
23 protected:
24     virtual void generateFeatures();
25 
26     class Feature
27     {
28     public:
29         Feature();
30         Feature( int offset, int x, int y, int _block_w, int _block_h  );
31         uchar calc( const cv::Mat& _sum, size_t y ) const;
32         void write( cv::FileStorage &fs ) const;
33 
34         cv::Rect rect;
35         int p[16];
36     };
37     std::vector<Feature> features;
38 
39     cv::Mat sum;
40 };
41 
calc(const cv::Mat & _sum,size_t y)42 inline uchar CvLBPEvaluator::Feature::calc(const cv::Mat &_sum, size_t y) const
43 {
44     const int* psum = _sum.ptr<int>((int)y);
45     int cval = psum[p[5]] - psum[p[6]] - psum[p[9]] + psum[p[10]];
46 
47     return (uchar)((psum[p[0]] - psum[p[1]] - psum[p[4]] + psum[p[5]] >= cval ? 128 : 0) |   // 0
48         (psum[p[1]] - psum[p[2]] - psum[p[5]] + psum[p[6]] >= cval ? 64 : 0) |    // 1
49         (psum[p[2]] - psum[p[3]] - psum[p[6]] + psum[p[7]] >= cval ? 32 : 0) |    // 2
50         (psum[p[6]] - psum[p[7]] - psum[p[10]] + psum[p[11]] >= cval ? 16 : 0) |  // 5
51         (psum[p[10]] - psum[p[11]] - psum[p[14]] + psum[p[15]] >= cval ? 8 : 0) | // 8
52         (psum[p[9]] - psum[p[10]] - psum[p[13]] + psum[p[14]] >= cval ? 4 : 0) |  // 7
53         (psum[p[8]] - psum[p[9]] - psum[p[12]] + psum[p[13]] >= cval ? 2 : 0) |   // 6
54         (psum[p[4]] - psum[p[5]] - psum[p[8]] + psum[p[9]] >= cval ? 1 : 0));     // 3
55 }
56 
57 #endif
58