-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_hog.cc
More file actions
135 lines (121 loc) · 4.34 KB
/
visualize_hog.cc
File metadata and controls
135 lines (121 loc) · 4.34 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "visualize_hog.h"
#include "constant.h"
void VisualizeHog::calculate_visual_weight(double *feat, std::vector<Mat>& weight) {
for (unsigned y = 0; y < kBlockHeight; ++y) {
for (unsigned x = 0; x < kBlockWidth; ++x) {
const unsigned base_idx
= y * kBlockWidth * kBlockDim + x * kBlockDim;
for (unsigned j = 0; j < kBlockY; ++j) {
for (unsigned i = 0; i < kBlockX; ++i) {
for (unsigned k = 0; k < kCellBin; ++k) {
const unsigned idx
= base_idx + j * kBlockX * kCellBin + i * kCellBin + k;
weight[y + j][x + i][k] += feat[idx];
}
}
}
}
}
}
void VisualizeHog::calculate_visual_weight(const std::vector<double>& feat,
std::vector<Mat>& weight) {
for (unsigned y = 0; y < kBlockHeight; ++y) {
for (unsigned x = 0; x < kBlockWidth; ++x) {
const unsigned base_idx
= y * kBlockWidth * kBlockDim + x * kBlockDim;
for (unsigned j = 0; j < kBlockY; ++j) {
for (unsigned i = 0; i < kBlockX; ++i) {
for (unsigned k = 0; k < kCellBin; ++k) {
const unsigned idx
= base_idx + j * kBlockX * kCellBin + i * kCellBin + k;
weight[y + j][x + i][k] += feat[idx];
}
}
}
}
}
}
cv::Mat VisualizeHog::generate_mat(double *feat) {
assert(false);
// calculate weight
std::vector<Mat> weight(8, Mat(8, Vec(9, 0)));
calculate_visual_weight(feat, weight);
// make glyph
cv::Mat base(20, 20, CV_8UC1, cv::Scalar(0, 0, 0));
for (int i = 0; i < 20; ++i)
base.at<uchar>(i, 9) = base.at<uchar>(i, 10) = 1;
// output destination
cv::Mat od(20 * kCellWidth,
20 * kCellHeight,
CV_32FC1,
cv::Scalar(0, 0, 0));
// visualize
for (unsigned y = 0; y < 8; ++y) {
for (unsigned x = 0; x < 8; ++x) {
for (int k = 0; k < 9; ++k) {
float angle = -20.0 * k;
cv::Point2f center(base.rows * 0.5, base.cols * 0.5);
const cv::Mat affine_matrix =
cv::getRotationMatrix2D(center, angle, 1.0f);
cv::Mat dst;
cv::warpAffine(base, dst, affine_matrix, base.size());
// draw
for (unsigned i = 0; i < 20; ++i) {
for (unsigned j = 0; j < 20; ++j) {
od.at<float>(y * 20 + i, x * 20 + j)
+= dst.at<uchar>(i,j) * weight[y][x][k];
}
}
}
}
}
return od;
}
cv::Mat VisualizeHog::generate_mat(const std::vector<double>& feat) {
SMat visual_weight(kBlockHeight, Mat(kBlockWidth, Vec(kOrientation)));
// calc weight
for (unsigned y = 0; y < kBlockHeight; ++y)
for (unsigned x = 0; x < kBlockWidth; ++x)
for (unsigned j = 0; j < kBlockY; ++j)
for (unsigned i = 0; i < kBlockX; ++i)
for (unsigned k = 0; k < kOrientation; ++k)
visual_weight[y][x][k]
+= feat[ y * kBlockWidth * kBlockDim +
x * kBlockDim +
j * kBlockX * kOrientation +
i * kOrientation +
k ];
// Output destination
cv::Mat od = cv::Mat::zeros(kGlyphSize * kBlockHeight,
kGlyphSize * kBlockWidth,
CV_32FC1);
// Make base-glyph
cv::Mat base = cv::Mat::zeros(kGlyphSize,
kGlyphSize,
CV_32FC1);
for (int i = 0; i < kGlyphSize; ++i)
base.at<float>(i, 9) = base.at<float>(i, 10) = 1;
for (unsigned y = 0; y < kBlockHeight; ++y) {
for (unsigned x = 0; x < kBlockWidth; ++x) {
for (int k = 0; k < kOrientation; ++k) {
float angle = -20.0 * k;
cv::Point2f center(base.rows * 0.5, base.cols * 0.5);
const cv::Mat affine_matrix =
cv::getRotationMatrix2D(center, angle, 1.0f);
cv::Mat dst;
cv::warpAffine(base, dst, affine_matrix, base.size());
// draw
for (unsigned i = 0; i < kGlyphSize; ++i) {
for (unsigned j = 0; j < kGlyphSize; ++j) {
od.at<float>(y * kGlyphSize + i, x * kGlyphSize + j)
+= dst.at<float>(i,j) * visual_weight[y][x][k];
}
}
}
// cv::namedWindow("test", CV_WINDOW_AUTOSIZE | CV_WINDOW_FREERATIO);
// cv::imshow("test", od);
// cv::waitKey(0);
}
}
return od;
}