2012-10-17 15:12:04 +08:00
|
|
|
#ifndef _OPENCV_HOGFEATURES_H_
|
|
|
|
#define _OPENCV_HOGFEATURES_H_
|
|
|
|
|
|
|
|
#include "traincascade_features.h"
|
|
|
|
|
|
|
|
//#define TEST_INTHIST_BUILD
|
|
|
|
//#define TEST_FEAT_CALC
|
|
|
|
|
|
|
|
#define N_BINS 9
|
|
|
|
#define N_CELLS 4
|
|
|
|
|
|
|
|
#define HOGF_NAME "HOGFeatureParams"
|
|
|
|
struct CvHOGFeatureParams : public CvFeatureParams
|
|
|
|
{
|
|
|
|
CvHOGFeatureParams();
|
|
|
|
};
|
|
|
|
|
|
|
|
class CvHOGEvaluator : public CvFeatureEvaluator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~CvHOGEvaluator() {}
|
|
|
|
virtual void init(const CvFeatureParams *_featureParams,
|
2013-11-16 23:56:08 +08:00
|
|
|
int _maxSampleCount, cv::Size _winSize );
|
|
|
|
virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
|
2012-10-17 15:12:04 +08:00
|
|
|
virtual float operator()(int varIdx, int sampleIdx) const;
|
2013-11-16 23:56:08 +08:00
|
|
|
virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
|
2012-10-17 15:12:04 +08:00
|
|
|
protected:
|
|
|
|
virtual void generateFeatures();
|
2013-11-16 23:56:08 +08:00
|
|
|
virtual void integralHistogram(const cv::Mat &img, std::vector<cv::Mat> &histogram, cv::Mat &norm, int nbins) const;
|
2012-10-17 15:12:04 +08:00
|
|
|
class Feature
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Feature();
|
|
|
|
Feature( int offset, int x, int y, int cellW, int cellH );
|
2013-11-16 23:56:08 +08:00
|
|
|
float calc( const std::vector<cv::Mat> &_hists, const cv::Mat &_normSum, size_t y, int featComponent ) const;
|
|
|
|
void write( cv::FileStorage &fs ) const;
|
|
|
|
void write( cv::FileStorage &fs, int varIdx ) const;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-11-16 23:56:08 +08:00
|
|
|
cv::Rect rect[N_CELLS]; //cells
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
int p0, p1, p2, p3;
|
|
|
|
} fastRect[N_CELLS];
|
|
|
|
};
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Feature> features;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-11-16 23:56:08 +08:00
|
|
|
cv::Mat normSum; //for nomalization calculation (L1 or L2)
|
|
|
|
std::vector<cv::Mat> hist;
|
2012-10-17 15:12:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
inline float CvHOGEvaluator::operator()(int varIdx, int sampleIdx) const
|
|
|
|
{
|
|
|
|
int featureIdx = varIdx / (N_BINS * N_CELLS);
|
|
|
|
int componentIdx = varIdx % (N_BINS * N_CELLS);
|
|
|
|
//return features[featureIdx].calc( hist, sampleIdx, componentIdx);
|
|
|
|
return features[featureIdx].calc( hist, normSum, sampleIdx, componentIdx);
|
|
|
|
}
|
|
|
|
|
2013-11-16 23:56:08 +08:00
|
|
|
inline float CvHOGEvaluator::Feature::calc( const std::vector<cv::Mat>& _hists, const cv::Mat& _normSum, size_t y, int featComponent ) const
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
|
|
|
float normFactor;
|
|
|
|
float res;
|
|
|
|
|
|
|
|
int binIdx = featComponent % N_BINS;
|
|
|
|
int cellIdx = featComponent / N_BINS;
|
|
|
|
|
|
|
|
const float *phist = _hists[binIdx].ptr<float>((int)y);
|
|
|
|
res = phist[fastRect[cellIdx].p0] - phist[fastRect[cellIdx].p1] - phist[fastRect[cellIdx].p2] + phist[fastRect[cellIdx].p3];
|
|
|
|
|
|
|
|
const float *pnormSum = _normSum.ptr<float>((int)y);
|
|
|
|
normFactor = (float)(pnormSum[fastRect[0].p0] - pnormSum[fastRect[1].p1] - pnormSum[fastRect[2].p2] + pnormSum[fastRect[3].p3]);
|
|
|
|
res = (res > 0.001f) ? ( res / (normFactor + 0.001f) ) : 0.f; //for cutting negative values, which apper due to floating precision
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _OPENCV_HOGFEATURES_H_
|