scirs2-vision 0.6.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
//! Multi-source depth fusion.
//!
//! Combines depth maps from multiple sensors using Kalman filtering or
//! confidence-weighted averaging.

use scirs2_core::ndarray::Array2;

use crate::error::{Result, VisionError};

use super::types::{CompletionMethod, CompletionResult, DepthSource, FusionConfig};

// ─────────────────────────────────────────────────────────────────────────────
// KalmanDepthFusion
// ─────────────────────────────────────────────────────────────────────────────

/// Per-pixel Kalman filter for fusing multiple depth sources.
///
/// Each pixel maintains a state `(depth_estimate, variance)`.  Sources are
/// incorporated sequentially via the standard Kalman measurement-update step.
#[derive(Debug, Clone)]
pub struct KalmanDepthFusion {
    /// Per-pixel depth estimate.
    state_mean: Array2<f64>,
    /// Per-pixel variance (uncertainty).
    state_var: Array2<f64>,
    /// Fusion configuration.
    config: FusionConfig,
    /// Number of sources incorporated so far.
    source_count: usize,
}

impl KalmanDepthFusion {
    /// Create a new Kalman depth fusion state.
    pub fn new(height: usize, width: usize, config: FusionConfig) -> Self {
        Self {
            state_mean: Array2::zeros((height, width)),
            state_var: Array2::from_elem((height, width), 1e6), // large initial variance
            config,
            source_count: 0,
        }
    }

    /// Incorporate a new depth source via Kalman measurement update.
    ///
    /// For each pixel with a valid measurement (`depth > 0`), the update is:
    /// ```text
    /// K = P / (P + R)
    /// x = x + K * (z - x)
    /// P = (1 - K) * P
    /// ```
    ///
    /// Outliers are rejected using a Mahalanobis-distance test.
    pub fn update(&mut self, source: &DepthSource) -> Result<()> {
        let (h, w) = self.state_mean.dim();
        let (sh, sw) = source.depth_map.dim();
        if sh != h || sw != w {
            return Err(VisionError::DimensionMismatch(format!(
                "source '{}' is {sh}x{sw} but fusion state is {h}x{w}",
                source.source_id
            )));
        }

        let r = source.noise_variance;

        // Prediction step: add process noise.
        if self.source_count > 0 {
            for row in 0..h {
                for col in 0..w {
                    self.state_var[[row, col]] += self.config.process_noise;
                }
            }
        }

        // Measurement update.
        for row in 0..h {
            for col in 0..w {
                let z = source.depth_map[[row, col]];
                if z <= 0.0 || !z.is_finite() {
                    continue; // no valid measurement
                }

                let p = self.state_var[[row, col]];
                let x = self.state_mean[[row, col]];

                // Outlier rejection (skip for first source).
                if self.source_count > 0 && self.is_outlier(z, x, p, r) {
                    continue;
                }

                // Kalman gain.
                let k = p / (p + r);

                // State update.
                self.state_mean[[row, col]] = x + k * (z - x);
                self.state_var[[row, col]] = (1.0 - k) * p;
            }
        }

        self.source_count += 1;
        Ok(())
    }

    /// Mahalanobis-distance outlier test.
    ///
    /// Returns `true` if the measurement is an outlier:
    /// `|z - x| / sqrt(P + R) > threshold`.
    fn is_outlier(&self, z: f64, x: f64, p: f64, r: f64) -> bool {
        let innovation_var = p + r;
        if innovation_var <= 0.0 {
            return false;
        }
        let mahalanobis = (z - x).abs() / innovation_var.sqrt();
        mahalanobis > self.config.outlier_threshold
    }

    /// Extract the current fused depth map and confidence.
    pub fn result(&self) -> CompletionResult {
        let (h, w) = self.state_mean.dim();
        let mut conf = Array2::zeros((h, w));

        // Confidence is inversely proportional to variance.
        let max_var = self
            .state_var
            .iter()
            .filter(|v| v.is_finite())
            .fold(1.0_f64, |a, &b| a.max(b));

        for row in 0..h {
            for col in 0..w {
                let v = self.state_var[[row, col]];
                conf[[row, col]] = if max_var > 1e-12 {
                    1.0 - (v / max_var).min(1.0)
                } else {
                    1.0
                };
            }
        }

        CompletionResult {
            dense_depth: self.state_mean.clone(),
            confidence_map: conf,
            method_used: CompletionMethod::KalmanFusion,
            iterations: self.source_count,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// fuse_sources
// ─────────────────────────────────────────────────────────────────────────────

/// Fuse multiple depth sources using sequential Kalman updates.
///
/// # Errors
/// Returns an error if no sources are provided or if sources have inconsistent
/// dimensions.
pub fn fuse_sources(sources: &[DepthSource], config: &FusionConfig) -> Result<CompletionResult> {
    if sources.is_empty() {
        return Err(VisionError::InvalidParameter(
            "no depth sources provided".to_string(),
        ));
    }

    let (h, w) = sources[0].depth_map.dim();
    let mut fusion = KalmanDepthFusion::new(h, w, config.clone());

    for source in sources {
        fusion.update(source)?;
    }

    Ok(fusion.result())
}

// ─────────────────────────────────────────────────────────────────────────────
// confidence_weighted_average
// ─────────────────────────────────────────────────────────────────────────────

/// Simple confidence-weighted average of multiple depth sources.
///
/// For each pixel: `d = sum(conf_i * d_i) / sum(conf_i)`.
///
/// # Errors
/// Returns an error if no sources are provided or dimensions are inconsistent.
pub fn confidence_weighted_average(sources: &[DepthSource]) -> Result<CompletionResult> {
    if sources.is_empty() {
        return Err(VisionError::InvalidParameter(
            "no depth sources provided".to_string(),
        ));
    }

    let (h, w) = sources[0].depth_map.dim();
    let mut depth_sum: Array2<f64> = Array2::zeros((h, w));
    let mut weight_sum: Array2<f64> = Array2::zeros((h, w));

    for source in sources {
        let (sh, sw) = source.depth_map.dim();
        if sh != h || sw != w {
            return Err(VisionError::DimensionMismatch(format!(
                "source '{}' is {sh}x{sw} but expected {h}x{w}",
                source.source_id
            )));
        }

        for row in 0..h {
            for col in 0..w {
                let d = source.depth_map[[row, col]];
                if d <= 0.0 || !d.is_finite() {
                    continue;
                }
                let c = source.confidence[[row, col]].max(0.0);
                depth_sum[[row, col]] += c * d;
                weight_sum[[row, col]] += c;
            }
        }
    }

    let mut dense = Array2::zeros((h, w));
    let mut conf = Array2::zeros((h, w));

    for row in 0..h {
        for col in 0..w {
            let ws: f64 = weight_sum[[row, col]];
            if ws > 1e-12 {
                dense[[row, col]] = depth_sum[[row, col]] / ws;
                conf[[row, col]] = ws.min(1.0);
            }
        }
    }

    Ok(CompletionResult {
        dense_depth: dense,
        confidence_map: conf,
        method_used: CompletionMethod::ConfidenceWeightedAverage,
        iterations: 0,
    })
}

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

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

    fn make_source(id: &str, depth: f64, confidence: f64, noise: f64) -> DepthSource {
        DepthSource {
            source_id: id.to_string(),
            depth_map: Array2::from_elem((4, 4), depth),
            confidence: Array2::from_elem((4, 4), confidence),
            noise_variance: noise,
        }
    }

    #[test]
    fn kalman_two_identical_sources_reduces_variance() {
        let config = FusionConfig::default();
        let mut fusion = KalmanDepthFusion::new(4, 4, config);

        let source = make_source("s1", 5.0, 1.0, 0.1);
        fusion.update(&source).expect("ok");
        let var_after_one = fusion.state_var[[0, 0]];

        fusion.update(&source).expect("ok");
        let var_after_two = fusion.state_var[[0, 0]];

        assert!(
            var_after_two < var_after_one,
            "variance should decrease: {var_after_two} >= {var_after_one}"
        );
    }

    #[test]
    fn kalman_weights_by_inverse_noise() {
        let config = FusionConfig {
            outlier_threshold: 100.0, // no rejection
            process_noise: 0.0,
            measurement_noise: 0.1,
        };

        // Source A: depth=2, low noise
        let source_a = DepthSource {
            source_id: "a".to_string(),
            depth_map: Array2::from_elem((2, 2), 2.0),
            confidence: Array2::from_elem((2, 2), 1.0),
            noise_variance: 0.01,
        };
        // Source B: depth=8, high noise
        let source_b = DepthSource {
            source_id: "b".to_string(),
            depth_map: Array2::from_elem((2, 2), 8.0),
            confidence: Array2::from_elem((2, 2), 1.0),
            noise_variance: 10.0,
        };

        let result = fuse_sources(&[source_a, source_b], &config).expect("ok");
        let fused = result.dense_depth[[0, 0]];
        // Should be much closer to 2.0 (low noise) than 8.0 (high noise).
        assert!(
            fused < 5.0,
            "fused depth should favour low-noise source, got {fused}"
        );
    }

    #[test]
    fn outlier_rejected_at_5_sigma() {
        let config = FusionConfig {
            outlier_threshold: 3.0,
            process_noise: 0.0,
            measurement_noise: 0.1,
        };

        let mut fusion = KalmanDepthFusion::new(4, 4, config);

        // First source: depth=5
        let s1 = make_source("s1", 5.0, 1.0, 0.1);
        fusion.update(&s1).expect("ok");
        let after_s1 = fusion.state_mean[[0, 0]];

        // Second source: extreme outlier depth=500
        let s2 = make_source("s2", 500.0, 1.0, 0.1);
        fusion.update(&s2).expect("ok");
        let after_s2 = fusion.state_mean[[0, 0]];

        // The outlier should be rejected, so depth should stay near 5.0.
        assert!(
            (after_s2 - after_s1).abs() < 1.0,
            "outlier should be rejected: before={after_s1}, after={after_s2}"
        );
    }

    #[test]
    fn confidence_weighted_correct_mean() {
        let s1 = DepthSource {
            source_id: "s1".to_string(),
            depth_map: Array2::from_elem((2, 2), 4.0),
            confidence: Array2::from_elem((2, 2), 0.75),
            noise_variance: 0.1,
        };
        let s2 = DepthSource {
            source_id: "s2".to_string(),
            depth_map: Array2::from_elem((2, 2), 8.0),
            confidence: Array2::from_elem((2, 2), 0.25),
            noise_variance: 0.1,
        };

        let result = confidence_weighted_average(&[s1, s2]).expect("ok");
        // Expected: (0.75*4 + 0.25*8) / (0.75+0.25) = (3+2)/1 = 5.0
        let val = result.dense_depth[[0, 0]];
        assert!(
            (val - 5.0).abs() < 1e-9,
            "weighted mean should be 5.0, got {val}"
        );
    }

    #[test]
    fn single_source_passthrough() {
        let source = make_source("s", 7.0, 1.0, 0.1);
        let config = FusionConfig::default();
        let result = fuse_sources(&[source], &config).expect("ok");
        let val = result.dense_depth[[0, 0]];
        assert!(
            (val - 7.0).abs() < 0.01,
            "single source should pass through, got {val}"
        );
    }

    #[test]
    fn empty_sources_error() {
        let config = FusionConfig::default();
        assert!(fuse_sources(&[], &config).is_err());
        assert!(confidence_weighted_average(&[]).is_err());
    }

    #[test]
    fn config_defaults_reasonable() {
        let config = FusionConfig::default();
        assert!(config.outlier_threshold > 0.0);
        assert!(config.process_noise >= 0.0);
        assert!(config.measurement_noise > 0.0);

        let dc = super::super::types::DepthCompletionConfig::default();
        assert!(dc.sigma_spatial > 0.0);
        assert!(dc.sigma_intensity > 0.0);
        assert!(dc.tv_lambda > 0.0);
        assert!(dc.max_iterations > 0);
        assert!(dc.convergence_tol > 0.0);
    }

    #[test]
    fn dimension_mismatch_errors() {
        let s1 = make_source("s1", 5.0, 1.0, 0.1);
        let s2 = DepthSource {
            source_id: "s2".to_string(),
            depth_map: Array2::from_elem((3, 3), 5.0),
            confidence: Array2::from_elem((3, 3), 1.0),
            noise_variance: 0.1,
        };
        let config = FusionConfig::default();
        assert!(fuse_sources(&[s1, s2], &config).is_err());
    }
}