scirs2-vision 0.4.4

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
//! Camera models for 3D vision
//!
//! Provides pinhole camera models, radial/tangential distortion (Brown-Conrady),
//! and utilities for projecting 3-D points to 2-D pixels, unprojecting pixels to
//! 3-D rays, and full image undistortion.

use crate::error::{Result, VisionError};
use scirs2_core::ndarray::Array3;

// ─────────────────────────────────────────────────────────────────────────────
// PinholeCameraModel
// ─────────────────────────────────────────────────────────────────────────────

/// Pinhole camera intrinsic model.
///
/// # Coordinate convention
///
/// - **X** points right, **Y** points down, **Z** points forward (camera space).
/// - The image origin is at the top-left pixel centre.
///
/// Projection:  `u = fx * X/Z + cx`,  `v = fy * Y/Z + cy`.
#[derive(Debug, Clone, PartialEq)]
pub struct PinholeCameraModel {
    /// Focal length in pixels along the X axis.
    pub fx: f64,
    /// Focal length in pixels along the Y axis.
    pub fy: f64,
    /// Principal point X coordinate in pixels.
    pub cx: f64,
    /// Principal point Y coordinate in pixels.
    pub cy: f64,
    /// Image width in pixels.
    pub width: usize,
    /// Image height in pixels.
    pub height: usize,
}

impl PinholeCameraModel {
    /// Create a new pinhole camera model.
    pub fn new(fx: f64, fy: f64, cx: f64, cy: f64, width: usize, height: usize) -> Self {
        Self {
            fx,
            fy,
            cx,
            cy,
            width,
            height,
        }
    }

    /// Build from a 3×3 calibration matrix K.
    ///
    /// ```
    /// # use scirs2_vision::camera_model::PinholeCameraModel;
    /// let k = [[800.0, 0.0, 320.0], [0.0, 800.0, 240.0], [0.0, 0.0, 1.0]];
    /// let cam = PinholeCameraModel::from_calibration_matrix(&k, 640, 480);
    /// assert!((cam.fx - 800.0).abs() < 1e-9);
    /// ```
    pub fn from_calibration_matrix(k: &[[f64; 3]; 3], width: usize, height: usize) -> Self {
        Self {
            fx: k[0][0],
            fy: k[1][1],
            cx: k[0][2],
            cy: k[1][2],
            width,
            height,
        }
    }

    /// Return the intrinsic matrix K as a flat row-major 3×3 array.
    pub fn to_matrix(&self) -> [[f64; 3]; 3] {
        [
            [self.fx, 0.0, self.cx],
            [0.0, self.fy, self.cy],
            [0.0, 0.0, 1.0],
        ]
    }

    /// Horizontal field-of-view in radians.
    pub fn fov_x(&self) -> f64 {
        2.0 * (self.width as f64 / (2.0 * self.fx)).atan()
    }

    /// Vertical field-of-view in radians.
    pub fn fov_y(&self) -> f64 {
        2.0 * (self.height as f64 / (2.0 * self.fy)).atan()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// project / unproject
// ─────────────────────────────────────────────────────────────────────────────

/// Project a 3-D camera-space point to a 2-D pixel coordinate.
///
/// Returns `Err` when `Z ≤ 0` (point is behind or at the camera plane).
///
/// # Arguments
///
/// * `model`     – Pinhole camera intrinsics.
/// * `point_3d`  – `[X, Y, Z]` in camera space (Z > 0 required).
///
/// # Returns
///
/// `(u, v)` pixel coordinates.
///
/// # Example
///
/// ```
/// use scirs2_vision::camera_model::{PinholeCameraModel, project};
/// let cam = PinholeCameraModel::new(800.0, 800.0, 320.0, 240.0, 640, 480);
/// let (u, v) = project(&cam, &[0.0, 0.0, 1.0]).unwrap();
/// assert!((u - 320.0).abs() < 1e-9);
/// assert!((v - 240.0).abs() < 1e-9);
/// ```
pub fn project(model: &PinholeCameraModel, point_3d: &[f64; 3]) -> Result<(f64, f64)> {
    let z = point_3d[2];
    if z <= 0.0 {
        return Err(VisionError::InvalidParameter(
            "point_3d Z must be positive (point must be in front of camera)".to_string(),
        ));
    }
    let u = model.fx * point_3d[0] / z + model.cx;
    let v = model.fy * point_3d[1] / z + model.cy;
    Ok((u, v))
}

/// Unproject a pixel coordinate and depth to a 3-D camera-space point.
///
/// # Arguments
///
/// * `model`  – Pinhole camera intrinsics.
/// * `pixel`  – `(u, v)` pixel coordinate.
/// * `depth`  – Depth (Z value) in the same units as the desired 3-D output.
///
/// # Returns
///
/// `[X, Y, Z]` in camera space.
///
/// # Example
///
/// ```
/// use scirs2_vision::camera_model::{PinholeCameraModel, unproject};
/// let cam = PinholeCameraModel::new(800.0, 800.0, 320.0, 240.0, 640, 480);
/// let pt = unproject(&cam, (320.0, 240.0), 2.0);
/// assert!((pt[2] - 2.0).abs() < 1e-9);
/// ```
pub fn unproject(model: &PinholeCameraModel, pixel: (f64, f64), depth: f64) -> [f64; 3] {
    let (u, v) = pixel;
    let x = (u - model.cx) * depth / model.fx;
    let y = (v - model.cy) * depth / model.fy;
    [x, y, depth]
}

// ─────────────────────────────────────────────────────────────────────────────
// RadialDistortion  (Brown-Conrady model)
// ─────────────────────────────────────────────────────────────────────────────

/// Radial and tangential distortion coefficients (Brown-Conrady model).
///
/// The distortion model is:
///
/// ```text
/// r² = x² + y²
/// x_d = x(1 + k1 r² + k2 r⁴ + k3 r⁶) + 2 p1 xy + p2(r² + 2x²)
/// y_d = y(1 + k1 r² + k2 r⁴ + k3 r⁶) + p1(r² + 2y²) + 2 p2 xy
/// ```
///
/// where `(x, y) = ((u - cx)/fx, (v - cy)/fy)` are normalised image coordinates.
#[derive(Debug, Clone, PartialEq)]
pub struct RadialDistortion {
    /// Radial distortion coefficient 1.
    pub k1: f64,
    /// Radial distortion coefficient 2.
    pub k2: f64,
    /// Radial distortion coefficient 3.
    pub k3: f64,
    /// Tangential distortion coefficient 1.
    pub p1: f64,
    /// Tangential distortion coefficient 2.
    pub p2: f64,
}

impl Default for RadialDistortion {
    fn default() -> Self {
        Self {
            k1: 0.0,
            k2: 0.0,
            k3: 0.0,
            p1: 0.0,
            p2: 0.0,
        }
    }
}

impl RadialDistortion {
    /// Create a new distortion model with all coefficients.
    pub fn new(k1: f64, k2: f64, k3: f64, p1: f64, p2: f64) -> Self {
        Self { k1, k2, k3, p1, p2 }
    }

    /// Returns true when all coefficients are zero (no distortion).
    pub fn is_identity(&self) -> bool {
        self.k1 == 0.0 && self.k2 == 0.0 && self.k3 == 0.0 && self.p1 == 0.0 && self.p2 == 0.0
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// distort / undistort_point
// ─────────────────────────────────────────────────────────────────────────────

/// Apply radial and tangential distortion to a normalised image point `(x, y)`.
///
/// The input `point` should be in **normalised camera coordinates**
/// `x = (u - cx)/fx`,  `y = (v - cy)/fy`.
///
/// Returns the distorted normalised point `(x_d, y_d)`.
///
/// # Example
///
/// ```
/// use scirs2_vision::camera_model::{RadialDistortion, distort};
/// let d = RadialDistortion::new(0.1, 0.01, 0.0, 0.0, 0.0);
/// let (xd, yd) = distort(&d, (0.0, 0.0));
/// // At the principal point there is no distortion.
/// assert!((xd).abs() < 1e-12);
/// assert!((yd).abs() < 1e-12);
/// ```
pub fn distort(model: &RadialDistortion, point: (f64, f64)) -> (f64, f64) {
    let (x, y) = point;
    let r2 = x * x + y * y;
    let r4 = r2 * r2;
    let r6 = r4 * r2;

    let radial = 1.0 + model.k1 * r2 + model.k2 * r4 + model.k3 * r6;
    let x_d = x * radial + 2.0 * model.p1 * x * y + model.p2 * (r2 + 2.0 * x * x);
    let y_d = y * radial + model.p1 * (r2 + 2.0 * y * y) + 2.0 * model.p2 * x * y;
    (x_d, y_d)
}

/// Undistort a single normalised image point using iterative Newton refinement.
///
/// Inverts the Brown-Conrady distortion model numerically.
/// At most `max_iters` Newton steps are taken; the loop stops early when the
/// residual is below `tol`.
///
/// # Arguments
///
/// * `model`     – Distortion coefficients.
/// * `point`     – Observed (distorted) normalised point `(x_d, y_d)`.
/// * `max_iters` – Maximum number of Newton iterations (≥ 1).
/// * `tol`       – Convergence tolerance for the residual norm.
pub fn undistort_point(
    model: &RadialDistortion,
    point: (f64, f64),
    max_iters: usize,
    tol: f64,
) -> (f64, f64) {
    if model.is_identity() {
        return point;
    }

    let (xd, yd) = point;
    // Initial guess: use the distorted point directly.
    let mut x = xd;
    let mut y = yd;

    for _ in 0..max_iters.max(1) {
        let (x_proj, y_proj) = distort(model, (x, y));
        let ex = x_proj - xd;
        let ey = y_proj - yd;

        if ex * ex + ey * ey < tol * tol {
            break;
        }

        // Simple gradient-descent step (Jacobian ≈ I for small distortions).
        x -= ex;
        y -= ey;
    }

    (x, y)
}

// ─────────────────────────────────────────────────────────────────────────────
// undistort_image
// ─────────────────────────────────────────────────────────────────────────────

/// Produce an undistorted copy of `image` using the provided camera model and
/// distortion coefficients.
///
/// The function maps every destination pixel `(u, v)` back to a distorted source
/// position using the inverse of the Brown-Conrady model (solved iteratively), then
/// samples the source image with bilinear interpolation.
///
/// # Arguments
///
/// * `image`      – Source image as `Array3<f64>` with shape `[H, W, C]`.
///   Pixel values are expected in `[0, 255]`.
/// * `model`      – Pinhole camera intrinsics.
/// * `distortion` – Radial and tangential distortion coefficients.
///
/// # Returns
///
/// Undistorted image with the same shape as the input.
///
/// # Errors
///
/// Returns [`VisionError::InvalidParameter`] when the image dimensions do not
/// match the camera model's declared `width` / `height`.
///
/// # Example
///
/// ```
/// use scirs2_vision::camera_model::{PinholeCameraModel, RadialDistortion, undistort_image};
/// use scirs2_core::ndarray::Array3;
///
/// let cam = PinholeCameraModel::new(400.0, 400.0, 160.0, 120.0, 320, 240);
/// let dist = RadialDistortion::default(); // no distortion
/// let img = Array3::<f64>::zeros((240, 320, 3));
/// let out = undistort_image(&img, &cam, &dist).unwrap();
/// assert_eq!(out.dim(), img.dim());
/// ```
pub fn undistort_image(
    image: &Array3<f64>,
    model: &PinholeCameraModel,
    distortion: &RadialDistortion,
) -> Result<Array3<f64>> {
    let (h, w, c) = image.dim();
    if h != model.height || w != model.width {
        return Err(VisionError::InvalidParameter(format!(
            "image dimensions {}×{} do not match camera model {}×{}",
            h, w, model.height, model.width
        )));
    }

    let mut output = Array3::zeros((h, w, c));

    for v in 0..h {
        for u in 0..w {
            // Normalised destination coordinates.
            let xn = (u as f64 - model.cx) / model.fx;
            let yn = (v as f64 - model.cy) / model.fy;

            // Find corresponding distorted normalised source point.
            let (xd, yd) = distort(distortion, (xn, yn));

            // Back to pixel coordinates in the source (distorted) image.
            let us = model.fx * xd + model.cx;
            let vs = model.fy * yd + model.cy;

            // Bilinear interpolation.
            let u0 = us.floor() as i64;
            let v0 = vs.floor() as i64;
            let u1 = u0 + 1;
            let v1 = v0 + 1;

            let du = us - u0 as f64;
            let dv = vs - v0 as f64;

            let in_bounds = |uu: i64, vv: i64| uu >= 0 && uu < w as i64 && vv >= 0 && vv < h as i64;

            for ch in 0..c {
                let sample = |uu: i64, vv: i64| -> f64 {
                    if in_bounds(uu, vv) {
                        image[[vv as usize, uu as usize, ch]]
                    } else {
                        0.0
                    }
                };

                let val = sample(u0, v0) * (1.0 - du) * (1.0 - dv)
                    + sample(u1, v0) * du * (1.0 - dv)
                    + sample(u0, v1) * (1.0 - du) * dv
                    + sample(u1, v1) * du * dv;

                output[[v, u, ch]] = val;
            }
        }
    }

    Ok(output)
}

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

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

    #[test]
    fn test_project_principal_ray() {
        let cam = PinholeCameraModel::new(800.0, 800.0, 320.0, 240.0, 640, 480);
        let (u, v) =
            project(&cam, &[0.0, 0.0, 1.0]).expect("project should succeed for principal ray");
        assert!((u - 320.0).abs() < 1e-9, "u={u}");
        assert!((v - 240.0).abs() < 1e-9, "v={v}");
    }

    #[test]
    fn test_project_negative_z_errors() {
        let cam = PinholeCameraModel::new(800.0, 800.0, 320.0, 240.0, 640, 480);
        assert!(project(&cam, &[0.0, 0.0, -1.0]).is_err());
        assert!(project(&cam, &[0.0, 0.0, 0.0]).is_err());
    }

    #[test]
    fn test_unproject_roundtrip() {
        let cam = PinholeCameraModel::new(800.0, 800.0, 320.0, 240.0, 640, 480);
        let depth = 3.5;
        let (u, v) = project(&cam, &[0.5, -0.3, depth])
            .expect("project should succeed for point in front of camera");
        let pt = unproject(&cam, (u, v), depth);
        assert!((pt[0] - 0.5).abs() < 1e-9, "X={}", pt[0]);
        assert!((pt[1] - (-0.3)).abs() < 1e-9, "Y={}", pt[1]);
        assert!((pt[2] - depth).abs() < 1e-9, "Z={}", pt[2]);
    }

    #[test]
    fn test_distort_identity() {
        let d = RadialDistortion::default();
        let (xd, yd) = distort(&d, (0.3, 0.2));
        assert!((xd - 0.3).abs() < 1e-12);
        assert!((yd - 0.2).abs() < 1e-12);
    }

    #[test]
    fn test_distort_nonzero() {
        let d = RadialDistortion::new(0.1, 0.0, 0.0, 0.0, 0.0);
        let x = 0.5;
        let y = 0.0;
        let (xd, _yd) = distort(&d, (x, y));
        // r2 = 0.25, radial = 1 + 0.1*0.25 = 1.025
        assert!((xd - x * 1.025).abs() < 1e-12, "xd={xd}");
    }

    #[test]
    fn test_undistort_image_identity_distortion() {
        let cam = PinholeCameraModel::new(400.0, 400.0, 160.0, 120.0, 320, 240);
        let dist = RadialDistortion::default();
        let img = Array3::from_elem((240, 320, 3), 128.0_f64);
        let out = undistort_image(&img, &cam, &dist)
            .expect("undistort_image should succeed with identity distortion");
        assert_eq!(out.dim(), img.dim());
        // With no distortion the output should be identical to the input (bilinear interpolation of uniform image).
        let err: f64 = (&out - &img).iter().map(|x| x.abs()).sum::<f64>() / (240 * 320 * 3) as f64;
        assert!(err < 1.0, "mean error = {err}");
    }

    #[test]
    fn test_undistort_image_wrong_size() {
        let cam = PinholeCameraModel::new(400.0, 400.0, 160.0, 120.0, 320, 240);
        let dist = RadialDistortion::default();
        let img = Array3::<f64>::zeros((100, 100, 3));
        assert!(undistort_image(&img, &cam, &dist).is_err());
    }

    #[test]
    fn test_fov() {
        let cam = PinholeCameraModel::new(800.0, 800.0, 320.0, 240.0, 640, 480);
        let fov_x = cam.fov_x();
        let fov_y = cam.fov_y();
        // Rough sanity: should be in (0, π)
        assert!(fov_x > 0.0 && fov_x < std::f64::consts::PI);
        assert!(fov_y > 0.0 && fov_y < std::f64::consts::PI);
    }

    #[test]
    fn test_to_matrix_roundtrip() {
        let cam = PinholeCameraModel::new(750.0, 760.0, 319.5, 239.5, 640, 480);
        let k = cam.to_matrix();
        assert!((k[0][0] - cam.fx).abs() < 1e-12);
        assert!((k[1][1] - cam.fy).abs() < 1e-12);
        assert!((k[0][2] - cam.cx).abs() < 1e-12);
        assert!((k[1][2] - cam.cy).abs() < 1e-12);
    }
}