lbpfeatures.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/imgproc.hpp"
  3. #include "lbpfeatures.h"
  4. #include "cascadeclassifier.h"
  5. using namespace cv;
  6. CvLBPFeatureParams::CvLBPFeatureParams()
  7. {
  8. maxCatCount = 256;
  9. name = LBPF_NAME;
  10. }
  11. void CvLBPEvaluator::init(const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize)
  12. {
  13. CV_Assert( _maxSampleCount > 0);
  14. sum.create((int)_maxSampleCount, (_winSize.width + 1) * (_winSize.height + 1), CV_32SC1);
  15. CvFeatureEvaluator::init( _featureParams, _maxSampleCount, _winSize );
  16. }
  17. void CvLBPEvaluator::setImage(const Mat &img, uchar clsLabel, int idx)
  18. {
  19. CV_DbgAssert( !sum.empty() );
  20. CvFeatureEvaluator::setImage( img, clsLabel, idx );
  21. Mat innSum(winSize.height + 1, winSize.width + 1, sum.type(), sum.ptr<int>((int)idx));
  22. integral( img, innSum );
  23. }
  24. void CvLBPEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
  25. {
  26. _writeFeatures( features, fs, featureMap );
  27. }
  28. void CvLBPEvaluator::generateFeatures()
  29. {
  30. int offset = winSize.width + 1;
  31. for( int x = 0; x < winSize.width; x++ )
  32. for( int y = 0; y < winSize.height; y++ )
  33. for( int w = 1; w <= winSize.width / 3; w++ )
  34. for( int h = 1; h <= winSize.height / 3; h++ )
  35. if ( (x+3*w <= winSize.width) && (y+3*h <= winSize.height) )
  36. features.push_back( Feature(offset, x, y, w, h ) );
  37. numFeatures = (int)features.size();
  38. }
  39. CvLBPEvaluator::Feature::Feature()
  40. {
  41. rect = cvRect(0, 0, 0, 0);
  42. }
  43. CvLBPEvaluator::Feature::Feature( int offset, int x, int y, int _blockWidth, int _blockHeight )
  44. {
  45. Rect tr = rect = cvRect(x, y, _blockWidth, _blockHeight);
  46. CV_SUM_OFFSETS( p[0], p[1], p[4], p[5], tr, offset )
  47. tr.x += 2*rect.width;
  48. CV_SUM_OFFSETS( p[2], p[3], p[6], p[7], tr, offset )
  49. tr.y +=2*rect.height;
  50. CV_SUM_OFFSETS( p[10], p[11], p[14], p[15], tr, offset )
  51. tr.x -= 2*rect.width;
  52. CV_SUM_OFFSETS( p[8], p[9], p[12], p[13], tr, offset )
  53. }
  54. void CvLBPEvaluator::Feature::write(FileStorage &fs) const
  55. {
  56. fs << CC_RECT << "[:" << rect.x << rect.y << rect.width << rect.height << "]";
  57. }