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
259
260
261
262
263
264
265
266
267
268
mod contrast_factor;
mod derivatives;
mod descriptors;
mod detector_response;
mod evolution;
mod fed_tau;
mod image;
mod nonlinear_diffusion;
mod scale_space_extrema;
use crate::image::{gaussian_blur, GrayFloatImage};
use ::image::{DynamicImage, GenericImageView, ImageResult};
use bitarray::BitArray;
use cv_core::nalgebra::Point2;
use cv_core::ImagePoint;
use evolution::*;
use log::*;
use nonlinear_diffusion::pm_g2;
use std::path::Path;
#[derive(Debug, Clone, Copy)]
pub struct KeyPoint {
pub point: (f32, f32),
pub response: f32,
pub size: f32,
pub octave: usize,
pub class_id: usize,
pub angle: f32,
}
impl ImagePoint for KeyPoint {
fn image_point(&self) -> Point2<f64> {
Point2::new(self.point.0 as f64, self.point.1 as f64)
}
}
#[derive(Debug, Copy, Clone)]
pub struct Akaze {
pub num_sublevels: u32,
pub max_octave_evolution: u32,
pub base_scale_offset: f64,
pub initial_contrast: f64,
pub contrast_percentile: f64,
pub contrast_factor_num_bins: usize,
pub derivative_factor: f64,
pub detector_threshold: f64,
pub descriptor_channels: usize,
pub descriptor_pattern_size: usize,
}
impl Akaze {
pub fn new(threshold: f64) -> Self {
Self {
detector_threshold: threshold,
..Default::default()
}
}
pub fn sparse() -> Self {
Self::new(0.01)
}
pub fn dense() -> Self {
Self::new(0.0001)
}
}
impl Default for Akaze {
fn default() -> Akaze {
Akaze {
num_sublevels: 4,
max_octave_evolution: 4,
base_scale_offset: 1.6f64,
initial_contrast: 0.001f64,
contrast_percentile: 0.7f64,
contrast_factor_num_bins: 300,
derivative_factor: 1.5f64,
detector_threshold: 0.001f64,
descriptor_channels: 3usize,
descriptor_pattern_size: 10usize,
}
}
}
impl Akaze {
fn create_nonlinear_scale_space(
&self,
evolutions: &mut Vec<EvolutionStep>,
image: &GrayFloatImage,
) {
trace!("Creating first evolution.");
evolutions[0].Lt = gaussian_blur(image, self.base_scale_offset as f32);
trace!("Gaussian blur finished.");
evolutions[0].Lsmooth = evolutions[0].Lt.clone();
debug!(
"Convolving first evolution with sigma={} Gaussian.",
self.base_scale_offset
);
let mut contrast_factor = contrast_factor::compute_contrast_factor(
&evolutions[0].Lsmooth,
self.contrast_percentile,
1.0f64,
self.contrast_factor_num_bins,
);
trace!("Computing contrast factor finished.");
debug!(
"Contrast percentile={}, Num bins={}, Initial contrast factor={}",
self.contrast_percentile, self.contrast_factor_num_bins, contrast_factor
);
for i in 1..evolutions.len() {
trace!("Creating evolution {}.", i);
if evolutions[i].octave > evolutions[i - 1].octave {
evolutions[i].Lt = evolutions[i - 1].Lt.half_size();
trace!("Half-sizing done.");
contrast_factor *= 0.75;
debug!(
"New image size: {}x{}, new contrast factor: {}",
evolutions[i].Lt.width(),
evolutions[i].Lt.height(),
contrast_factor
);
} else {
evolutions[i].Lt = evolutions[i - 1].Lt.clone();
}
evolutions[i].Lsmooth = gaussian_blur(&evolutions[i].Lt, 1.0f32);
trace!("Gaussian blur finished.");
evolutions[i].Lx = derivatives::scharr_horizontal(&evolutions[i].Lsmooth, 1);
trace!("Computing derivative Lx done.");
evolutions[i].Ly = derivatives::scharr_vertical(&evolutions[i].Lsmooth, 1);
trace!("Computing derivative Ly done.");
evolutions[i].Lflow = pm_g2(&evolutions[i].Lx, &evolutions[i].Ly, contrast_factor);
trace!("Lflow finished.");
for j in 0..evolutions[i].fed_tau_steps.len() {
trace!("Starting diffusion step.");
let step_size = evolutions[i].fed_tau_steps[j];
nonlinear_diffusion::calculate_step(&mut evolutions[i], step_size as f32);
trace!("Diffusion step finished with step size {}", step_size);
}
}
}
fn find_image_keypoints(&self, evolutions: &mut Vec<EvolutionStep>) -> Vec<KeyPoint> {
self.detector_response(evolutions);
trace!("Computing detector response finished.");
self.detect_keypoints(evolutions)
}
pub fn extract(&self, image: &DynamicImage) -> (Vec<KeyPoint>, Vec<BitArray<64>>) {
let float_image = GrayFloatImage::from_dynamic(&image);
info!("Loaded a {} x {} image", image.width(), image.height());
let mut evolutions = self.allocate_evolutions(image.width(), image.height());
self.create_nonlinear_scale_space(&mut evolutions, &float_image);
trace!("Creating scale space finished.");
let keypoints = self.find_image_keypoints(&mut evolutions);
let descriptors = self.extract_descriptors(&evolutions, &keypoints);
trace!("Computing descriptors finished.");
(keypoints, descriptors)
}
pub fn extract_path(
&self,
path: impl AsRef<Path>,
) -> ImageResult<(Vec<KeyPoint>, Vec<BitArray<64>>)> {
Ok(self.extract(&::image::open(path)?))
}
}