features.cpp 2.9 KB

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