Remove all using directives for STL namespace and members
[profile/ivi/opencv.git] / apps / traincascade / HOGfeatures.cpp
1 #include "opencv2/core/core.hpp"
2 #include "opencv2/core/internal.hpp"
3
4 #include "HOGfeatures.h"
5 #include "cascadeclassifier.h"
6
7 using namespace std;
8
9 CvHOGFeatureParams::CvHOGFeatureParams()
10 {
11     maxCatCount = 0;
12     name = HOGF_NAME;
13     featSize = N_BINS * N_CELLS;
14 }
15
16 void CvHOGEvaluator::init(const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize)
17 {
18     CV_Assert( _maxSampleCount > 0);
19     int cols = (_winSize.width + 1) * (_winSize.height + 1);
20     for (int bin = 0; bin < N_BINS; bin++)
21     {
22         hist.push_back(Mat(_maxSampleCount, cols, CV_32FC1));
23     }
24     normSum.create( (int)_maxSampleCount, cols, CV_32FC1 );
25     CvFeatureEvaluator::init( _featureParams, _maxSampleCount, _winSize );
26 }
27
28 void CvHOGEvaluator::setImage(const Mat &img, uchar clsLabel, int idx)
29 {
30     CV_DbgAssert( !hist.empty());
31     CvFeatureEvaluator::setImage( img, clsLabel, idx );
32     vector<Mat> integralHist;
33     for (int bin = 0; bin < N_BINS; bin++)
34     {
35         integralHist.push_back( Mat(winSize.height + 1, winSize.width + 1, hist[bin].type(), hist[bin].ptr<float>((int)idx)) );
36     }
37     Mat integralNorm(winSize.height + 1, winSize.width + 1, normSum.type(), normSum.ptr<float>((int)idx));
38     integralHistogram(img, integralHist, integralNorm, (int)N_BINS);
39 }
40
41 //void CvHOGEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
42 //{
43 //    _writeFeatures( features, fs, featureMap );
44 //}
45
46 void CvHOGEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
47 {
48     int featIdx;
49     int componentIdx;
50     const Mat_<int>& featureMap_ = (const Mat_<int>&)featureMap;
51     fs << FEATURES << "[";
52     for ( int fi = 0; fi < featureMap.cols; fi++ )
53         if ( featureMap_(0, fi) >= 0 )
54         {
55             fs << "{";
56             featIdx = fi / getFeatureSize();
57             componentIdx = fi % getFeatureSize();
58             features[featIdx].write( fs, componentIdx );
59             fs << "}";
60         }
61     fs << "]";
62 }
63
64 void CvHOGEvaluator::generateFeatures()
65 {
66     int offset = winSize.width + 1;
67     Size blockStep;
68     int x, y, t, w, h;
69
70     for (t = 8; t <= winSize.width/2; t+=8) //t = size of a cell. blocksize = 4*cellSize
71     {
72         blockStep = Size(4,4);
73         w = 2*t; //width of a block
74         h = 2*t; //height of a block
75         for (x = 0; x <= winSize.width - w; x += blockStep.width)
76         {
77             for (y = 0; y <= winSize.height - h; y += blockStep.height)
78             {
79                 features.push_back(Feature(offset, x, y, t, t));
80             }
81         }
82         w = 2*t;
83         h = 4*t;
84         for (x = 0; x <= winSize.width - w; x += blockStep.width)
85         {
86             for (y = 0; y <= winSize.height - h; y += blockStep.height)
87             {
88                 features.push_back(Feature(offset, x, y, t, 2*t));
89             }
90         }
91         w = 4*t;
92         h = 2*t;
93         for (x = 0; x <= winSize.width - w; x += blockStep.width)
94         {
95             for (y = 0; y <= winSize.height - h; y += blockStep.height)
96             {
97                 features.push_back(Feature(offset, x, y, 2*t, t));
98             }
99         }
100     }
101
102     numFeatures = (int)features.size();
103 }
104
105 CvHOGEvaluator::Feature::Feature()
106 {
107     for (int i = 0; i < N_CELLS; i++)
108     {
109         rect[i] = Rect(0, 0, 0, 0);
110     }
111 }
112
113 CvHOGEvaluator::Feature::Feature( int offset, int x, int y, int cellW, int cellH )
114 {
115     rect[0] = Rect(x, y, cellW, cellH); //cell0
116     rect[1] = Rect(x+cellW, y, cellW, cellH); //cell1
117     rect[2] = Rect(x, y+cellH, cellW, cellH); //cell2
118     rect[3] = Rect(x+cellW, y+cellH, cellW, cellH); //cell3
119
120     for (int i = 0; i < N_CELLS; i++)
121     {
122         CV_SUM_OFFSETS(fastRect[i].p0, fastRect[i].p1, fastRect[i].p2, fastRect[i].p3, rect[i], offset);
123     }
124 }
125
126 void CvHOGEvaluator::Feature::write(FileStorage &fs) const
127 {
128     fs << CC_RECTS << "[";
129     for( int i = 0; i < N_CELLS; i++ )
130     {
131         fs << "[:" << rect[i].x << rect[i].y << rect[i].width << rect[i].height << "]";
132     }
133     fs << "]";
134 }
135
136 //cell and bin idx writing
137 //void CvHOGEvaluator::Feature::write(FileStorage &fs, int varIdx) const
138 //{
139 //    int featComponent = varIdx % (N_CELLS * N_BINS);
140 //    int cellIdx = featComponent / N_BINS;
141 //    int binIdx = featComponent % N_BINS;
142 //
143 //    fs << CC_RECTS << "[:" << rect[cellIdx].x << rect[cellIdx].y <<
144 //        rect[cellIdx].width << rect[cellIdx].height << binIdx << "]";
145 //}
146
147 //cell[0] and featComponent idx writing. By cell[0] it's possible to recover all block
148 //All block is nessesary for block normalization
149 void CvHOGEvaluator::Feature::write(FileStorage &fs, int featComponentIdx) const
150 {
151     fs << CC_RECT << "[:" << rect[0].x << rect[0].y <<
152         rect[0].width << rect[0].height << featComponentIdx << "]";
153 }
154
155
156 void CvHOGEvaluator::integralHistogram(const Mat &img, vector<Mat> &histogram, Mat &norm, int nbins) const
157 {
158     CV_Assert( img.type() == CV_8U || img.type() == CV_8UC3 );
159     int x, y, binIdx;
160
161     Size gradSize(img.size());
162     Size histSize(histogram[0].size());
163     Mat grad(gradSize, CV_32F);
164     Mat qangle(gradSize, CV_8U);
165
166     AutoBuffer<int> mapbuf(gradSize.width + gradSize.height + 4);
167     int* xmap = (int*)mapbuf + 1;
168     int* ymap = xmap + gradSize.width + 2;
169
170     const int borderType = (int)BORDER_REPLICATE;
171
172     for( x = -1; x < gradSize.width + 1; x++ )
173         xmap[x] = borderInterpolate(x, gradSize.width, borderType);
174     for( y = -1; y < gradSize.height + 1; y++ )
175         ymap[y] = borderInterpolate(y, gradSize.height, borderType);
176
177     int width = gradSize.width;
178     AutoBuffer<float> _dbuf(width*4);
179     float* dbuf = _dbuf;
180     Mat Dx(1, width, CV_32F, dbuf);
181     Mat Dy(1, width, CV_32F, dbuf + width);
182     Mat Mag(1, width, CV_32F, dbuf + width*2);
183     Mat Angle(1, width, CV_32F, dbuf + width*3);
184
185     float angleScale = (float)(nbins/CV_PI);
186
187     for( y = 0; y < gradSize.height; y++ )
188     {
189         const uchar* currPtr = img.data + img.step*ymap[y];
190         const uchar* prevPtr = img.data + img.step*ymap[y-1];
191         const uchar* nextPtr = img.data + img.step*ymap[y+1];
192         float* gradPtr = (float*)grad.ptr(y);
193         uchar* qanglePtr = (uchar*)qangle.ptr(y);
194
195         for( x = 0; x < width; x++ )
196         {
197             dbuf[x] = (float)(currPtr[xmap[x+1]] - currPtr[xmap[x-1]]);
198             dbuf[width + x] = (float)(nextPtr[xmap[x]] - prevPtr[xmap[x]]);
199         }
200         cartToPolar( Dx, Dy, Mag, Angle, false );
201         for( x = 0; x < width; x++ )
202         {
203             float mag = dbuf[x+width*2];
204             float angle = dbuf[x+width*3];
205             angle = angle*angleScale - 0.5f;
206             int bidx = cvFloor(angle);
207             angle -= bidx;
208             if( bidx < 0 )
209                 bidx += nbins;
210             else if( bidx >= nbins )
211                 bidx -= nbins;
212
213             qanglePtr[x] = (uchar)bidx;
214             gradPtr[x] = mag;
215         }
216     }
217     integral(grad, norm, grad.depth());
218
219     float* histBuf;
220     const float* magBuf;
221     const uchar* binsBuf;
222
223     int binsStep = (int)( qangle.step / sizeof(uchar) );
224     int histStep = (int)( histogram[0].step / sizeof(float) );
225     int magStep = (int)( grad.step / sizeof(float) );
226     for( binIdx = 0; binIdx < nbins; binIdx++ )
227     {
228         histBuf = (float*)histogram[binIdx].data;
229         magBuf = (const float*)grad.data;
230         binsBuf = (const uchar*)qangle.data;
231
232         memset( histBuf, 0, histSize.width * sizeof(histBuf[0]) );
233         histBuf += histStep + 1;
234         for( y = 0; y < qangle.rows; y++ )
235         {
236             histBuf[-1] = 0.f;
237             float strSum = 0.f;
238             for( x = 0; x < qangle.cols; x++ )
239             {
240                 if( binsBuf[x] == binIdx )
241                     strSum += magBuf[x];
242                 histBuf[x] = histBuf[-histStep + x] + strSum;
243             }
244             histBuf += histStep;
245             binsBuf += binsStep;
246             magBuf += magStep;
247         }
248     }
249 }