1 #ifndef _OPENCV_HOGFEATURES_H_
2 #define _OPENCV_HOGFEATURES_H_
4 #include "traincascade_features.h"
6 //#define TEST_INTHIST_BUILD
7 //#define TEST_FEAT_CALC
12 #define HOGF_NAME "HOGFeatureParams"
13 struct CvHOGFeatureParams : public CvFeatureParams
18 class CvHOGEvaluator : public CvFeatureEvaluator
21 virtual ~CvHOGEvaluator() {}
22 virtual void init(const CvFeatureParams *_featureParams,
23 int _maxSampleCount, Size _winSize );
24 virtual void setImage(const Mat& img, uchar clsLabel, int idx);
25 virtual float operator()(int varIdx, int sampleIdx) const;
26 virtual void writeFeatures( FileStorage &fs, const Mat& featureMap ) const;
28 virtual void generateFeatures();
29 virtual void integralHistogram(const Mat &img, std::vector<Mat> &histogram, Mat &norm, int nbins) const;
34 Feature( int offset, int x, int y, int cellW, int cellH );
35 float calc( const std::vector<Mat> &_hists, const Mat &_normSum, size_t y, int featComponent ) const;
36 void write( FileStorage &fs ) const;
37 void write( FileStorage &fs, int varIdx ) const;
39 Rect rect[N_CELLS]; //cells
46 std::vector<Feature> features;
48 Mat normSum; //for nomalization calculation (L1 or L2)
49 std::vector<Mat> hist;
52 inline float CvHOGEvaluator::operator()(int varIdx, int sampleIdx) const
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);
60 inline float CvHOGEvaluator::Feature::calc( const std::vector<Mat>& _hists, const Mat& _normSum, size_t y, int featComponent ) const
65 int binIdx = featComponent % N_BINS;
66 int cellIdx = featComponent / N_BINS;
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];
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
78 #endif // _OPENCV_HOGFEATURES_H_