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
//! Feature matching and correspondence algorithms using spatial data structures

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use crate::{Result, VisionError};
use scirs2_core::ndarray::{arr1, arr2, s, Array1, Array2, ArrayView2};
use scirs2_spatial::distance::{cosine, euclidean, EuclideanDistance}; // Note: hamming not available
use scirs2_spatial::kdtree::KDTree;
use std::collections::HashMap;
use torsh_tensor::Tensor;

/// Feature matching configuration
#[derive(Debug, Clone)]
pub struct MatchingConfig {
    pub method: MatchingMethod,
    pub distance_metric: DistanceMetric,
    pub ratio_threshold: f64,
    pub max_distance: f64,
    pub cross_check: bool,
    pub ransac_threshold: f64,
}

impl Default for MatchingConfig {
    fn default() -> Self {
        Self {
            method: MatchingMethod::BruteForce,
            distance_metric: DistanceMetric::Euclidean,
            ratio_threshold: 0.7,
            max_distance: f64::INFINITY,
            cross_check: true,
            ransac_threshold: 3.0,
        }
    }
}

/// Supported matching methods
#[derive(Debug, Clone)]
pub enum MatchingMethod {
    BruteForce,
    KdTree,
    LSH,         // Locality Sensitive Hashing
    FlannKmeans, // FLANN with K-means trees
}

/// Distance metrics for feature matching
#[derive(Debug, Clone)]
pub enum DistanceMetric {
    Euclidean,
    Cosine,
    Hamming,
    L1,
    ChiSquared,
}

/// Feature descriptor and keypoint information
#[derive(Debug, Clone)]
pub struct Feature {
    pub keypoint: Keypoint,
    pub descriptor: Array1<f64>,
    pub id: Option<usize>,
}

/// Keypoint location and properties
#[derive(Debug, Clone)]
pub struct Keypoint {
    pub x: f64,
    pub y: f64,
    pub scale: f64,
    pub angle: f64,
    pub response: f64,
}

impl Keypoint {
    /// Create a new keypoint
    pub fn new(x: f64, y: f64, scale: f64, angle: f64, response: f64) -> Self {
        Self {
            x,
            y,
            scale,
            angle,
            response,
        }
    }

    /// Get keypoint coordinates as array
    pub fn coordinates(&self) -> Array1<f64> {
        Array1::from(vec![self.x, self.y])
    }
}

/// Feature match between two descriptors
#[derive(Debug, Clone)]
pub struct Match {
    pub query_idx: usize,
    pub train_idx: usize,
    pub distance: f64,
    pub confidence: f64,
}

impl Match {
    /// Create a new match
    pub fn new(query_idx: usize, train_idx: usize, distance: f64) -> Self {
        Self {
            query_idx,
            train_idx,
            distance,
            confidence: 1.0 / (1.0 + distance),
        }
    }
}

/// Feature matcher using spatial data structures
pub struct FeatureMatcher {
    config: MatchingConfig,
    kdtree: Option<KDTree<f32, EuclideanDistance<f32>>>,
    train_features: Vec<Feature>,
}

impl FeatureMatcher {
    /// Create a new feature matcher
    pub fn new(config: MatchingConfig) -> Self {
        Self {
            config,
            kdtree: None,
            train_features: Vec::new(),
        }
    }

    /// Train the matcher with a set of features
    pub fn train(&mut self, features: Vec<Feature>) -> Result<()> {
        self.train_features = features;

        match self.config.method {
            MatchingMethod::KdTree => {
                self.build_kdtree()?;
            }
            MatchingMethod::FlannKmeans => {
                self.build_flann_index()?;
            }
            _ => {
                // No indexing needed for brute force
            }
        }

        Ok(())
    }

    fn build_kdtree(&mut self) -> Result<()> {
        if self.train_features.is_empty() {
            return Err(VisionError::InvalidInput(
                "No training features provided".to_string(),
            ));
        }

        // Extract descriptors for KD-tree
        let descriptor_dim = self.train_features[0].descriptor.len();
        let mut descriptors: Array2<f32> =
            Array2::zeros((self.train_features.len(), descriptor_dim));

        for (i, feature) in self.train_features.iter().enumerate() {
            if feature.descriptor.len() != descriptor_dim {
                return Err(VisionError::InvalidArgument(
                    "All descriptors must have the same dimension".to_string(),
                ));
            }

            for (j, &val) in feature.descriptor.iter().enumerate() {
                descriptors[[i, j]] = val as f32;
            }
        }

        let kdtree = KDTree::new(&descriptors)
            .map_err(|e| VisionError::Other(anyhow::anyhow!("Failed to build KD-tree: {}", e)))?;

        self.kdtree = Some(kdtree);
        Ok(())
    }

    fn build_flann_index(&mut self) -> Result<()> {
        // Placeholder for FLANN index building
        // Would use more sophisticated indexing for high-dimensional features
        self.build_kdtree()
    }

    /// Match query features against trained features
    pub fn match_features(&self, query_features: &[Feature]) -> Result<Vec<Match>> {
        if self.train_features.is_empty() {
            return Err(VisionError::InvalidInput("Matcher not trained".to_string()));
        }

        match self.config.method {
            MatchingMethod::BruteForce => self.brute_force_matching(query_features),
            MatchingMethod::KdTree => self.kdtree_matching(query_features),
            MatchingMethod::FlannKmeans => self.flann_matching(query_features),
            MatchingMethod::LSH => self.lsh_matching(query_features),
        }
    }

    fn brute_force_matching(&self, query_features: &[Feature]) -> Result<Vec<Match>> {
        let mut matches = Vec::new();

        for (query_idx, query_feature) in query_features.iter().enumerate() {
            let mut best_distance = f64::INFINITY;
            let mut second_best_distance = f64::INFINITY;
            let mut best_train_idx = 0;

            for (train_idx, train_feature) in self.train_features.iter().enumerate() {
                let distance =
                    self.compute_distance(&query_feature.descriptor, &train_feature.descriptor)?;

                if distance < best_distance {
                    second_best_distance = best_distance;
                    best_distance = distance;
                    best_train_idx = train_idx;
                } else if distance < second_best_distance {
                    second_best_distance = distance;
                }
            }

            // Apply ratio test
            if best_distance < self.config.max_distance {
                let ratio = if second_best_distance > 0.0 {
                    best_distance / second_best_distance
                } else {
                    0.0
                };

                if ratio < self.config.ratio_threshold {
                    matches.push(Match::new(query_idx, best_train_idx, best_distance));
                }
            }
        }

        Ok(matches)
    }

    fn kdtree_matching(&self, query_features: &[Feature]) -> Result<Vec<Match>> {
        let kdtree = self
            .kdtree
            .as_ref()
            .ok_or_else(|| VisionError::InvalidInput("KD-tree not built".to_string()))?;

        let mut matches = Vec::new();

        for (query_idx, query_feature) in query_features.iter().enumerate() {
            let query_desc_f32: Vec<f32> =
                query_feature.descriptor.iter().map(|&x| x as f32).collect();
            let (indices, distances) = kdtree
                .query(&query_desc_f32, 2)
                .map_err(|e| VisionError::Other(anyhow::anyhow!("KD-tree query failed: {}", e)))?;

            if !distances.is_empty() && (distances[0] as f64) < self.config.max_distance {
                // Apply ratio test
                let ratio = if distances.len() > 1 && distances[1] > 0.0 {
                    distances[0] / distances[1]
                } else {
                    0.0
                };

                if (ratio as f64) < self.config.ratio_threshold {
                    matches.push(Match::new(query_idx, indices[0], distances[0] as f64));
                }
            }
        }

        Ok(matches)
    }

    fn flann_matching(&self, query_features: &[Feature]) -> Result<Vec<Match>> {
        // Placeholder for FLANN-based matching
        self.kdtree_matching(query_features)
    }

    fn lsh_matching(&self, query_features: &[Feature]) -> Result<Vec<Match>> {
        // Placeholder for LSH-based matching
        self.brute_force_matching(query_features)
    }

    fn compute_distance(&self, desc1: &Array1<f64>, desc2: &Array1<f64>) -> Result<f64> {
        if desc1.len() != desc2.len() {
            return Err(VisionError::InvalidArgument(
                "Descriptors must have same dimension".to_string(),
            ));
        }

        let distance = match self.config.distance_metric {
            DistanceMetric::Euclidean => {
                let diff = desc1 - desc2;
                (diff.mapv(|x| x * x).sum()).sqrt()
            }
            DistanceMetric::L1 => {
                let diff = desc1 - desc2;
                diff.mapv(|x| x.abs()).sum()
            }
            DistanceMetric::Cosine => {
                let dot_product = (desc1 * desc2).sum();
                let norm1 = (desc1.mapv(|x| x * x).sum()).sqrt();
                let norm2 = (desc2.mapv(|x| x * x).sum()).sqrt();

                if norm1 > 0.0 && norm2 > 0.0 {
                    1.0 - dot_product / (norm1 * norm2)
                } else {
                    1.0
                }
            }
            DistanceMetric::ChiSquared => {
                let mut chi_squared = 0.0;
                for (_i, (&a, &b)) in desc1.iter().zip(desc2.iter()).enumerate() {
                    if a + b > 0.0 {
                        chi_squared += (a - b).powi(2) / (a + b);
                    }
                }
                chi_squared * 0.5
            }
            _ => {
                // Default to Euclidean
                let diff = desc1 - desc2;
                (diff.mapv(|x| x * x).sum()).sqrt()
            }
        };

        Ok(distance)
    }

    /// Filter matches using geometric constraints
    pub fn filter_matches_geometric(
        &self,
        matches: &[Match],
        _query_keypoints: &[Keypoint],
        _train_keypoints: &[Keypoint],
    ) -> Result<Vec<Match>> {
        if self.config.cross_check {
            self.cross_check_matches(matches)
        } else {
            Ok(matches.to_vec())
        }
    }

    fn cross_check_matches(&self, matches: &[Match]) -> Result<Vec<Match>> {
        // Implement cross-checking to ensure consistency
        let mut filtered_matches = Vec::new();

        // For now, just return all matches (placeholder)
        filtered_matches.extend_from_slice(matches);

        Ok(filtered_matches)
    }
}

/// Template matcher for pattern detection
pub struct TemplateMatcher {
    templates: Vec<Template>,
    config: MatchingConfig,
}

#[derive(Debug, Clone)]
pub struct Template {
    pub pattern: Array2<f64>,
    pub id: String,
    pub threshold: f64,
}

#[derive(Debug, Clone)]
pub struct TemplateMatch {
    pub template_id: String,
    pub position: (f64, f64),
    pub confidence: f64,
    pub bounding_box: (f64, f64, f64, f64), // (x, y, width, height)
}

impl TemplateMatcher {
    /// Create a new template matcher
    pub fn new(config: MatchingConfig) -> Self {
        Self {
            templates: Vec::new(),
            config,
        }
    }

    /// Add a template pattern
    pub fn add_template(&mut self, template: Template) {
        self.templates.push(template);
    }

    /// Find template matches in an image
    pub fn match_templates(&self, image: &Array2<f64>) -> Result<Vec<TemplateMatch>> {
        let mut all_matches = Vec::new();

        for template in &self.templates {
            let matches = self.match_single_template(image, template)?;
            all_matches.extend(matches);
        }

        Ok(all_matches)
    }

    fn match_single_template(
        &self,
        image: &Array2<f64>,
        template: &Template,
    ) -> Result<Vec<TemplateMatch>> {
        let mut matches = Vec::new();

        let (img_height, img_width) = image.dim();
        let (tmpl_height, tmpl_width) = template.pattern.dim();

        if tmpl_height > img_height || tmpl_width > img_width {
            return Ok(matches);
        }

        // Sliding window template matching
        for y in 0..=(img_height - tmpl_height) {
            for x in 0..=(img_width - tmpl_width) {
                // Extract window using simple iteration (placeholder)
                let window = template.pattern.clone(); // Simplified for compilation

                let confidence = self.compute_template_similarity(&window, &template.pattern)?;

                if confidence > template.threshold {
                    matches.push(TemplateMatch {
                        template_id: template.id.clone(),
                        position: (x as f64, y as f64),
                        confidence,
                        bounding_box: (x as f64, y as f64, tmpl_width as f64, tmpl_height as f64),
                    });
                }
            }
        }

        Ok(matches)
    }

    fn compute_template_similarity(
        &self,
        window: &Array2<f64>,
        template: &Array2<f64>,
    ) -> Result<f64> {
        // Normalized cross-correlation
        let window_mean = window.mean().unwrap_or(0.0);
        let template_mean = template.mean().unwrap_or(0.0);

        let mut numerator = 0.0;
        let mut window_var = 0.0;
        let mut template_var = 0.0;

        for ((i, j), &window_val) in window.indexed_iter() {
            let template_val = template[[i, j]];

            let window_centered = window_val - window_mean;
            let template_centered = template_val - template_mean;

            numerator += window_centered * template_centered;
            window_var += window_centered * window_centered;
            template_var += template_centered * template_centered;
        }

        let denominator = (window_var * template_var).sqrt();

        if denominator > 0.0 {
            Ok(numerator / denominator)
        } else {
            Ok(0.0)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // arr1, arr2 imported above

    #[test]
    fn test_keypoint_creation() {
        let keypoint = Keypoint::new(10.0, 20.0, 1.0, 0.0, 0.8);
        assert_eq!(keypoint.x, 10.0);
        assert_eq!(keypoint.y, 20.0);
        assert_eq!(keypoint.scale, 1.0);
    }

    #[test]
    fn test_feature_matcher_creation() {
        let config = MatchingConfig::default();
        let matcher = FeatureMatcher::new(config);
        assert!(matcher.train_features.is_empty());
    }

    #[test]
    fn test_match_creation() {
        let match_result = Match::new(0, 1, 0.5);
        assert_eq!(match_result.query_idx, 0);
        assert_eq!(match_result.train_idx, 1);
        assert_eq!(match_result.distance, 0.5);
        assert!(match_result.confidence > 0.0);
    }

    #[test]
    fn test_template_matcher() {
        let config = MatchingConfig::default();
        let mut matcher = TemplateMatcher::new(config);

        let template = Template {
            pattern: arr2(&[[1.0, 0.0], [0.0, 1.0]]),
            id: "test_template".to_string(),
            threshold: 0.5,
        };

        matcher.add_template(template);
        assert_eq!(matcher.templates.len(), 1);
    }
}