1 #ifndef _OPENCV_FEATURES_H_ 2 #define _OPENCV_FEATURES_H_ 3 4 #include "imagestorage.h" 5 #include <stdio.h> 6 7 #define FEATURES "features" 8 9 #define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \ 10 /* (x, y) */ \ 11 (p0) = (rect).x + (step) * (rect).y; \ 12 /* (x + w, y) */ \ 13 (p1) = (rect).x + (rect).width + (step) * (rect).y; \ 14 /* (x + w, y) */ \ 15 (p2) = (rect).x + (step) * ((rect).y + (rect).height); \ 16 /* (x + w, y + h) */ \ 17 (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height); 18 19 #define CV_TILTED_OFFSETS( p0, p1, p2, p3, rect, step ) \ 20 /* (x, y) */ \ 21 (p0) = (rect).x + (step) * (rect).y; \ 22 /* (x - h, y + h) */ \ 23 (p1) = (rect).x - (rect).height + (step) * ((rect).y + (rect).height);\ 24 /* (x + w, y + w) */ \ 25 (p2) = (rect).x + (rect).width + (step) * ((rect).y + (rect).width); \ 26 /* (x + w - h, y + w + h) */ \ 27 (p3) = (rect).x + (rect).width - (rect).height \ 28 + (step) * ((rect).y + (rect).width + (rect).height); 29 30 float calcNormFactor( const cv::Mat& sum, const cv::Mat& sqSum ); 31 32 template<class Feature> 33 void _writeFeatures( const std::vector<Feature> features, cv::FileStorage &fs, const cv::Mat& featureMap ) 34 { 35 fs << FEATURES << "["; 36 const cv::Mat_<int>& featureMap_ = (const cv::Mat_<int>&)featureMap; 37 for ( int fi = 0; fi < featureMap.cols; fi++ ) 38 if ( featureMap_(0, fi) >= 0 ) 39 { 40 fs << "{"; 41 features[fi].write( fs ); 42 fs << "}"; 43 } 44 fs << "]"; 45 } 46 47 class CvParams 48 { 49 public: 50 CvParams(); 51 virtual ~CvParams() {} 52 // from|to file 53 virtual void write( cv::FileStorage &fs ) const = 0; 54 virtual bool read( const cv::FileNode &node ) = 0; 55 // from|to screen 56 virtual void printDefaults() const; 57 virtual void printAttrs() const; 58 virtual bool scanAttr( const std::string prmName, const std::string val ); 59 std::string name; 60 }; 61 62 class CvFeatureParams : public CvParams 63 { 64 public: 65 enum { HAAR = 0, LBP = 1, HOG = 2 }; 66 CvFeatureParams(); 67 virtual void init( const CvFeatureParams& fp ); 68 virtual void write( cv::FileStorage &fs ) const; 69 virtual bool read( const cv::FileNode &node ); 70 static cv::Ptr<CvFeatureParams> create( int featureType ); 71 int maxCatCount; // 0 in case of numerical features 72 int featSize; // 1 in case of simple features (HAAR, LBP) and N_BINS(9)*N_CELLS(4) in case of Dalal's HOG features 73 }; 74 75 class CvFeatureEvaluator 76 { 77 public: 78 virtual ~CvFeatureEvaluator() {} 79 virtual void init(const CvFeatureParams *_featureParams, 80 int _maxSampleCount, cv::Size _winSize ); 81 virtual void setImage(const cv::Mat& img, uchar clsLabel, int idx); 82 virtual void writeFeatures( cv::FileStorage &fs, const cv::Mat& featureMap ) const = 0; 83 virtual float operator()(int featureIdx, int sampleIdx) const = 0; 84 static cv::Ptr<CvFeatureEvaluator> create(int type); 85 86 int getNumFeatures() const { return numFeatures; } 87 int getMaxCatCount() const { return featureParams->maxCatCount; } 88 int getFeatureSize() const { return featureParams->featSize; } 89 const cv::Mat& getCls() const { return cls; } 90 float getCls(int si) const { return cls.at<float>(si, 0); } 91 protected: 92 virtual void generateFeatures() = 0; 93 94 int npos, nneg; 95 int numFeatures; 96 cv::Size winSize; 97 CvFeatureParams *featureParams; 98 cv::Mat cls; 99 }; 100 101 #endif 102