1 #include "opencv2/core/core.hpp"
2 #include "opencv2/core/internal.hpp"
4 #include "HOGfeatures.h"
5 #include "cascadeclassifier.h"
9 CvHOGFeatureParams::CvHOGFeatureParams()
13 featSize = N_BINS * N_CELLS;
16 void CvHOGEvaluator::init(const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize)
18 CV_Assert( _maxSampleCount > 0);
19 int cols = (_winSize.width + 1) * (_winSize.height + 1);
20 for (int bin = 0; bin < N_BINS; bin++)
22 hist.push_back(Mat(_maxSampleCount, cols, CV_32FC1));
24 normSum.create( (int)_maxSampleCount, cols, CV_32FC1 );
25 CvFeatureEvaluator::init( _featureParams, _maxSampleCount, _winSize );
28 void CvHOGEvaluator::setImage(const Mat &img, uchar clsLabel, int idx)
30 CV_DbgAssert( !hist.empty());
31 CvFeatureEvaluator::setImage( img, clsLabel, idx );
32 vector<Mat> integralHist;
33 for (int bin = 0; bin < N_BINS; bin++)
35 integralHist.push_back( Mat(winSize.height + 1, winSize.width + 1, hist[bin].type(), hist[bin].ptr<float>((int)idx)) );
37 Mat integralNorm(winSize.height + 1, winSize.width + 1, normSum.type(), normSum.ptr<float>((int)idx));
38 integralHistogram(img, integralHist, integralNorm, (int)N_BINS);
41 //void CvHOGEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
43 // _writeFeatures( features, fs, featureMap );
46 void CvHOGEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
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 )
56 featIdx = fi / getFeatureSize();
57 componentIdx = fi % getFeatureSize();
58 features[featIdx].write( fs, componentIdx );
64 void CvHOGEvaluator::generateFeatures()
66 int offset = winSize.width + 1;
70 for (t = 8; t <= winSize.width/2; t+=8) //t = size of a cell. blocksize = 4*cellSize
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)
77 for (y = 0; y <= winSize.height - h; y += blockStep.height)
79 features.push_back(Feature(offset, x, y, t, t));
84 for (x = 0; x <= winSize.width - w; x += blockStep.width)
86 for (y = 0; y <= winSize.height - h; y += blockStep.height)
88 features.push_back(Feature(offset, x, y, t, 2*t));
93 for (x = 0; x <= winSize.width - w; x += blockStep.width)
95 for (y = 0; y <= winSize.height - h; y += blockStep.height)
97 features.push_back(Feature(offset, x, y, 2*t, t));
102 numFeatures = (int)features.size();
105 CvHOGEvaluator::Feature::Feature()
107 for (int i = 0; i < N_CELLS; i++)
109 rect[i] = Rect(0, 0, 0, 0);
113 CvHOGEvaluator::Feature::Feature( int offset, int x, int y, int cellW, int cellH )
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
120 for (int i = 0; i < N_CELLS; i++)
122 CV_SUM_OFFSETS(fastRect[i].p0, fastRect[i].p1, fastRect[i].p2, fastRect[i].p3, rect[i], offset);
126 void CvHOGEvaluator::Feature::write(FileStorage &fs) const
128 fs << CC_RECTS << "[";
129 for( int i = 0; i < N_CELLS; i++ )
131 fs << "[:" << rect[i].x << rect[i].y << rect[i].width << rect[i].height << "]";
136 //cell and bin idx writing
137 //void CvHOGEvaluator::Feature::write(FileStorage &fs, int varIdx) const
139 // int featComponent = varIdx % (N_CELLS * N_BINS);
140 // int cellIdx = featComponent / N_BINS;
141 // int binIdx = featComponent % N_BINS;
143 // fs << CC_RECTS << "[:" << rect[cellIdx].x << rect[cellIdx].y <<
144 // rect[cellIdx].width << rect[cellIdx].height << binIdx << "]";
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
151 fs << CC_RECT << "[:" << rect[0].x << rect[0].y <<
152 rect[0].width << rect[0].height << featComponentIdx << "]";
156 void CvHOGEvaluator::integralHistogram(const Mat &img, vector<Mat> &histogram, Mat &norm, int nbins) const
158 CV_Assert( img.type() == CV_8U || img.type() == CV_8UC3 );
161 Size gradSize(img.size());
162 Size histSize(histogram[0].size());
163 Mat grad(gradSize, CV_32F);
164 Mat qangle(gradSize, CV_8U);
166 AutoBuffer<int> mapbuf(gradSize.width + gradSize.height + 4);
167 int* xmap = (int*)mapbuf + 1;
168 int* ymap = xmap + gradSize.width + 2;
170 const int borderType = (int)BORDER_REPLICATE;
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);
177 int width = gradSize.width;
178 AutoBuffer<float> _dbuf(width*4);
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);
185 float angleScale = (float)(nbins/CV_PI);
187 for( y = 0; y < gradSize.height; y++ )
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);
195 for( x = 0; x < width; x++ )
197 dbuf[x] = (float)(currPtr[xmap[x+1]] - currPtr[xmap[x-1]]);
198 dbuf[width + x] = (float)(nextPtr[xmap[x]] - prevPtr[xmap[x]]);
200 cartToPolar( Dx, Dy, Mag, Angle, false );
201 for( x = 0; x < width; x++ )
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);
210 else if( bidx >= nbins )
213 qanglePtr[x] = (uchar)bidx;
217 integral(grad, norm, grad.depth());
221 const uchar* binsBuf;
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++ )
228 histBuf = (float*)histogram[binIdx].data;
229 magBuf = (const float*)grad.data;
230 binsBuf = (const uchar*)qangle.data;
232 memset( histBuf, 0, histSize.width * sizeof(histBuf[0]) );
233 histBuf += histStep + 1;
234 for( y = 0; y < qangle.rows; y++ )
238 for( x = 0; x < qangle.cols; x++ )
240 if( binsBuf[x] == binIdx )
242 histBuf[x] = histBuf[-histStep + x] + strSum;