-
Notifications
You must be signed in to change notification settings - Fork 0
/
hog.cc
87 lines (85 loc) · 3.05 KB
/
hog.cc
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
87
#include "hog.h"
void Hog::make_histgram() {
// make histogram_ by cell
for (unsigned y = 0; y < kImageHeight; ++y) {
for (unsigned x = 0; x < kImageWidth; ++x) {
double x_grad = 0, y_grad = 0;
// x
if (x == 0) {
x_grad = source_.at<uchar>(y, 1) - source_.at<uchar>(y, 0);
} else if (x == kImageWidth - 1) {
x_grad = source_.at<uchar>(y, kImageWidth - 1)
- source_.at<uchar>(y, kImageWidth - 2);
} else {
x_grad = source_.at<uchar>(y, x + 1) - source_.at<uchar>(y, x);
}
// y
if (y == 0) {
y_grad = source_.at<uchar>(1, x) - source_.at<uchar>(0, x);
} else if (y == kImageHeight - 1) {
y_grad = source_.at<uchar>(kImageHeight - 1, x)
- source_.at<uchar>(kImageHeight - 2, x);
} else {
y_grad = source_.at<uchar>(y + 1, x) - source_.at<uchar>(y - 1, x);
}
// params
double magnitude = std::sqrt(x_grad * x_grad + y_grad * y_grad);
double gradient = std::atan2(y_grad, x_grad) * 180 / CV_PI;
double test = gradient;
if (gradient < 0.0) gradient += 360.0;
if (180.0 < gradient) gradient -= 180.0;
gradient = gradient / kAngle;
const unsigned idx =
(y / kCellSize) * (kImageWidth / kCellSize) * kOrientation +
(x / kCellSize) * kOrientation + (static_cast<int>(gradient - 1e-10));
// store
if ((idx >= kHistgram) || (static_cast<int>(gradient-1e-10) == 9)) {
printf("err : %d >= %d\n", idx, kHistgram);
printf("gradient %f %f\n", test, gradient);
assert(false);
}
histogram_[idx] = magnitude;
}
}
}
void Hog::make_feature() {
int fcount = 0;
// block loop
for (unsigned y = 0; y < kImageHeight / kCellSize - kBlockSize + 1; ++y) {
for (unsigned x = 0; x < kImageWidth / kCellSize - kBlockSize + 1; ++x) {
double sum_magnitude = 0.0;
// cell loop
for (unsigned j = 0; j < kBlockSize; ++j) {
for (unsigned i = 0; i < kBlockSize; ++i) {
for (unsigned k = 0; k < kOrientation; ++k) {
const unsigned idx = (y + j) * (kImageWidth / kCellSize) * kOrientation +
(x + i) * kOrientation + k;
if (idx >= kHistgram) {
printf("err : %d\n", idx);
assert(idx < kHistgram);
}
double tmp = histogram_[idx];
sum_magnitude += tmp * tmp;
}
}
}
sum_magnitude = 1.0 / std::sqrt(sum_magnitude + 1.0);
// cell loop
for (unsigned j = 0; j < kBlockSize; ++j) {
for (unsigned i = 0; i < kBlockSize; ++i) {
for (unsigned k = 0; k < kOrientation; ++k) {
const unsigned idx = (y + j) * (kImageWidth / kCellSize) * kOrientation +
(x + i) * kOrientation +
k;
if (idx >= kHistgram) {
printf("err : %d\n", idx);
assert(idx < kHistgram);
}
destination_[fcount] = histogram_[idx] * sum_magnitude;
++fcount;
}
}
}
}
}
}