1 #include "opencv2/core.hpp" 2 3 #include "traincascade_features.h" 4 #include "cascadeclassifier.h" 5 6 using namespace std; 7 using namespace cv; 8 9 float calcNormFactor( const Mat& sum, const Mat& sqSum ) 10 { 11 CV_DbgAssert( sum.cols > 3 && sqSum.rows > 3 ); 12 Rect normrect( 1, 1, sum.cols - 3, sum.rows - 3 ); 13 size_t p0, p1, p2, p3; 14 CV_SUM_OFFSETS( p0, p1, p2, p3, normrect, sum.step1() ) 15 double area = normrect.width * normrect.height; 16 const int *sp = sum.ptr<int>(); 17 int valSum = sp[p0] - sp[p1] - sp[p2] + sp[p3]; 18 const double *sqp = sqSum.ptr<double>(); 19 double valSqSum = sqp[p0] - sqp[p1] - sqp[p2] + sqp[p3]; 20 return (float) sqrt( (double) (area * valSqSum - (double)valSum * valSum) ); 21 } 22 23 CvParams::CvParams() : name( "params" ) {} 24 void CvParams::printDefaults() const 25 { cout << "--" << name << "--" << endl; } 26 void CvParams::printAttrs() const {} 27 bool CvParams::scanAttr( const string, const string ) { return false; } 28 29 30 //---------------------------- FeatureParams -------------------------------------- 31 32 CvFeatureParams::CvFeatureParams() : maxCatCount( 0 ), featSize( 1 ) 33 { 34 name = CC_FEATURE_PARAMS; 35 } 36 37 void CvFeatureParams::init( const CvFeatureParams& fp ) 38 { 39 maxCatCount = fp.maxCatCount; 40 featSize = fp.featSize; 41 } 42 43 void CvFeatureParams::write( FileStorage &fs ) const 44 { 45 fs << CC_MAX_CAT_COUNT << maxCatCount; 46 fs << CC_FEATURE_SIZE << featSize; 47 } 48 49 bool CvFeatureParams::read( const FileNode &node ) 50 { 51 if ( node.empty() ) 52 return false; 53 maxCatCount = node[CC_MAX_CAT_COUNT]; 54 featSize = node[CC_FEATURE_SIZE]; 55 return ( maxCatCount >= 0 && featSize >= 1 ); 56 } 57 58 Ptr<CvFeatureParams> CvFeatureParams::create( int featureType ) 59 { 60 return featureType == HAAR ? Ptr<CvFeatureParams>(new CvHaarFeatureParams) : 61 featureType == LBP ? Ptr<CvFeatureParams>(new CvLBPFeatureParams) : 62 featureType == HOG ? Ptr<CvFeatureParams>(new CvHOGFeatureParams) : 63 Ptr<CvFeatureParams>(); 64 } 65 66 //------------------------------------- FeatureEvaluator --------------------------------------- 67 68 void CvFeatureEvaluator::init(const CvFeatureParams *_featureParams, 69 int _maxSampleCount, Size _winSize ) 70 { 71 CV_Assert(_maxSampleCount > 0); 72 featureParams = (CvFeatureParams *)_featureParams; 73 winSize = _winSize; 74 numFeatures = 0; 75 cls.create( (int)_maxSampleCount, 1, CV_32FC1 ); 76 generateFeatures(); 77 } 78 79 void CvFeatureEvaluator::setImage(const Mat &img, uchar clsLabel, int idx) 80 { 81 CV_Assert(img.cols == winSize.width); 82 CV_Assert(img.rows == winSize.height); 83 CV_Assert(idx < cls.rows); 84 cls.ptr<float>(idx)[0] = clsLabel; 85 } 86 87 Ptr<CvFeatureEvaluator> CvFeatureEvaluator::create(int type) 88 { 89 return type == CvFeatureParams::HAAR ? Ptr<CvFeatureEvaluator>(new CvHaarEvaluator) : 90 type == CvFeatureParams::LBP ? Ptr<CvFeatureEvaluator>(new CvLBPEvaluator) : 91 type == CvFeatureParams::HOG ? Ptr<CvFeatureEvaluator>(new CvHOGEvaluator) : 92 Ptr<CvFeatureEvaluator>(); 93 } 94