Update to 2.0.0 tree from current Fremantle build
[opencv] / apps / traincascade / lbpfeatures.cpp
1 #include "lbpfeatures.h"
2 #include "cascadeclassifier.h"
3
4 CvLBPFeatureParams::CvLBPFeatureParams()
5 {
6     maxCatCount = 256;
7     name = LBPF_NAME;
8 }
9
10 void CvLBPEvaluator::init(const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize)
11 {
12     CV_Assert( _maxSampleCount > 0);
13     sum.create((int)_maxSampleCount, (_winSize.width + 1) * (_winSize.height + 1), CV_32SC1);
14     CvFeatureEvaluator::init( _featureParams, _maxSampleCount, _winSize );
15 }
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 sum(winSize.height + 1, winSize.width + 1, sum.type(), sum.ptr<int>((int)idx));
22     integral( img, sum );
23 }
24
25 void CvLBPEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
26 {
27     _writeFeatures( features, fs, featureMap );
28 }
29
30 void CvLBPEvaluator::generateFeatures()
31 {
32     int offset = winSize.width + 1;
33     for( int x = 0; x < winSize.width; x++ )
34         for( int y = 0; y < winSize.height; y++ )
35             for( int w = 1; w <= winSize.width / 3; w++ )
36                 for( int h = 1; h <= winSize.height / 3; h++ )
37                     if ( (x+3*w <= winSize.width) && (y+3*h <= winSize.height) )
38                         features.push_back( Feature(offset, x, y, w, h ) );
39     numFeatures = (int)features.size();
40 }
41
42 CvLBPEvaluator::Feature::Feature()
43 {
44     rect = cvRect(0, 0, 0, 0);
45 }
46
47 CvLBPEvaluator::Feature::Feature( int offset, int x, int y, int _blockWidth, int _blockHeight )
48 {
49     Rect tr = rect = cvRect(x, y, _blockWidth, _blockHeight);
50     CV_SUM_OFFSETS( p[0], p[1], p[4], p[5], tr, offset )
51     tr.x += 2*rect.width;
52     CV_SUM_OFFSETS( p[2], p[3], p[6], p[7], tr, offset )
53     tr.y +=2*rect.height;
54     CV_SUM_OFFSETS( p[10], p[11], p[14], p[15], tr, offset )
55     tr.x -= 2*rect.width;
56     CV_SUM_OFFSETS( p[8], p[9], p[12], p[13], tr, offset )
57 }
58
59 void CvLBPEvaluator::Feature::write(FileStorage &fs) const
60 {
61     fs << CC_RECT << "[:" << rect.x << rect.y << rect.width << rect.height << "]";
62 }