2010-05-11 19:44:00 +02:00
|
|
|
#ifndef _OPENCV_HAARFEATURES_H_
|
|
|
|
#define _OPENCV_HAARFEATURES_H_
|
|
|
|
|
|
|
|
#include "traincascade_features.h"
|
|
|
|
|
|
|
|
#define CV_HAAR_FEATURE_MAX 3
|
|
|
|
|
|
|
|
#define HFP_NAME "haarFeatureParams"
|
|
|
|
class CvHaarFeatureParams : public CvFeatureParams
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum { BASIC = 0, CORE = 1, ALL = 2 };
|
|
|
|
/* 0 - BASIC = Viola
|
|
|
|
* 1 - CORE = All upright
|
|
|
|
* 2 - ALL = All features */
|
2012-10-17 01:18:30 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
CvHaarFeatureParams();
|
|
|
|
CvHaarFeatureParams( int _mode );
|
2012-10-17 01:18:30 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
virtual void init( const CvFeatureParams& fp );
|
2013-11-16 16:56:08 +01:00
|
|
|
virtual void write( cv::FileStorage &fs ) const;
|
|
|
|
virtual bool read( const cv::FileNode &node );
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-10-17 01:18:30 +02:00
|
|
|
virtual void printDefaults() const;
|
2010-05-11 19:44:00 +02:00
|
|
|
virtual void printAttrs() const;
|
2013-02-24 17:14:01 +01:00
|
|
|
virtual bool scanAttr( const std::string prm, const std::string val);
|
2012-10-17 01:18:30 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
int mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CvHaarEvaluator : public CvFeatureEvaluator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void init(const CvFeatureParams *_featureParams,
|
2013-11-16 16:56:08 +01:00
|
|
|
int _maxSampleCount, cv::Size _winSize );
|
|
|
|
virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx);
|
2010-05-11 19:44:00 +02:00
|
|
|
virtual float operator()(int featureIdx, int sampleIdx) const;
|
2013-11-16 16:56:08 +01:00
|
|
|
virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const;
|
|
|
|
void writeFeature( cv::FileStorage &fs, int fi ) const; // for old file fornat
|
2010-05-11 19:44:00 +02:00
|
|
|
protected:
|
|
|
|
virtual void generateFeatures();
|
|
|
|
|
|
|
|
class Feature
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Feature();
|
|
|
|
Feature( int offset, bool _tilted,
|
|
|
|
int x0, int y0, int w0, int h0, float wt0,
|
|
|
|
int x1, int y1, int w1, int h1, float wt1,
|
2012-10-17 01:18:30 +02:00
|
|
|
int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F );
|
2013-11-16 16:56:08 +01:00
|
|
|
float calc( const cv::Mat &sum, const cv::Mat &tilted, size_t y) const;
|
|
|
|
void write( cv::FileStorage &fs ) const;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
bool tilted;
|
|
|
|
struct
|
|
|
|
{
|
2013-11-16 16:56:08 +01:00
|
|
|
cv::Rect r;
|
2010-05-11 19:44:00 +02:00
|
|
|
float weight;
|
|
|
|
} rect[CV_HAAR_FEATURE_MAX];
|
|
|
|
|
2012-10-17 01:18:30 +02:00
|
|
|
struct
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
int p0, p1, p2, p3;
|
|
|
|
} fastRect[CV_HAAR_FEATURE_MAX];
|
2012-10-17 01:18:30 +02:00
|
|
|
};
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2013-02-24 17:14:01 +01:00
|
|
|
std::vector<Feature> features;
|
2013-11-16 16:56:08 +01:00
|
|
|
cv::Mat sum; /* sum images (each row represents image) */
|
|
|
|
cv::Mat tilted; /* tilted sum images (each row represents image) */
|
|
|
|
cv::Mat normfactor; /* normalization factor */
|
2010-05-11 19:44:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
inline float CvHaarEvaluator::operator()(int featureIdx, int sampleIdx) const
|
|
|
|
{
|
|
|
|
float nf = normfactor.at<float>(0, sampleIdx);
|
2012-10-17 01:18:30 +02:00
|
|
|
return !nf ? 0.0f : (features[featureIdx].calc( sum, tilted, sampleIdx)/nf);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2013-11-16 16:56:08 +01:00
|
|
|
inline float CvHaarEvaluator::Feature::calc( const cv::Mat &_sum, const cv::Mat &_tilted, size_t y) const
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
const int* img = tilted ? _tilted.ptr<int>((int)y) : _sum.ptr<int>((int)y);
|
|
|
|
float ret = rect[0].weight * (img[fastRect[0].p0] - img[fastRect[0].p1] - img[fastRect[0].p2] + img[fastRect[0].p3] ) +
|
|
|
|
rect[1].weight * (img[fastRect[1].p0] - img[fastRect[1].p1] - img[fastRect[1].p2] + img[fastRect[1].p3] );
|
|
|
|
if( rect[2].weight != 0.0f )
|
|
|
|
ret += rect[2].weight * (img[fastRect[2].p0] - img[fastRect[2].p1] - img[fastRect[2].p2] + img[fastRect[2].p3] );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|