lbpfeatures.h 2.0 KB

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