Home | History | Annotate | Download | only in traincascade
      1 #ifndef _OPENCV_HOGFEATURES_H_
      2 #define _OPENCV_HOGFEATURES_H_
      3 
      4 #include "traincascade_features.h"
      5 
      6 //#define TEST_INTHIST_BUILD
      7 //#define TEST_FEAT_CALC
      8 
      9 #define N_BINS 9
     10 #define N_CELLS 4
     11 
     12 #define HOGF_NAME "HOGFeatureParams"
     13 struct CvHOGFeatureParams : public CvFeatureParams
     14 {
     15     CvHOGFeatureParams();
     16 };
     17 
     18 class CvHOGEvaluator : public CvFeatureEvaluator
     19 {
     20 public:
     21     virtual ~CvHOGEvaluator() {}
     22     virtual void init(const CvFeatureParams *_featureParams,
     23         int _maxSampleCount, cv::Size _winSize );
     24     virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
     25     virtual float operator()(int varIdx, int sampleIdx) const;
     26     virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
     27 protected:
     28     virtual void generateFeatures();
     29     virtual void integralHistogram(const cv::Mat &img, std::vector<cv::Mat> &histogram, cv::Mat &norm, int nbins) const;
     30     class Feature
     31     {
     32     public:
     33         Feature();
     34         Feature( int offset, int x, int y, int cellW, int cellH );
     35         float calc( const std::vector<cv::Mat> &_hists, const cv::Mat &_normSum, size_t y, int featComponent ) const;
     36         void write( cv::FileStorage &fs ) const;
     37         void write( cv::FileStorage &fs, int varIdx ) const;
     38 
     39         cv::Rect rect[N_CELLS]; //cells
     40 
     41         struct
     42         {
     43             int p0, p1, p2, p3;
     44         } fastRect[N_CELLS];
     45     };
     46     std::vector<Feature> features;
     47 
     48     cv::Mat normSum; //for nomalization calculation (L1 or L2)
     49     std::vector<cv::Mat> hist;
     50 };
     51 
     52 inline float CvHOGEvaluator::operator()(int varIdx, int sampleIdx) const
     53 {
     54     int featureIdx = varIdx / (N_BINS * N_CELLS);
     55     int componentIdx = varIdx % (N_BINS * N_CELLS);
     56     //return features[featureIdx].calc( hist, sampleIdx, componentIdx);
     57     return features[featureIdx].calc( hist, normSum, sampleIdx, componentIdx);
     58 }
     59 
     60 inline float CvHOGEvaluator::Feature::calc( const std::vector<cv::Mat>& _hists, const cv::Mat& _normSum, size_t y, int featComponent ) const
     61 {
     62     float normFactor;
     63     float res;
     64 
     65     int binIdx = featComponent % N_BINS;
     66     int cellIdx = featComponent / N_BINS;
     67 
     68     const float *phist = _hists[binIdx].ptr<float>((int)y);
     69     res = phist[fastRect[cellIdx].p0] - phist[fastRect[cellIdx].p1] - phist[fastRect[cellIdx].p2] + phist[fastRect[cellIdx].p3];
     70 
     71     const float *pnormSum = _normSum.ptr<float>((int)y);
     72     normFactor = (float)(pnormSum[fastRect[0].p0] - pnormSum[fastRect[1].p1] - pnormSum[fastRect[2].p2] + pnormSum[fastRect[3].p3]);
     73     res = (res > 0.001f) ? ( res / (normFactor + 0.001f) ) : 0.f; //for cutting negative values, which apper due to floating precision
     74 
     75     return res;
     76 }
     77 
     78 #endif // _OPENCV_HOGFEATURES_H_
     79