torsh-vision 0.1.2

Computer vision utilities for ToRSh deep learning framework
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Advanced Feature Detection and Description
//!
//! This module provides state-of-the-art feature detection and description algorithms
//! integrated from scirs2-vision 0.1.5, including neural network-based methods.
//!
//! # Features
//! - SuperPoint: Self-supervised learned feature detector and descriptor
//! - Learned SIFT: Deep learning enhanced SIFT features
//! - Attention-based feature matching
//! - Multi-scale feature extraction
//! - SIMD-accelerated traditional methods (Harris, FAST, etc.)
//!
//! # Examples
//!
//! ```rust,ignore
//! use torsh_vision::feature_detection_advanced::*;
//!
//! // SuperPoint feature detection
//! let detector = SuperPointDetector::new()?;
//! let features = detector.detect(&image)?;
//!
//! // Feature matching with attention
//! let matcher = AttentionMatcher::new()?;
//! let matches = matcher.match_features(&features1, &features2)?;
//! ```

use crate::{Result, VisionError};
use scirs2_core::ndarray::{Array1, Array2, Array3, ArrayView2};
use std::collections::HashMap;
use torsh_nn::Module;
use torsh_tensor::Tensor;

/// Feature point with location, response, and descriptor
#[derive(Debug, Clone)]
pub struct Feature {
    /// X coordinate
    pub x: f32,
    /// Y coordinate
    pub y: f32,
    /// Response strength/score
    pub response: f32,
    /// Feature descriptor vector
    pub descriptor: Array1<f32>,
    /// Scale/size of the feature
    pub scale: f32,
    /// Orientation (in radians)
    pub orientation: f32,
}

/// SuperPoint detector configuration
#[derive(Debug, Clone)]
pub struct SuperPointConfig {
    /// Detection threshold
    pub detection_threshold: f32,
    /// Non-maximum suppression radius
    pub nms_radius: usize,
    /// Maximum number of features to detect
    pub max_features: usize,
    /// Whether to use GPU acceleration
    pub use_gpu: bool,
}

impl Default for SuperPointConfig {
    fn default() -> Self {
        Self {
            detection_threshold: 0.015,
            nms_radius: 4,
            max_features: 1000,
            use_gpu: false,
        }
    }
}

/// SuperPoint: Self-supervised interest point detector and descriptor
///
/// Based on "SuperPoint: Self-Supervised Interest Point Detection and Description"
/// (DeTone et al., CVPR 2018)
pub struct SuperPointDetector {
    config: SuperPointConfig,
    // Neural network model for detection and description
    // TODO: Integrate with torsh-nn for the actual neural network
}

impl SuperPointDetector {
    /// Create a new SuperPoint detector
    pub fn new(config: SuperPointConfig) -> Result<Self> {
        Ok(Self { config })
    }

    /// Detect features in an image
    pub fn detect(&self, _image: &Tensor) -> Result<Vec<Feature>> {
        // TODO: Implement SuperPoint detection using torsh-nn
        // For now, return placeholder error
        Err(VisionError::InvalidParameter(
            "SuperPoint detection not yet implemented - requires neural network integration"
                .to_string(),
        ))
    }

    /// Detect and compute descriptors
    pub fn detect_and_compute(&self, image: &Tensor) -> Result<Vec<Feature>> {
        self.detect(image)
    }
}

/// Learned SIFT configuration
#[derive(Debug, Clone)]
pub struct LearnedSiftConfig {
    /// Number of octaves for multi-scale detection
    pub num_octaves: usize,
    /// Number of scales per octave
    pub num_scales: usize,
    /// Detection threshold
    pub detection_threshold: f32,
    /// Edge threshold for removing edge responses
    pub edge_threshold: f32,
    /// Contrast threshold
    pub contrast_threshold: f32,
}

impl Default for LearnedSiftConfig {
    fn default() -> Self {
        Self {
            num_octaves: 4,
            num_scales: 3,
            detection_threshold: 0.04,
            edge_threshold: 10.0,
            contrast_threshold: 0.03,
        }
    }
}

/// Learned SIFT: Deep learning enhanced SIFT detector
///
/// Combines traditional SIFT keypoint detection with learned descriptors
pub struct LearnedSiftDetector {
    config: LearnedSiftConfig,
}

impl LearnedSiftDetector {
    /// Create a new Learned SIFT detector
    pub fn new(config: LearnedSiftConfig) -> Result<Self> {
        Ok(Self { config })
    }

    /// Detect SIFT features with learned descriptors
    pub fn detect(&self, _image: &Tensor) -> Result<Vec<Feature>> {
        // TODO: Implement Learned SIFT detection
        Err(VisionError::InvalidParameter(
            "Learned SIFT not yet implemented - requires neural network integration".to_string(),
        ))
    }
}

/// Attention-based feature matcher configuration
#[derive(Debug, Clone)]
pub struct AttentionMatcherConfig {
    /// Number of attention heads
    pub num_heads: usize,
    /// Hidden dimension size
    pub hidden_dim: usize,
    /// Match confidence threshold
    pub match_threshold: f32,
    /// Whether to enforce mutual best match
    pub mutual_match: bool,
}

impl Default for AttentionMatcherConfig {
    fn default() -> Self {
        Self {
            num_heads: 8,
            hidden_dim: 256,
            match_threshold: 0.5,
            mutual_match: true,
        }
    }
}

/// Feature match with confidence score
#[derive(Debug, Clone)]
pub struct FeatureMatch {
    /// Index of feature in first image
    pub idx1: usize,
    /// Index of feature in second image
    pub idx2: usize,
    /// Match confidence score (0.0 to 1.0)
    pub confidence: f32,
    /// Distance between descriptors
    pub distance: f32,
}

/// Attention-based feature matcher
///
/// Uses transformer-style attention mechanism for robust feature matching
pub struct AttentionMatcher {
    config: AttentionMatcherConfig,
}

impl AttentionMatcher {
    /// Create a new attention-based matcher
    pub fn new(config: AttentionMatcherConfig) -> Result<Self> {
        Ok(Self { config })
    }

    /// Match features between two images using attention
    pub fn match_features(
        &self,
        features1: &[Feature],
        features2: &[Feature],
    ) -> Result<Vec<FeatureMatch>> {
        if features1.is_empty() || features2.is_empty() {
            return Ok(Vec::new());
        }

        // Extract descriptors
        let desc1 = self.stack_descriptors(features1)?;
        let desc2 = self.stack_descriptors(features2)?;

        // Compute attention-based similarity
        let similarity = self.compute_attention_similarity(&desc1, &desc2)?;

        // Extract matches
        let matches = self.extract_matches(&similarity)?;

        Ok(matches)
    }

    /// Stack descriptors into matrix
    fn stack_descriptors(&self, features: &[Feature]) -> Result<Array2<f32>> {
        if features.is_empty() {
            return Err(VisionError::InvalidParameter(
                "Cannot stack empty feature set".to_string(),
            ));
        }

        let desc_dim = features[0].descriptor.len();
        let mut descriptors = Array2::zeros((features.len(), desc_dim));

        for (i, feature) in features.iter().enumerate() {
            if feature.descriptor.len() != desc_dim {
                return Err(VisionError::InvalidParameter(
                    "Inconsistent descriptor dimensions".to_string(),
                ));
            }
            for (j, &val) in feature.descriptor.iter().enumerate() {
                descriptors[[i, j]] = val;
            }
        }

        Ok(descriptors)
    }

    /// Compute attention-based similarity matrix
    fn compute_attention_similarity(
        &self,
        desc1: &Array2<f32>,
        desc2: &Array2<f32>,
    ) -> Result<Array2<f32>> {
        let n1 = desc1.nrows();
        let n2 = desc2.nrows();

        // Simplified attention: compute dot product similarity
        // TODO: Implement full transformer-style attention with learned parameters
        let mut similarity = Array2::zeros((n1, n2));

        for i in 0..n1 {
            for j in 0..n2 {
                let mut dot = 0.0;
                let mut norm1 = 0.0;
                let mut norm2 = 0.0;

                for k in 0..desc1.ncols() {
                    let v1 = desc1[[i, k]];
                    let v2 = desc2[[j, k]];
                    dot += v1 * v2;
                    norm1 += v1 * v1;
                    norm2 += v2 * v2;
                }

                // Cosine similarity
                similarity[[i, j]] = if norm1 > 0.0 && norm2 > 0.0 {
                    dot / (norm1.sqrt() * norm2.sqrt())
                } else {
                    0.0
                };
            }
        }

        Ok(similarity)
    }

    /// Extract matches from similarity matrix
    fn extract_matches(&self, similarity: &Array2<f32>) -> Result<Vec<FeatureMatch>> {
        let mut matches = Vec::new();
        let n1 = similarity.nrows();
        let n2 = similarity.ncols();

        for i in 0..n1 {
            // Find best match for feature i
            let mut best_j = 0;
            let mut best_score = similarity[[i, 0]];

            for j in 1..n2 {
                if similarity[[i, j]] > best_score {
                    best_score = similarity[[i, j]];
                    best_j = j;
                }
            }

            // Check threshold
            if best_score < self.config.match_threshold {
                continue;
            }

            // Enforce mutual best match if requested
            if self.config.mutual_match {
                let mut reverse_best_i = 0;
                let mut reverse_best_score = similarity[[0, best_j]];

                for ii in 1..n1 {
                    if similarity[[ii, best_j]] > reverse_best_score {
                        reverse_best_score = similarity[[ii, best_j]];
                        reverse_best_i = ii;
                    }
                }

                if reverse_best_i != i {
                    continue; // Not a mutual best match
                }
            }

            matches.push(FeatureMatch {
                idx1: i,
                idx2: best_j,
                confidence: best_score,
                distance: 1.0 - best_score, // Convert similarity to distance
            });
        }

        Ok(matches)
    }
}

/// Multi-scale feature extraction configuration
#[derive(Debug, Clone)]
pub struct MultiScaleConfig {
    /// Number of scale levels
    pub num_scales: usize,
    /// Scale factor between levels
    pub scale_factor: f32,
    /// Whether to compute features at each scale independently
    pub independent_scales: bool,
}

impl Default for MultiScaleConfig {
    fn default() -> Self {
        Self {
            num_scales: 5,
            scale_factor: 1.2,
            independent_scales: false,
        }
    }
}

/// Multi-scale feature detector
///
/// Detects features across multiple image scales for scale invariance
pub struct MultiScaleDetector<D> {
    detector: D,
    config: MultiScaleConfig,
}

impl<D> MultiScaleDetector<D> {
    /// Create a new multi-scale detector wrapping another detector
    pub fn new(detector: D, config: MultiScaleConfig) -> Self {
        Self { detector, config }
    }
}

/// Brute force feature matcher (for comparison baseline)
pub struct BruteForceMatcher {
    /// Distance metric to use
    metric: DistanceMetric,
    /// Cross-check matches for robustness
    cross_check: bool,
}

/// Distance metrics for feature matching
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DistanceMetric {
    /// Euclidean distance (L2)
    L2,
    /// Manhattan distance (L1)
    L1,
    /// Hamming distance (for binary descriptors)
    Hamming,
    /// Cosine distance
    Cosine,
}

impl BruteForceMatcher {
    /// Create a new brute force matcher
    pub fn new(metric: DistanceMetric, cross_check: bool) -> Self {
        Self {
            metric,
            cross_check,
        }
    }

    /// Match features using brute force search
    pub fn match_features(
        &self,
        features1: &[Feature],
        features2: &[Feature],
    ) -> Result<Vec<FeatureMatch>> {
        if features1.is_empty() || features2.is_empty() {
            return Ok(Vec::new());
        }

        let mut matches = Vec::new();

        for (i, f1) in features1.iter().enumerate() {
            let mut best_dist = f32::MAX;
            let mut best_j = 0;

            for (j, f2) in features2.iter().enumerate() {
                let dist = self.compute_distance(&f1.descriptor, &f2.descriptor)?;
                if dist < best_dist {
                    best_dist = dist;
                    best_j = j;
                }
            }

            // Cross-check if enabled
            if self.cross_check {
                let mut reverse_best_dist = f32::MAX;
                let mut reverse_best_i = 0;

                for (i2, f1_2) in features1.iter().enumerate() {
                    let dist =
                        self.compute_distance(&features2[best_j].descriptor, &f1_2.descriptor)?;
                    if dist < reverse_best_dist {
                        reverse_best_dist = dist;
                        reverse_best_i = i2;
                    }
                }

                if reverse_best_i != i {
                    continue; // Not a cross-checked match
                }
            }

            matches.push(FeatureMatch {
                idx1: i,
                idx2: best_j,
                confidence: 1.0 / (1.0 + best_dist), // Convert distance to confidence
                distance: best_dist,
            });
        }

        Ok(matches)
    }

    /// Compute distance between two descriptors
    fn compute_distance(&self, desc1: &Array1<f32>, desc2: &Array1<f32>) -> Result<f32> {
        if desc1.len() != desc2.len() {
            return Err(VisionError::InvalidParameter(
                "Descriptor dimensions mismatch".to_string(),
            ));
        }

        let dist = match self.metric {
            DistanceMetric::L2 => {
                let mut sum = 0.0;
                for i in 0..desc1.len() {
                    let diff = desc1[i] - desc2[i];
                    sum += diff * diff;
                }
                sum.sqrt()
            }
            DistanceMetric::L1 => {
                let mut sum = 0.0;
                for i in 0..desc1.len() {
                    sum += (desc1[i] - desc2[i]).abs();
                }
                sum
            }
            DistanceMetric::Hamming => {
                // For Hamming, convert to binary and count differences
                let mut count = 0;
                for i in 0..desc1.len() {
                    if (desc1[i] > 0.5) != (desc2[i] > 0.5) {
                        count += 1;
                    }
                }
                count as f32
            }
            DistanceMetric::Cosine => {
                let mut dot = 0.0;
                let mut norm1 = 0.0;
                let mut norm2 = 0.0;
                for i in 0..desc1.len() {
                    dot += desc1[i] * desc2[i];
                    norm1 += desc1[i] * desc1[i];
                    norm2 += desc2[i] * desc2[i];
                }
                if norm1 > 0.0 && norm2 > 0.0 {
                    1.0 - dot / (norm1.sqrt() * norm2.sqrt())
                } else {
                    1.0
                }
            }
        };

        Ok(dist)
    }
}

/// Ratio test for filtering matches (Lowe's ratio test)
pub fn apply_ratio_test(matches: &[FeatureMatch], ratio_threshold: f32) -> Vec<FeatureMatch> {
    // Group matches by source feature
    let mut matches_by_src: HashMap<usize, Vec<&FeatureMatch>> = HashMap::new();
    for m in matches {
        matches_by_src
            .entry(m.idx1)
            .or_insert_with(Vec::new)
            .push(m);
    }

    let mut filtered = Vec::new();

    for (_, mut group) in matches_by_src {
        if group.len() < 2 {
            // Need at least 2 matches for ratio test
            if let Some(&m) = group.first() {
                filtered.push(m.clone());
            }
            continue;
        }

        // Sort by distance (ascending)
        group.sort_by(|a, b| {
            a.distance
                .partial_cmp(&b.distance)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Ratio test: best_dist / second_best_dist < threshold
        let best = group[0];
        let second_best = group[1];

        if best.distance / second_best.distance < ratio_threshold {
            filtered.push(best.clone());
        }
    }

    filtered
}

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

    #[test]
    fn test_superpoint_config_default() {
        let config = SuperPointConfig::default();
        assert_eq!(config.detection_threshold, 0.015);
        assert_eq!(config.nms_radius, 4);
        assert_eq!(config.max_features, 1000);
    }

    #[test]
    fn test_attention_matcher_config_default() {
        let config = AttentionMatcherConfig::default();
        assert_eq!(config.num_heads, 8);
        assert_eq!(config.hidden_dim, 256);
        assert_eq!(config.match_threshold, 0.5);
        assert!(config.mutual_match);
    }

    #[test]
    fn test_brute_force_matcher_l2() {
        let matcher = BruteForceMatcher::new(DistanceMetric::L2, false);

        let f1 = Feature {
            x: 0.0,
            y: 0.0,
            response: 1.0,
            descriptor: Array1::from_vec(vec![1.0, 0.0, 0.0]),
            scale: 1.0,
            orientation: 0.0,
        };

        let f2 = Feature {
            x: 1.0,
            y: 1.0,
            response: 1.0,
            descriptor: Array1::from_vec(vec![0.9, 0.1, 0.0]),
            scale: 1.0,
            orientation: 0.0,
        };

        let matches = matcher
            .match_features(&[f1.clone()], &[f2.clone()])
            .expect("Matching failed");
        assert_eq!(matches.len(), 1);
    }

    #[test]
    fn test_attention_matcher_empty_features() {
        let matcher = AttentionMatcher::new(AttentionMatcherConfig::default())
            .expect("Failed to create matcher");

        let matches = matcher.match_features(&[], &[]).expect("Matching failed");
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_ratio_test() {
        let matches = vec![
            FeatureMatch {
                idx1: 0,
                idx2: 0,
                confidence: 0.9,
                distance: 0.1,
            },
            FeatureMatch {
                idx1: 0,
                idx2: 1,
                confidence: 0.5,
                distance: 0.5,
            },
        ];

        let filtered = apply_ratio_test(&matches, 0.8);
        assert_eq!(filtered.len(), 1); // Only best match should pass ratio test
    }

    #[test]
    fn test_multi_scale_config_default() {
        let config = MultiScaleConfig::default();
        assert_eq!(config.num_scales, 5);
        assert_eq!(config.scale_factor, 1.2);
    }
}