forked from beetleskin/hrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HoG.cpp
executable file
·86 lines (63 loc) · 1.92 KB
/
HoG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "HoG.hpp"
#include <vector>
#include <iostream>
HoG::HoG() {
bins = 9;
binsize = (3.14159265f * 80.0f) / float(bins);;
g_w = 5;
Gauss = cvCreateMat( g_w, g_w, CV_32FC1 );
double a = -(g_w - 1) / 2.0;
double sigma2 = 2 * (0.5 * g_w) * (0.5 * g_w);
double count = 0;
for (int x = 0; x < g_w; ++x) {
for (int y = 0; y < g_w; ++y) {
double tmp = exp(-( (a + x) * (a + x) + (a + y) * (a + y) ) / sigma2);
count += tmp;
cvSet2D( Gauss, x, y, cvScalar(tmp) );
}
}
cvConvertScale( Gauss, Gauss, 1.0 / count);
ptGauss = new float[g_w * g_w];
int i = 0;
for (int y = 0; y < g_w; ++y)
for (int x = 0; x < g_w; ++x)
ptGauss[i++] = (float)cvmGet( Gauss, x, y );
}
void HoG::extractOBin(Mat &Iorient, Mat &Imagn, vector<Mat> &out) {
double *desc = new double[bins];
// reset output image (border=0) and get pointers
uchar **ptOut = new uchar*[bins];
uchar **ptOut_row = new uchar*[bins];
for (int k = 0; k < bins; ++k) {
ptOut[k] = out[k].data;
}
// get pointers to orientation, magnitude
int step = Iorient.step[0];
uchar *ptOrient = Iorient.data;
uchar *ptOrient_row;
uchar *ptMagn = Imagn.data;
uchar *ptMagn_row;
int off_w = int(g_w / 2.0);
for (int l = 0; l < bins; ++l)
ptOut[l] += off_w * step;
for (int y = 0; y < Iorient.rows - g_w; y++, ptMagn += step, ptOrient += step) {
// Get row pointers
ptOrient_row = &ptOrient[0];
ptMagn_row = &ptMagn[0];
for (int l = 0; l < bins; ++l)
ptOut_row[l] = &ptOut[l][0] + off_w;
for (int x = 0; x < Iorient.cols - g_w; ++x, ++ptOrient_row, ++ptMagn_row) {
calcHoGBin( ptOrient_row, ptMagn_row, step, desc );
for (int l = 0; l < bins; ++l) {
*ptOut_row[l] = (uchar)desc[l];
++ptOut_row[l];
}
}
// update pointer
for (int l = 0; l < bins; ++l)
ptOut[l] += step;
}
delete[] desc;
delete[] ptOut;
delete[] ptOut_row;
}