-
Notifications
You must be signed in to change notification settings - Fork 0
/
detector_harrisa.cpp
258 lines (223 loc) · 8.08 KB
/
detector_harrisa.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "detector_harrisa.hpp"
detector_harrisa::detector_harrisa()
: _window_size (5)
, _min_intensiti (10)
, _bobl_raduis (5)
, _k (0.05)
, _dimensionality (256)
, _size_area_point (30)
, _area_for_average (5)
{}
detector_harrisa::detector_harrisa(size_type window_size, int min_intensiti, size_type bobl_raduis, double k,
size_type dimensionality, size_type size_area_point, size_type area_for_average)
: _window_size ( window_size )
, _min_intensiti ( min_intensiti )
, _bobl_raduis ( bobl_raduis )
, _k ( k )
, _dimensionality ( dimensionality )
, _size_area_point ( size_area_point )
, _area_for_average ( area_for_average )
{}
size_type detector_harrisa::window_size() const
{
return _window_size;
}
double detector_harrisa::k() const
{
return _k;
}
size_type detector_harrisa::bobl_raduis() const
{
return _bobl_raduis;
}
int detector_harrisa::min_intensiti() const
{
return _min_intensiti;
}
size_type detector_harrisa::dimensionality() const
{
return _dimensionality;
}
size_type detector_harrisa::size_area_point() const
{
return _size_area_point;
}
size_type detector_harrisa::area_for_average() const
{
return _area_for_average;
}
void detector_harrisa::normalize(matrix_type& image)
{
for(size_type h = 0; h < image.rows; ++h)
for(size_type w = 0; w < image.cols; ++w)
image(h, w) /= 255;
}
matrix_type detector_harrisa::gradient_x(const matrix_type &image)
{
size_type kernel_size = 3;
vector<vector<int>> kernel_x = { {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1} };
matrix_type I_x(image.rows, image.cols);
for(size_type h = 1; h < image.rows - 1; ++h)
for(size_type w = 1; w < image.cols - 1; ++w)
{
double sum = 0;
for(size_type i = 0; i < kernel_size; ++i)
for(size_type j = 0; j < kernel_size; ++j)
{
double current_pixel = image( h-1 + i, w-1 + j);
sum += current_pixel * kernel_x[i][j];
}
I_x( h, w ) = sum;
}
return I_x;
}
matrix_type detector_harrisa::gradient_y(const matrix_type &image)
{
size_type kernel_size = 3;
vector<vector<int>> kernel_x = { {-1, -2, -1}, {0, 0, 0}, {1, 2, 1} };
matrix_type I_y(image.rows, image.cols);
for(size_type h = 1; h < image.rows - 1; ++h)
for(size_type w = 1; w < image.cols - 1; ++w)
{
double sum = 0;
for(size_type i = 0; i < kernel_size; ++i)
for(size_type j = 0; j < kernel_size; ++j)
{
double current_pixel = image( h-1 + i, w-1 + j);
sum += current_pixel * kernel_x[i][j];
}
I_y( h, w ) = sum;
}
return I_y;
}
std::vector<key_point> detector_harrisa::search_key_point(const matrix_type &image)
{
std::vector<key_point> key_points;
bool add = false;
size_type offset = window_size()/2;//?
matrix_type normalize_image;
image.copyTo(normalize_image);
normalize(normalize_image);
Mat_<double> I_x = gradient_x(normalize_image);
Mat_<double> I_y = gradient_y(normalize_image);
Mat_<double> I_xx;
Mat_<double> I_xy;
Mat_<double> I_yy;
pow(I_x, 2.0, I_xx);
pow(I_y, 2.0, I_yy);
multiply(I_x, I_y, I_xy);
for (size_t h = 1 + offset; h < normalize_image.rows - offset - 1; ++h)
for (size_t w = 1 + offset; w < normalize_image.cols - offset - 1; ++w)
{
double sum_xx = 0;
double sum_xy = 0;
double sum_yy = 0;
for (size_t i = 0; i < window_size(); ++i)
for(size_t j = 0; j < window_size(); ++j)
{
sum_xx += I_xx(h - offset + i, w - offset + j);
sum_xy += I_xy(h - offset + i, w - offset + j);
sum_yy += I_yy(h - offset + i, w - offset + j);
}
double det = sum_xx * sum_yy - sum_xy * sum_xy;
double trace = sum_xx + sum_yy;
double intensty = det - k() * trace * trace ;
if ( intensty > min_intensiti() )
{
if (key_points.size() == 0)
{
key_points.push_back( key_point( w, h, intensty ) );
}
else
{
for(auto current = key_points.begin(); current != key_points.end(); ++current)
{
int delta_x = w - current->coordinates.x;
int delta_y = h - current->coordinates.y;
int rad = pow(delta_x, 2.0) + pow( delta_y, 2.0 );
if( rad <= pow( bobl_raduis(), 2.0 ) )
{
add = false;
if ( intensty > current->intensity )
{
current->coordinates.x = w;
current->coordinates.y = h;
current->intensity = intensty;
}
break;
}
else
add = true;
}
if(add)
key_points.push_back( key_point( w, h, intensty ) );
}
}
}
return key_points;
}
double detector_harrisa::average_intensity(int x, int y, const matrix_type & pixel_image) const
{
size_type offset = area_for_average()/2;
int sum = 0;
int count = 0;
for (int h = 0; h < area_for_average(); ++h)
for (int w = 0; w < area_for_average(); ++w)
{
sum += pixel_image( y - offset + h, x - offset + w);
++count;
}
return double(sum)/count;
}
vector<descriptor_type> detector_harrisa::brief(const matrix_type &pixel_image,
const std::vector<key_point> &key_points)
{
srand(time(0));
vector<descriptor_type> descriptors;
for (auto current_key = key_points.begin(); current_key != key_points.end(); ++ current_key)
{
vector<bool> vec;
for (size_type i = 0; i < _dimensionality; ++i)
{
size_type offset = size_area_point() / 2;
int q1 = current_key->coordinates.x + offset;
int q2 = current_key->coordinates.x - offset;
int q3 = current_key->coordinates.y + offset;
int q4 = current_key->coordinates.y - offset;
while ( q1 >= pixel_image.cols || q2 < 0 || q3 >= pixel_image.rows || q4 < 0 )
{
offset /= 2;
q1 = current_key->coordinates.x + offset;
q2 = current_key->coordinates.x - offset;
q3 = current_key->coordinates.y + offset;
q4 = current_key->coordinates.y - offset;
}
int delta_x1 = 0;
int delta_x2 = 0;
int delta_y1 = 0;
int delta_y2 = 0;
size_type interval = offset - area_for_average()/2;
if ( interval > 0 )
{
delta_x1 = -interval + rand() % ( 2 * interval );
delta_x2 = -interval + rand() % ( 2 * interval );
delta_y1 = -interval + rand() % ( 2 * interval );
delta_y2 = -interval + rand() % ( 2 * interval );
}
double I1 = average_intensity(current_key->coordinates.x + delta_x1,
current_key->coordinates.y + delta_y1, pixel_image);
double I2 = average_intensity(current_key->coordinates.x + delta_x2,
current_key->coordinates.y + delta_y2, pixel_image);
if (I1 < I2)
vec.push_back(1);
else
vec.push_back(0);
}
descriptors.push_back( vec );
}
return descriptors;
}
vector<descriptor_type> detector_harrisa::search_descriptor(const matrix_type &image)
{
return brief( image, search_key_point(image) );
}