HOGfeatures.h 2.6 KB

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