1
#include "opencv2/core.hpp"
2
#include "opencv2/imgproc.hpp"
4
#include "HOGfeatures.h"
5
#include "cascadeclassifier.h"
10
CvHOGFeatureParams::CvHOGFeatureParams()
14
featSize = N_BINS * N_CELLS;
17
void CvHOGEvaluator::init(const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize)
19
CV_Assert( _maxSampleCount > 0);
20
int cols = (_winSize.width + 1) * (_winSize.height + 1);
21
for (int bin = 0; bin < N_BINS; bin++)
23
hist.push_back(Mat(_maxSampleCount, cols, CV_32FC1));
25
normSum.create( (int)_maxSampleCount, cols, CV_32FC1 );
26
CvFeatureEvaluator::init( _featureParams, _maxSampleCount, _winSize );
29
void CvHOGEvaluator::setImage(const Mat &img, uchar clsLabel, int idx)
31
CV_DbgAssert( !hist.empty());
32
CvFeatureEvaluator::setImage( img, clsLabel, idx );
33
vector<Mat> integralHist;
34
for (int bin = 0; bin < N_BINS; bin++)
36
integralHist.push_back( Mat(winSize.height + 1, winSize.width + 1, hist[bin].type(), hist[bin].ptr<float>((int)idx)) );
38
Mat integralNorm(winSize.height + 1, winSize.width + 1, normSum.type(), normSum.ptr<float>((int)idx));
39
integralHistogram(img, integralHist, integralNorm, (int)N_BINS);
42
//void CvHOGEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
44
// _writeFeatures( features, fs, featureMap );
47
void CvHOGEvaluator::writeFeatures( FileStorage &fs, const Mat& featureMap ) const
51
const Mat_<int>& featureMap_ = (const Mat_<int>&)featureMap;
52
fs << FEATURES << "[";
53
for ( int fi = 0; fi < featureMap.cols; fi++ )
54
if ( featureMap_(0, fi) >= 0 )
57
featIdx = fi / getFeatureSize();
58
componentIdx = fi % getFeatureSize();
59
features[featIdx].write( fs, componentIdx );
65
void CvHOGEvaluator::generateFeatures()
67
int offset = winSize.width + 1;
71
for (t = 8; t <= winSize.width/2; t+=8) //t = size of a cell. blocksize = 4*cellSize
73
blockStep = Size(4,4);
74
w = 2*t; //width of a block
75
h = 2*t; //height of a block
76
for (x = 0; x <= winSize.width - w; x += blockStep.width)
78
for (y = 0; y <= winSize.height - h; y += blockStep.height)
80
features.push_back(Feature(offset, x, y, t, t));
85
for (x = 0; x <= winSize.width - w; x += blockStep.width)
87
for (y = 0; y <= winSize.height - h; y += blockStep.height)
89
features.push_back(Feature(offset, x, y, t, 2*t));
94
for (x = 0; x <= winSize.width - w; x += blockStep.width)
96
for (y = 0; y <= winSize.height - h; y += blockStep.height)
98
features.push_back(Feature(offset, x, y, 2*t, t));
103
numFeatures = (int)features.size();
106
CvHOGEvaluator::Feature::Feature()
108
for (int i = 0; i < N_CELLS; i++)
110
rect[i] = Rect(0, 0, 0, 0);
114
CvHOGEvaluator::Feature::Feature( int offset, int x, int y, int cellW, int cellH )
116
rect[0] = Rect(x, y, cellW, cellH); //cell0
117
rect[1] = Rect(x+cellW, y, cellW, cellH); //cell1
118
rect[2] = Rect(x, y+cellH, cellW, cellH); //cell2
119
rect[3] = Rect(x+cellW, y+cellH, cellW, cellH); //cell3
121
for (int i = 0; i < N_CELLS; i++)
123
CV_SUM_OFFSETS(fastRect[i].p0, fastRect[i].p1, fastRect[i].p2, fastRect[i].p3, rect[i], offset);
127
void CvHOGEvaluator::Feature::write(FileStorage &fs) const
129
fs << CC_RECTS << "[";
130
for( int i = 0; i < N_CELLS; i++ )
132
fs << "[:" << rect[i].x << rect[i].y << rect[i].width << rect[i].height << "]";
137
//cell and bin idx writing
138
//void CvHOGEvaluator::Feature::write(FileStorage &fs, int varIdx) const
140
// int featComponent = varIdx % (N_CELLS * N_BINS);
141
// int cellIdx = featComponent / N_BINS;
142
// int binIdx = featComponent % N_BINS;
144
// fs << CC_RECTS << "[:" << rect[cellIdx].x << rect[cellIdx].y <<
145
// rect[cellIdx].width << rect[cellIdx].height << binIdx << "]";
148
//cell[0] and featComponent idx writing. By cell[0] it's possible to recover all block
149
//All block is nessesary for block normalization
150
void CvHOGEvaluator::Feature::write(FileStorage &fs, int featComponentIdx) const
152
fs << CC_RECT << "[:" << rect[0].x << rect[0].y <<
153
rect[0].width << rect[0].height << featComponentIdx << "]";
157
void CvHOGEvaluator::integralHistogram(const Mat &img, vector<Mat> &histogram, Mat &norm, int nbins) const
159
CV_Assert( img.type() == CV_8U || img.type() == CV_8UC3 );
162
Size gradSize(img.size());
163
Size histSize(histogram[0].size());
164
Mat grad(gradSize, CV_32F);
165
Mat qangle(gradSize, CV_8U);
167
AutoBuffer<int> mapbuf(gradSize.width + gradSize.height + 4);
168
int* xmap = (int*)mapbuf + 1;
169
int* ymap = xmap + gradSize.width + 2;
171
const int borderType = (int)BORDER_REPLICATE;
173
for( x = -1; x < gradSize.width + 1; x++ )
174
xmap[x] = borderInterpolate(x, gradSize.width, borderType);
175
for( y = -1; y < gradSize.height + 1; y++ )
176
ymap[y] = borderInterpolate(y, gradSize.height, borderType);
178
int width = gradSize.width;
179
AutoBuffer<float> _dbuf(width*4);
181
Mat Dx(1, width, CV_32F, dbuf);
182
Mat Dy(1, width, CV_32F, dbuf + width);
183
Mat Mag(1, width, CV_32F, dbuf + width*2);
184
Mat Angle(1, width, CV_32F, dbuf + width*3);
186
float angleScale = (float)(nbins/CV_PI);
188
for( y = 0; y < gradSize.height; y++ )
190
const uchar* currPtr = img.ptr(ymap[y]);
191
const uchar* prevPtr = img.ptr(ymap[y-1]);
192
const uchar* nextPtr = img.ptr(ymap[y+1]);
193
float* gradPtr = grad.ptr<float>(y);
194
uchar* qanglePtr = qangle.ptr(y);
196
for( x = 0; x < width; x++ )
198
dbuf[x] = (float)(currPtr[xmap[x+1]] - currPtr[xmap[x-1]]);
199
dbuf[width + x] = (float)(nextPtr[xmap[x]] - prevPtr[xmap[x]]);
201
cartToPolar( Dx, Dy, Mag, Angle, false );
202
for( x = 0; x < width; x++ )
204
float mag = dbuf[x+width*2];
205
float angle = dbuf[x+width*3];
206
angle = angle*angleScale - 0.5f;
207
int bidx = cvFloor(angle);
211
else if( bidx >= nbins )
214
qanglePtr[x] = (uchar)bidx;
218
integral(grad, norm, grad.depth());
222
const uchar* binsBuf;
224
int binsStep = (int)( qangle.step / sizeof(uchar) );
225
int histStep = (int)( histogram[0].step / sizeof(float) );
226
int magStep = (int)( grad.step / sizeof(float) );
227
for( binIdx = 0; binIdx < nbins; binIdx++ )
229
histBuf = histogram[binIdx].ptr<float>();
230
magBuf = grad.ptr<float>();
231
binsBuf = qangle.ptr();
233
memset( histBuf, 0, histSize.width * sizeof(histBuf[0]) );
234
histBuf += histStep + 1;
235
for( y = 0; y < qangle.rows; y++ )
239
for( x = 0; x < qangle.cols; x++ )
241
if( binsBuf[x] == binIdx )
243
histBuf[x] = histBuf[-histStep + x] + strSum;