-
Notifications
You must be signed in to change notification settings - Fork 1
/
hog.cpp
220 lines (166 loc) · 6.02 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
*http://blog.csdn.net/zhazhiqiang/article/details/21047207?utm_source=tuicool
*http://www.juergenwiki.de/work/wiki/doku.php?id=public:hog_descriptor_computation_and_visualization
*/
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
//#include <opencv2/objdetect/objdetect.hpp>
using namespace cv;
using namespace std;
Mat get_hogdescriptor_visual_image(Mat& origImg, vector<float>& descriptorValues, Size winSize, Size cellSize, int scaleFactor, double viz_factor) {
Mat visual_image;
resize(origImg, visual_image, Size(origImg.cols*scaleFactor, origImg.rows*scaleFactor));
int gradientBinSize = 9;
// dividing 180° into 9 bins, how large (in rad) is one bin?
float radRangeForOneBin = 3.14 / (float)gradientBinSize;
// prepare data structure: 9 orientation / gradient strenghts for each cell
int cells_in_x_dir = winSize.width / cellSize.width;
int cells_in_y_dir = winSize.height / cellSize.height;
int totalnrofcells = cells_in_x_dir * cells_in_y_dir;
float ***gradientStrengths = new float**[cells_in_y_dir];
int **cellUpdateCounter = new int*[cells_in_y_dir];
for (int y = 0; y < cells_in_y_dir; y++)
{
gradientStrengths[y] = new float*[cells_in_x_dir];
cellUpdateCounter[y] = new int[cells_in_x_dir];
for (int x = 0; x < cells_in_x_dir; x++)
{
gradientStrengths[y][x] = new float[gradientBinSize];
cellUpdateCounter[y][x] = 0;
for (int bin = 0; bin < gradientBinSize; bin++)
gradientStrengths[y][x][bin] = 0.0;
}
}
// nr of blocks = nr of cells - 1
// since there is a new block on each cell (overlapping blocks!) but the last one
int blocks_in_x_dir = cells_in_x_dir - 1;
int blocks_in_y_dir = cells_in_y_dir - 1;
// compute gradient strengths per cell
int descriptorDataIdx = 0;
int cellx = 0;
int celly = 0;
for (int blockx = 0; blockx < blocks_in_x_dir; blockx++)
{
for (int blocky = 0; blocky < blocks_in_y_dir; blocky++)
{
// 4 cells per block ...
for (int cellNr = 0; cellNr < 4; cellNr++)
{
// compute corresponding cell nr
int cellx = blockx;
int celly = blocky;
if (cellNr == 1) celly++;
if (cellNr == 2) cellx++;
if (cellNr == 3)
{
cellx++;
celly++;
}
for (int bin = 0; bin < gradientBinSize; bin++)
{
float gradientStrength = descriptorValues[descriptorDataIdx];
descriptorDataIdx++;
gradientStrengths[celly][cellx][bin] += gradientStrength;
} // for (all bins)
// note: overlapping blocks lead to multiple updates of this sum!
// we therefore keep track how often a cell was updated,
// to compute average gradient strengths
cellUpdateCounter[celly][cellx]++;
} // for (all cells)
} // for (all block x pos)
} // for (all block y pos)
// compute average gradient strengths
for (int celly = 0; celly < cells_in_y_dir; celly++)
{
for (int cellx = 0; cellx < cells_in_x_dir; cellx++)
{
float NrUpdatesForThisCell = (float)cellUpdateCounter[celly][cellx];
// compute average gradient strenghts for each gradient bin direction
for (int bin = 0; bin < gradientBinSize; bin++)
{
gradientStrengths[celly][cellx][bin] /= NrUpdatesForThisCell;
}
}
}
cout << "descriptorDataIdx = " << descriptorDataIdx << endl;
// draw cells
for (int celly = 0; celly < cells_in_y_dir; celly++)
{
for (int cellx = 0; cellx < cells_in_x_dir; cellx++)
{
int drawX = cellx * cellSize.width;
int drawY = celly * cellSize.height;
int mx = drawX + cellSize.width / 2;
int my = drawY + cellSize.height / 2;
rectangle(visual_image,
Point(drawX*scaleFactor, drawY*scaleFactor),
Point((drawX + cellSize.width)*scaleFactor,
(drawY + cellSize.height)*scaleFactor),
CV_RGB(100, 100, 100),
1);
// draw in each cell all 9 gradient strengths
for (int bin = 0; bin < gradientBinSize; bin++)
{
float currentGradStrength = gradientStrengths[celly][cellx][bin];
// no line to draw?
if (currentGradStrength == 0)
continue;
float currRad = bin * radRangeForOneBin + radRangeForOneBin / 2;
float dirVecX = cos(currRad);
float dirVecY = sin(currRad);
float maxVecLen = cellSize.width / 2;
float scale = viz_factor; // just a visual_imagealization scale,
// to see the lines better
// compute line coordinates
float x1 = mx - dirVecX * currentGradStrength * maxVecLen * scale;
float y1 = my - dirVecY * currentGradStrength * maxVecLen * scale;
float x2 = mx + dirVecX * currentGradStrength * maxVecLen * scale;
float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale;
// draw gradient visual_imagealization
line(visual_image,
Point(x1*scaleFactor, y1*scaleFactor),
Point(x2*scaleFactor, y2*scaleFactor),
CV_RGB(0, 0, 255),
1);
} // for (all bins)
} // for (cellx)
} // for (celly)
// don't forget to free memory allocated by helper data structures!
for (int y = 0; y < cells_in_y_dir; y++)
{
for (int x = 0; x < cells_in_x_dir; x++)
{
delete[] gradientStrengths[y][x];
}
delete[] gradientStrengths[y];
delete[] cellUpdateCounter[y];
}
delete[] gradientStrengths;
delete[] cellUpdateCounter;
return visual_image;
}
int main(int argc, char **argv) {
cv::Mat img = cv::imread("hogData/0.png");
// cv::resize(img, img, cv::Size(64, 128));
cv::Mat gray;
cv::cvtColor(img, gray, CV_RGB2GRAY);
cv::HOGDescriptor hog;
std::vector<float> desc;
std::vector<cv::Point> loc;
hog.compute(gray, desc, cv::Size(0, 0), cv::Size(0, 0), loc);
std::cout << "hog descriptor size is " << hog.getDescriptorSize() << std::endl;
std::cout << "found " << desc.size() << " descriptor values" << std::endl;
std::cout << "Nr of locations specified " << loc.size() << std::endl;
cv::Mat vis = get_hogdescriptor_visual_image(img, desc, cv::Size(64, 128), cv::Size(8, 8), 5, 3);
//cv::Mat vis = get_hogdescriptor_visual_image(img, desc, cv::Size(img.rows, img.cols), cv::Size(8, 8), 5, 4);
cv::Mat lap;
cv::Laplacian(img, lap, img.depth());
std::ofstream out("lap");
out << lap;
out.close();
//cv::imshow("ori", img);
cv::imshow("vis", vis);
cv::waitKey(0);
return 0;
}