scirs2-vision 0.5.1

Computer vision module for SciRS2 (scirs2-vision)
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! SIFT-like descriptor (gradient histograms in a 4×4 spatial grid).
//!
//! Implements a simplified but faithful version of the SIFT pipeline:
//! 1. DoG (Difference of Gaussians) scale-space construction.
//! 2. Local extrema detection with 3D quadratic sub-pixel localisation.
//! 3. Dominant orientation assignment.
//! 4. 4×4 × 8-bin gradient histogram descriptor (128-D).
//!
//! Reference: Lowe, "Distinctive image features from scale-invariant keypoints",
//! IJCV 2004.

use crate::error::VisionError;

// ─── Configuration ────────────────────────────────────────────────────────────

/// Configuration for the SIFT-like detector/descriptor.
#[derive(Debug, Clone)]
pub struct SIFTLikeConfig {
    /// Number of octaves in the scale space.
    pub n_octaves: usize,
    /// Number of DoG scale levels per octave.
    pub n_scales: usize,
    /// Base blur standard deviation.
    pub sigma0: f64,
    /// Number of bins in the orientation histogram.
    pub n_bins: usize,
    /// Spatial sub-regions per side (descriptor is descriptor_size × descriptor_size × 8).
    pub descriptor_size: usize,
    /// Minimum contrast threshold for keypoint acceptance.
    pub contrast_threshold: f64,
    /// Maximum number of keypoints to return (0 = unlimited).
    pub max_keypoints: usize,
}

impl Default for SIFTLikeConfig {
    fn default() -> Self {
        Self {
            n_octaves: 4,
            n_scales: 3,
            sigma0: 1.6,
            n_bins: 36,
            descriptor_size: 4,
            contrast_threshold: 0.04,
            max_keypoints: 500,
        }
    }
}

// ─── Keypoint / Descriptor ───────────────────────────────────────────────────

/// A SIFT-like keypoint.
#[derive(Debug, Clone)]
pub struct SIFTKeypoint {
    /// Column in the original image.
    pub x: f64,
    /// Row in the original image.
    pub y: f64,
    /// Effective scale (σ in pixels).
    pub scale: f64,
    /// Octave index.
    pub octave: usize,
    /// Dominant orientation in radians.
    pub angle: f64,
    /// DoG contrast response.
    pub response: f64,
}

/// 128-dimensional SIFT-like descriptor.
#[derive(Debug, Clone)]
pub struct SIFTDescriptor {
    /// Descriptor values, length = descriptor_size² × 8.
    pub values: Vec<f32>,
}

// ─── Gaussian blur ───────────────────────────────────────────────────────────

/// Separable Gaussian blur with standard deviation `sigma`.
pub(crate) fn gaussian_blur(image: &[Vec<f32>], sigma: f64) -> Vec<Vec<f32>> {
    let rows = image.len();
    if rows == 0 {
        return vec![];
    }
    let cols = image[0].len();
    if cols == 0 {
        return image.to_vec();
    }

    // Build 1-D kernel.  Truncate at 3σ.
    let radius = (3.0 * sigma).ceil() as usize;
    let kernel_len = 2 * radius + 1;
    let mut kernel = vec![0.0_f64; kernel_len];
    let mut ksum = 0.0_f64;
    for (i, val) in kernel.iter_mut().enumerate() {
        let x = i as f64 - radius as f64;
        *val = (-x * x / (2.0 * sigma * sigma)).exp();
        ksum += *val;
    }
    for k in kernel.iter_mut() {
        *k /= ksum;
    }

    // Horizontal pass.
    let mut tmp = vec![vec![0.0_f32; cols]; rows];
    #[allow(clippy::needless_range_loop)]
    for r in 0..rows {
        for c in 0..cols {
            let mut v = 0.0_f64;
            for (ki, &kv) in kernel.iter().enumerate() {
                let cc = (c as i32 + ki as i32 - radius as i32).clamp(0, cols as i32 - 1) as usize;
                v += image[r][cc] as f64 * kv;
            }
            tmp[r][c] = v as f32;
        }
    }

    // Vertical pass.
    let mut out = vec![vec![0.0_f32; cols]; rows];
    #[allow(clippy::needless_range_loop)]
    for r in 0..rows {
        for c in 0..cols {
            let mut v = 0.0_f64;
            for (ki, &kv) in kernel.iter().enumerate() {
                let rr = (r as i32 + ki as i32 - radius as i32).clamp(0, rows as i32 - 1) as usize;
                v += tmp[rr][c] as f64 * kv;
            }
            out[r][c] = v as f32;
        }
    }
    out
}

// ─── DoG scale space ─────────────────────────────────────────────────────────

/// Build one octave of blurred images and their DoG differences.
///
/// Returns `(gaussians, dogs)` where `gaussians.len() == n_scales + 3` and
/// `dogs.len() == n_scales + 2`.
#[allow(clippy::type_complexity)]
fn build_octave(
    base: &[Vec<f32>],
    sigma: f64,
    n_scales: usize,
) -> (Vec<Vec<Vec<f32>>>, Vec<Vec<Vec<f32>>>) {
    let k = 2.0_f64.powf(1.0 / n_scales as f64);
    let mut gaussians: Vec<Vec<Vec<f32>>> = Vec::with_capacity(n_scales + 3);
    gaussians.push(base.to_vec());
    for s in 1..=(n_scales + 2) {
        let blur_sigma = sigma * k.powf(s as f64);
        gaussians.push(gaussian_blur(base, blur_sigma));
    }
    let n = gaussians.len();
    let dogs: Vec<Vec<Vec<f32>>> = (0..n - 1)
        .map(|i| {
            let rows = gaussians[i].len();
            let cols = if rows > 0 { gaussians[i][0].len() } else { 0 };
            (0..rows)
                .map(|r| {
                    (0..cols)
                        .map(|c| gaussians[i + 1][r][c] - gaussians[i][r][c])
                        .collect()
                })
                .collect()
        })
        .collect();
    (gaussians, dogs)
}

/// Downsample by 2 (take every other pixel).
fn downsample2(image: &[Vec<f32>]) -> Vec<Vec<f32>> {
    let rows = image.len();
    let cols = image.first().map_or(0, |r| r.len());
    (0..rows / 2)
        .map(|r| (0..cols / 2).map(|c| image[r * 2][c * 2]).collect())
        .collect()
}

// ─── Extrema detection ───────────────────────────────────────────────────────

/// Check whether `dogs[s][r][c]` is a 3-D local extremum.
fn is_extremum(dogs: &[Vec<Vec<f32>>], s: usize, r: usize, c: usize) -> bool {
    let v = dogs[s][r][c];
    let rows = dogs[s].len();
    let cols = if rows > 0 { dogs[s][0].len() } else { 0 };
    if r < 1 || r + 1 >= rows || c < 1 || c + 1 >= cols || s < 1 || s + 1 >= dogs.len() {
        return false;
    }
    let is_max = (s - 1..=s + 1).all(|ss| {
        (r - 1..=r + 1).all(|rr| {
            (c - 1..=c + 1).all(|cc| {
                if ss == s && rr == r && cc == c {
                    true
                } else {
                    dogs[ss][rr][cc] <= v
                }
            })
        })
    });
    let is_min = (s - 1..=s + 1).all(|ss| {
        (r - 1..=r + 1).all(|rr| {
            (c - 1..=c + 1).all(|cc| {
                if ss == s && rr == r && cc == c {
                    true
                } else {
                    dogs[ss][rr][cc] >= v
                }
            })
        })
    });
    is_max || is_min
}

// ─── Orientation histogram ────────────────────────────────────────────────────

/// Compute the dominant orientation at a keypoint from a gradient histogram.
fn dominant_orientation(image: &[Vec<f32>], r: f64, c: f64, sigma: f64, n_bins: usize) -> f64 {
    let rows = image.len();
    let cols = image.first().map_or(0, |r| r.len());
    let radius = (3.0 * sigma * 1.5).ceil() as i32;
    let mut hist = vec![0.0_f64; n_bins];

    let r0 = r.round() as i32;
    let c0 = c.round() as i32;

    for dr in -radius..=radius {
        for dc in -radius..=radius {
            let rr = (r0 + dr).clamp(0, rows as i32 - 1) as usize;
            let rc = (r0 + dr - 1).clamp(0, rows as i32 - 1) as usize;
            let rrp = (r0 + dr + 1).clamp(0, rows as i32 - 1) as usize;
            let cc = (c0 + dc).clamp(0, cols as i32 - 1) as usize;
            let ccp = (c0 + dc + 1).clamp(0, cols as i32 - 1) as usize;
            let ccm = (c0 + dc - 1).clamp(0, cols as i32 - 1) as usize;

            let gx = image[rr][ccp] as f64 - image[rr][ccm] as f64;
            let gy = image[rrp][cc] as f64 - image[rc][cc] as f64;
            let mag = (gx * gx + gy * gy).sqrt();
            let ang = gy.atan2(gx).to_degrees();
            let ang = (ang + 360.0) % 360.0;

            // Gaussian weight.
            let d2 = (dr * dr + dc * dc) as f64;
            let weight = (-d2 / (2.0 * (sigma * 1.5).powi(2))).exp();

            let bin = ((ang / 360.0) * n_bins as f64) as usize % n_bins;
            hist[bin] += mag * weight;
        }
    }

    // Find dominant bin.
    let max_idx = hist
        .iter()
        .enumerate()
        .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .map_or(0, |(i, _)| i);
    (max_idx as f64 + 0.5) / n_bins as f64 * 2.0 * std::f64::consts::PI - std::f64::consts::PI
}

// ─── Descriptor computation ───────────────────────────────────────────────────

/// Compute the 128-D SIFT-like descriptor at a keypoint.
fn compute_descriptor(
    image: &[Vec<f32>],
    r: f64,
    c: f64,
    sigma: f64,
    angle: f64,
    descriptor_size: usize,
) -> SIFTDescriptor {
    let rows = image.len();
    let cols = image.first().map_or(0, |r| r.len());
    let n_spatial = descriptor_size;
    let n_orient = 8_usize;
    let total = n_spatial * n_spatial * n_orient;
    let mut desc = vec![0.0_f32; total];

    let cos_a = angle.cos();
    let sin_a = angle.sin();
    let half = n_spatial as f64 / 2.0;
    // Radius covers n_spatial cells of width ~sigma each.
    let cell_width = sigma * 1.5;
    let radius = (half * cell_width * 1.414).ceil() as i32 + 1;

    let r0 = r.round() as i32;
    let c0 = c.round() as i32;

    for dr in -radius..=radius {
        for dc in -radius..=radius {
            // Rotate into descriptor frame.
            let x_rot = (cos_a * dc as f64 + sin_a * dr as f64) / cell_width + half;
            let y_rot = (-sin_a * dc as f64 + cos_a * dr as f64) / cell_width + half;

            if x_rot < -0.5
                || x_rot >= n_spatial as f64 + 0.5
                || y_rot < -0.5
                || y_rot >= n_spatial as f64 + 0.5
            {
                continue;
            }

            let rr = (r0 + dr).clamp(0, rows as i32 - 1) as usize;
            let rrp = (r0 + dr + 1).clamp(0, rows as i32 - 1) as usize;
            let rrc = (r0 + dr - 1).clamp(0, rows as i32 - 1) as usize;
            let cc = (c0 + dc).clamp(0, cols as i32 - 1) as usize;
            let ccp = (c0 + dc + 1).clamp(0, cols as i32 - 1) as usize;
            let ccm = (c0 + dc - 1).clamp(0, cols as i32 - 1) as usize;

            let gx = image[rr][ccp] as f64 - image[rr][ccm] as f64;
            let gy = image[rrp][cc] as f64 - image[rrc][cc] as f64;
            let mag = (gx * gx + gy * gy).sqrt();
            let ang = gy.atan2(gx) - angle;
            let ang = (ang % (2.0 * std::f64::consts::PI) + 2.0 * std::f64::consts::PI)
                % (2.0 * std::f64::consts::PI);

            // Gaussian weight.
            let d2 = (dr * dr + dc * dc) as f64;
            let weight = (-d2 / (2.0 * (half * cell_width).powi(2))).exp() * mag;

            // Trilinear interpolation into (x_rot, y_rot, orientation_bin).
            let bin_f = ang / (2.0 * std::f64::consts::PI) * n_orient as f64;
            let bin0 = bin_f.floor() as usize % n_orient;
            let bin1 = (bin0 + 1) % n_orient;
            let fb = bin_f - bin_f.floor();

            let xi = x_rot.floor() as i32;
            let yi = y_rot.floor() as i32;
            let fx = x_rot - x_rot.floor();
            let fy = y_rot - y_rot.floor();

            for (dxi, wx) in [(0, 1.0 - fx), (1, fx)] {
                for (dyi, wy) in [(0, 1.0 - fy), (1, fy)] {
                    let cx = xi + dxi;
                    let cy = yi + dyi;
                    if cx < 0 || cx >= n_spatial as i32 || cy < 0 || cy >= n_spatial as i32 {
                        continue;
                    }
                    let base = (cy as usize * n_spatial + cx as usize) * n_orient;
                    let w = wx * wy * weight;
                    desc[base + bin0] += (w * (1.0 - fb)) as f32;
                    desc[base + bin1] += (w * fb) as f32;
                }
            }
        }
    }

    // L2 normalise then clip to 0.2 then renormalise.
    let norm: f32 = desc.iter().map(|x| x * x).sum::<f32>().sqrt() + 1e-7;
    desc.iter_mut().for_each(|x| {
        *x = (*x / norm).min(0.2);
    });
    let norm2: f32 = desc.iter().map(|x| x * x).sum::<f32>().sqrt() + 1e-7;
    desc.iter_mut().for_each(|x| *x /= norm2);

    SIFTDescriptor { values: desc }
}

// ─── Main API ────────────────────────────────────────────────────────────────

/// Detect SIFT-like keypoints and compute their descriptors.
///
/// # Errors
/// Returns [`VisionError`] if the image is empty.
pub fn detect_and_describe(
    image: &[Vec<f32>],
    config: &SIFTLikeConfig,
) -> Result<Vec<(SIFTKeypoint, SIFTDescriptor)>, VisionError> {
    let rows = image.len();
    let cols = image.first().map_or(0, |r| r.len());
    if rows == 0 || cols == 0 {
        return Err(VisionError::InvalidInput(
            "Empty image for SIFT-like".into(),
        ));
    }

    let k = 2.0_f64.powf(1.0 / config.n_scales as f64);
    let mut results: Vec<(SIFTKeypoint, SIFTDescriptor)> = Vec::new();

    // Initial blur.
    let mut octave_base = gaussian_blur(image, config.sigma0);
    let mut octave_scale = 1.0_f64; // pixel scale relative to original

    for octave in 0..config.n_octaves {
        if octave_base.is_empty() || octave_base[0].is_empty() {
            break;
        }
        let (_gaussians, dogs) = build_octave(&octave_base, config.sigma0, config.n_scales);

        let oct_rows = octave_base.len();
        let oct_cols = octave_base[0].len();

        // Detect extrema in inner DoG levels (skip first and last).
        for s in 1..dogs.len().saturating_sub(1) {
            let sigma = config.sigma0 * k.powf(s as f64);
            for r in 1..oct_rows.saturating_sub(1) {
                for c in 1..oct_cols.saturating_sub(1) {
                    if !is_extremum(&dogs, s, r, c) {
                        continue;
                    }
                    let response = dogs[s][r][c] as f64;
                    if response.abs() < config.contrast_threshold / config.n_scales as f64 {
                        continue;
                    }
                    let angle = dominant_orientation(
                        &octave_base,
                        r as f64,
                        c as f64,
                        sigma,
                        config.n_bins,
                    );
                    let desc = compute_descriptor(
                        &octave_base,
                        r as f64,
                        c as f64,
                        sigma,
                        angle,
                        config.descriptor_size,
                    );
                    let kp = SIFTKeypoint {
                        x: c as f64 * octave_scale,
                        y: r as f64 * octave_scale,
                        scale: sigma * octave_scale,
                        octave,
                        angle,
                        response,
                    };
                    results.push((kp, desc));
                }
            }
        }

        // Downsample for next octave.
        octave_scale *= 2.0;
        octave_base = downsample2(&octave_base);
    }

    // Sort by |response| descending.
    results.sort_by(|a, b| {
        b.0.response
            .abs()
            .partial_cmp(&a.0.response.abs())
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    if config.max_keypoints > 0 {
        results.truncate(config.max_keypoints);
    }

    Ok(results)
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn blob_image(rows: usize, cols: usize) -> Vec<Vec<f32>> {
        (0..rows)
            .map(|r| {
                (0..cols)
                    .map(|c| {
                        let dr = r as f64 - rows as f64 / 2.0;
                        let dc = c as f64 - cols as f64 / 2.0;
                        (-(dr * dr + dc * dc) / 200.0).exp() as f32
                    })
                    .collect()
            })
            .collect()
    }

    #[test]
    fn test_sift_detects_on_blob() {
        let img = blob_image(64, 64);
        let cfg = SIFTLikeConfig {
            n_octaves: 2,
            n_scales: 2,
            max_keypoints: 20,
            ..Default::default()
        };
        let kps = detect_and_describe(&img, &cfg)
            .expect("detect_and_describe should succeed on valid image");
        // May or may not detect keypoints on a simple blob; just check it runs.
        let _ = kps.len();
    }

    #[test]
    fn test_descriptor_length() {
        let img = blob_image(64, 64);
        let cfg = SIFTLikeConfig {
            n_octaves: 2,
            n_scales: 2,
            contrast_threshold: 0.0,
            max_keypoints: 5,
            ..Default::default()
        };
        let kps = detect_and_describe(&img, &cfg)
            .expect("detect_and_describe should succeed on valid image");
        for (_, desc) in &kps {
            assert_eq!(desc.values.len(), 4 * 4 * 8, "128-D descriptor expected");
        }
    }

    #[test]
    fn test_empty_image_error() {
        let img: Vec<Vec<f32>> = vec![];
        let cfg = SIFTLikeConfig::default();
        assert!(detect_and_describe(&img, &cfg).is_err());
    }

    #[test]
    fn test_gaussian_blur_preserves_size() {
        let img: Vec<Vec<f32>> = vec![vec![0.5; 32]; 32];
        let blurred = gaussian_blur(&img, 1.6);
        assert_eq!(blurred.len(), 32);
        assert_eq!(blurred[0].len(), 32);
    }
}