oximedia-cv 0.1.8

Computer vision for OxiMedia
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
646
647
648
649
//! Perspective transformation module.
//!
//! This module provides perspective (projective) transformations
//! and homography computation for applications like image warping
//! and perspective correction.
//!
//! # Example
//!
//! ```
//! use oximedia_cv::transform::PerspectiveTransform;
//!
//! let transform = PerspectiveTransform::identity();
//! let (x, y) = transform.transform_point(10.0, 20.0);
//! ```

use crate::error::{CvError, CvResult};

/// 2D perspective (projective) transformation matrix (3x3).
///
/// The matrix is:
/// ```text
/// | h00  h01  h02 |
/// | h10  h11  h12 |
/// | h20  h21  h22 |
/// ```
///
/// A point (x, y) is transformed to (x', y') by:
/// ```text
/// w = h20*x + h21*y + h22
/// x' = (h00*x + h01*y + h02) / w
/// y' = (h10*x + h11*y + h12) / w
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PerspectiveTransform {
    /// Transformation matrix elements (row-major).
    pub matrix: [[f64; 3]; 3],
}

impl PerspectiveTransform {
    /// Create a new perspective transform from matrix elements.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::transform::PerspectiveTransform;
    ///
    /// let transform = PerspectiveTransform::new([
    ///     [1.0, 0.0, 0.0],
    ///     [0.0, 1.0, 0.0],
    ///     [0.0, 0.0, 1.0],
    /// ]);
    /// ```
    #[must_use]
    pub const fn new(matrix: [[f64; 3]; 3]) -> Self {
        Self { matrix }
    }

    /// Create an identity transform.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::transform::PerspectiveTransform;
    ///
    /// let transform = PerspectiveTransform::identity();
    /// let (x, y) = transform.transform_point(10.0, 20.0);
    /// assert!((x - 10.0).abs() < 0.001);
    /// ```
    #[must_use]
    pub const fn identity() -> Self {
        Self::new([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
    }

    /// Transform a point.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::transform::PerspectiveTransform;
    ///
    /// let transform = PerspectiveTransform::identity();
    /// let (x, y) = transform.transform_point(10.0, 20.0);
    /// assert!((x - 10.0).abs() < 0.001);
    /// ```
    #[must_use]
    pub fn transform_point(&self, x: f64, y: f64) -> (f64, f64) {
        let m = &self.matrix;

        let w = m[2][0] * x + m[2][1] * y + m[2][2];
        if w.abs() < f64::EPSILON {
            return (f64::NAN, f64::NAN);
        }

        let x_new = (m[0][0] * x + m[0][1] * y + m[0][2]) / w;
        let y_new = (m[1][0] * x + m[1][1] * y + m[1][2]) / w;

        (x_new, y_new)
    }

    /// Transform multiple points.
    #[must_use]
    pub fn transform_points(&self, points: &[(f64, f64)]) -> Vec<(f64, f64)> {
        points
            .iter()
            .map(|&(x, y)| self.transform_point(x, y))
            .collect()
    }

    /// Calculate the determinant of the matrix.
    #[must_use]
    pub fn determinant(&self) -> f64 {
        let m = &self.matrix;

        m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
            - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
            + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
    }

    /// Check if the transform is invertible.
    #[must_use]
    pub fn is_invertible(&self) -> bool {
        self.determinant().abs() > f64::EPSILON
    }

    /// Calculate the inverse transform.
    ///
    /// # Returns
    ///
    /// The inverse transform, or None if the transform is singular.
    #[must_use]
    pub fn inverse(&self) -> Option<Self> {
        let det = self.determinant();
        if det.abs() <= f64::EPSILON {
            return None;
        }

        let m = &self.matrix;
        let inv_det = 1.0 / det;

        // Compute adjugate matrix and divide by determinant
        let inv = [
            [
                (m[1][1] * m[2][2] - m[1][2] * m[2][1]) * inv_det,
                (m[0][2] * m[2][1] - m[0][1] * m[2][2]) * inv_det,
                (m[0][1] * m[1][2] - m[0][2] * m[1][1]) * inv_det,
            ],
            [
                (m[1][2] * m[2][0] - m[1][0] * m[2][2]) * inv_det,
                (m[0][0] * m[2][2] - m[0][2] * m[2][0]) * inv_det,
                (m[0][2] * m[1][0] - m[0][0] * m[1][2]) * inv_det,
            ],
            [
                (m[1][0] * m[2][1] - m[1][1] * m[2][0]) * inv_det,
                (m[0][1] * m[2][0] - m[0][0] * m[2][1]) * inv_det,
                (m[0][0] * m[1][1] - m[0][1] * m[1][0]) * inv_det,
            ],
        ];

        Some(Self::new(inv))
    }

    /// Compose this transform with another (other * this).
    ///
    /// The resulting transform first applies `self`, then `other`.
    #[must_use]
    pub fn then(&self, other: &Self) -> Self {
        let a = &other.matrix;
        let b = &self.matrix;

        let mut result = [[0.0; 3]; 3];

        for i in 0..3 {
            for j in 0..3 {
                result[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
            }
        }

        Self::new(result)
    }

    /// Normalize the matrix so that h22 = 1 (if non-zero).
    #[must_use]
    pub fn normalized(&self) -> Self {
        let h22 = self.matrix[2][2];
        if h22.abs() < f64::EPSILON {
            return *self;
        }

        let mut result = self.matrix;
        for row in &mut result {
            for elem in row {
                *elem /= h22;
            }
        }

        Self::new(result)
    }
}

impl Default for PerspectiveTransform {
    fn default() -> Self {
        Self::identity()
    }
}

/// Find homography matrix that maps source points to destination points.
///
/// Uses the Direct Linear Transform (DLT) algorithm to compute
/// the 3x3 homography matrix.
///
/// # Arguments
///
/// * `src_points` - Source points (at least 4)
/// * `dst_points` - Destination points (same length as `src_points`)
///
/// # Returns
///
/// Estimated perspective transform.
///
/// # Errors
///
/// Returns an error if fewer than 4 point pairs are provided.
///
/// # Examples
///
/// ```
/// use oximedia_cv::transform::perspective::find_homography;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let src = vec![(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
/// let dst = vec![(10.0, 10.0), (90.0, 10.0), (90.0, 90.0), (10.0, 90.0)];
///
/// let transform = find_homography(&src, &dst)?;
/// Ok(())
/// }
/// ```
pub fn find_homography(
    src_points: &[(f64, f64)],
    dst_points: &[(f64, f64)],
) -> CvResult<PerspectiveTransform> {
    if src_points.len() < 4 || dst_points.len() < 4 {
        return Err(CvError::invalid_parameter(
            "points",
            "need at least 4 point pairs",
        ));
    }

    if src_points.len() != dst_points.len() {
        return Err(CvError::invalid_parameter(
            "points",
            "source and destination must have same length",
        ));
    }

    let n = src_points.len();

    // Build the 2n x 9 matrix A for DLT
    // Each point pair contributes 2 rows:
    // [-x, -y, -1, 0, 0, 0, x*x', y*x', x']
    // [0, 0, 0, -x, -y, -1, x*y', y*y', y']

    // For simplicity, we solve the 8x8 system (fixing h22 = 1)
    // This works well for most cases

    let mut a = [[0.0; 8]; 8];
    let mut b = [0.0; 8];

    // Use first 4 points (exactly determined system)
    for i in 0..4.min(n) {
        let (sx, sy) = src_points[i];
        let (dx, dy) = dst_points[i];

        let row_x = 2 * i;
        let row_y = 2 * i + 1;

        // x' = (h00*x + h01*y + h02) / (h20*x + h21*y + 1)
        // x'*(h20*x + h21*y + 1) = h00*x + h01*y + h02
        // h00*x + h01*y + h02 - x'*h20*x - x'*h21*y = x'

        a[row_x][0] = sx;
        a[row_x][1] = sy;
        a[row_x][2] = 1.0;
        a[row_x][3] = 0.0;
        a[row_x][4] = 0.0;
        a[row_x][5] = 0.0;
        a[row_x][6] = -dx * sx;
        a[row_x][7] = -dx * sy;
        b[row_x] = dx;

        // y' = (h10*x + h11*y + h12) / (h20*x + h21*y + 1)
        a[row_y][0] = 0.0;
        a[row_y][1] = 0.0;
        a[row_y][2] = 0.0;
        a[row_y][3] = sx;
        a[row_y][4] = sy;
        a[row_y][5] = 1.0;
        a[row_y][6] = -dy * sx;
        a[row_y][7] = -dy * sy;
        b[row_y] = dy;
    }

    // Solve using Gaussian elimination
    let h = solve_8x8(&a, &b)?;

    let matrix = [[h[0], h[1], h[2]], [h[3], h[4], h[5]], [h[6], h[7], 1.0]];

    Ok(PerspectiveTransform::new(matrix))
}

/// Solve an 8x8 linear system using Gaussian elimination.
fn solve_8x8(a: &[[f64; 8]; 8], b: &[f64; 8]) -> CvResult<[f64; 8]> {
    let mut aug = [[0.0; 9]; 8];

    // Build augmented matrix
    for i in 0..8 {
        for j in 0..8 {
            aug[i][j] = a[i][j];
        }
        aug[i][8] = b[i];
    }

    // Forward elimination with partial pivoting
    for i in 0..8 {
        // Find pivot
        let mut max_row = i;
        for k in (i + 1)..8 {
            if aug[k][i].abs() > aug[max_row][i].abs() {
                max_row = k;
            }
        }

        // Swap rows
        aug.swap(i, max_row);

        if aug[i][i].abs() < f64::EPSILON {
            return Err(CvError::transform_error("Matrix is singular"));
        }

        // Eliminate column
        for k in (i + 1)..8 {
            let factor = aug[k][i] / aug[i][i];
            for j in i..9 {
                aug[k][j] -= factor * aug[i][j];
            }
        }
    }

    // Back substitution
    let mut x = [0.0; 8];
    for i in (0..8).rev() {
        x[i] = aug[i][8];
        for j in (i + 1)..8 {
            x[i] -= aug[i][j] * x[j];
        }
        x[i] /= aug[i][i];
    }

    Ok(x)
}

/// Apply perspective warp to an image.
///
/// # Arguments
///
/// * `src` - Source grayscale image data
/// * `src_width` - Source image width
/// * `src_height` - Source image height
/// * `transform` - The perspective transformation
/// * `dst_width` - Output image width
/// * `dst_height` - Output image height
///
/// # Returns
///
/// Warped image data.
///
/// # Errors
///
/// Returns an error if dimensions are invalid or transform is not invertible.
///
/// # Examples
///
/// ```
/// use oximedia_cv::transform::{PerspectiveTransform, perspective::warp_perspective};
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let src = vec![100u8; 100];
/// let transform = PerspectiveTransform::identity();
/// let result = warp_perspective(&src, 10, 10, &transform, 10, 10)?;
/// Ok(())
/// }
/// ```
pub fn warp_perspective(
    src: &[u8],
    src_width: u32,
    src_height: u32,
    transform: &PerspectiveTransform,
    dst_width: u32,
    dst_height: u32,
) -> CvResult<Vec<u8>> {
    if src_width == 0 || src_height == 0 {
        return Err(CvError::invalid_dimensions(src_width, src_height));
    }
    if dst_width == 0 || dst_height == 0 {
        return Err(CvError::invalid_dimensions(dst_width, dst_height));
    }

    let expected_size = src_width as usize * src_height as usize;
    if src.len() < expected_size {
        return Err(CvError::insufficient_data(expected_size, src.len()));
    }

    let inv_transform = transform
        .inverse()
        .ok_or_else(|| CvError::transform_error("Transform is not invertible"))?;

    let dst_size = dst_width as usize * dst_height as usize;
    let mut dst = vec![0u8; dst_size];

    for dy in 0..dst_height {
        for dx in 0..dst_width {
            let (sx, sy) = inv_transform.transform_point(dx as f64, dy as f64);

            if sx.is_nan() || sy.is_nan() {
                continue;
            }

            // Bilinear interpolation
            let pixel = sample_bilinear(src, src_width, src_height, sx, sy);
            dst[dy as usize * dst_width as usize + dx as usize] = pixel;
        }
    }

    Ok(dst)
}

/// Sample image using bilinear interpolation.
fn sample_bilinear(src: &[u8], width: u32, height: u32, x: f64, y: f64) -> u8 {
    if x < 0.0 || y < 0.0 || x >= width as f64 - 1.0 || y >= height as f64 - 1.0 {
        // Out of bounds - use nearest neighbor for edge handling
        let sx = x.round().clamp(0.0, (width - 1) as f64) as usize;
        let sy = y.round().clamp(0.0, (height - 1) as f64) as usize;

        if sx < width as usize && sy < height as usize {
            return src[sy * width as usize + sx];
        }
        return 0;
    }

    let x0 = x.floor() as usize;
    let y0 = y.floor() as usize;
    let x1 = x0 + 1;
    let y1 = y0 + 1;

    let fx = x - x0 as f64;
    let fy = y - y0 as f64;

    let w = width as usize;

    let p00 = src[y0 * w + x0] as f64;
    let p10 = src[y0 * w + x1] as f64;
    let p01 = src[y1 * w + x0] as f64;
    let p11 = src[y1 * w + x1] as f64;

    let top = p00 * (1.0 - fx) + p10 * fx;
    let bottom = p01 * (1.0 - fx) + p11 * fx;
    let value = top * (1.0 - fy) + bottom * fy;

    value.round().clamp(0.0, 255.0) as u8
}

/// Get the perspective transform for a quadrilateral to rectangle mapping.
///
/// Useful for perspective correction (e.g., document scanning).
///
/// # Arguments
///
/// * `quad` - Four corners of the source quadrilateral (top-left, top-right, bottom-right, bottom-left)
/// * `rect_width` - Target rectangle width
/// * `rect_height` - Target rectangle height
///
/// # Returns
///
/// Perspective transform that maps the quadrilateral to a rectangle.
///
/// # Errors
///
/// Returns an error if the homography cannot be computed.
pub fn quad_to_rect(
    quad: &[(f64, f64); 4],
    rect_width: f64,
    rect_height: f64,
) -> CvResult<PerspectiveTransform> {
    let src = quad.to_vec();
    let dst = vec![
        (0.0, 0.0),
        (rect_width, 0.0),
        (rect_width, rect_height),
        (0.0, rect_height),
    ];

    find_homography(&src, &dst)
}

/// Get the perspective transform for a rectangle to quadrilateral mapping.
///
/// # Arguments
///
/// * `rect_width` - Source rectangle width
/// * `rect_height` - Source rectangle height
/// * `quad` - Four corners of the target quadrilateral
///
/// # Returns
///
/// Perspective transform that maps a rectangle to the quadrilateral.
///
/// # Errors
///
/// Returns an error if the homography cannot be computed.
pub fn rect_to_quad(
    rect_width: f64,
    rect_height: f64,
    quad: &[(f64, f64); 4],
) -> CvResult<PerspectiveTransform> {
    let src = vec![
        (0.0, 0.0),
        (rect_width, 0.0),
        (rect_width, rect_height),
        (0.0, rect_height),
    ];
    let dst = quad.to_vec();

    find_homography(&src, &dst)
}

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

    #[test]
    fn test_identity() {
        let transform = PerspectiveTransform::identity();
        let (x, y) = transform.transform_point(10.0, 20.0);
        assert!((x - 10.0).abs() < 0.001);
        assert!((y - 20.0).abs() < 0.001);
    }

    #[test]
    fn test_determinant() {
        let transform = PerspectiveTransform::identity();
        assert!((transform.determinant() - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_inverse() {
        let transform =
            PerspectiveTransform::new([[2.0, 0.0, 10.0], [0.0, 2.0, 20.0], [0.0, 0.0, 1.0]]);

        let inverse = transform.inverse().expect("inverse should succeed");

        let (x, y) = transform.transform_point(5.0, 5.0);
        let (rx, ry) = inverse.transform_point(x, y);

        assert!((rx - 5.0).abs() < 0.001);
        assert!((ry - 5.0).abs() < 0.001);
    }

    #[test]
    fn test_compose() {
        let t1 = PerspectiveTransform::new([[1.0, 0.0, 10.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]);
        let t2 = PerspectiveTransform::new([[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 1.0]]);

        let composed = t1.then(&t2);
        let (x, y) = composed.transform_point(5.0, 5.0);

        // First translate: (15, 5)
        // Then scale: (30, 10)
        assert!((x - 30.0).abs() < 0.001);
        assert!((y - 10.0).abs() < 0.001);
    }

    #[test]
    fn test_find_homography() {
        let src = vec![(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
        let dst = vec![(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];

        let transform = find_homography(&src, &dst).expect("find_homography should succeed");

        // Should be close to identity
        let (x, y) = transform.transform_point(50.0, 50.0);
        assert!((x - 50.0).abs() < 0.01);
        assert!((y - 50.0).abs() < 0.01);
    }

    #[test]
    fn test_find_homography_scale() {
        let src = vec![(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
        let dst = vec![(0.0, 0.0), (200.0, 0.0), (200.0, 200.0), (0.0, 200.0)];

        let transform = find_homography(&src, &dst).expect("find_homography should succeed");

        // Should scale by 2
        let (x, y) = transform.transform_point(50.0, 50.0);
        assert!((x - 100.0).abs() < 0.01);
        assert!((y - 100.0).abs() < 0.01);
    }

    #[test]
    fn test_warp_perspective() {
        let src = vec![100u8; 100];
        let transform = PerspectiveTransform::identity();
        let result = warp_perspective(&src, 10, 10, &transform, 10, 10)
            .expect("warp_perspective should succeed");
        assert_eq!(result.len(), 100);
    }

    #[test]
    fn test_quad_to_rect() {
        let quad = [(10.0, 10.0), (90.0, 10.0), (90.0, 90.0), (10.0, 90.0)];

        let transform = quad_to_rect(&quad, 100.0, 100.0).expect("quad_to_rect should succeed");

        // Top-left corner should map to (0, 0)
        let (x, y) = transform.transform_point(10.0, 10.0);
        assert!(x.abs() < 0.1);
        assert!(y.abs() < 0.1);

        // Bottom-right should map to (100, 100)
        let (x, y) = transform.transform_point(90.0, 90.0);
        assert!((x - 100.0).abs() < 0.1);
        assert!((y - 100.0).abs() < 0.1);
    }

    #[test]
    fn test_normalized() {
        let transform =
            PerspectiveTransform::new([[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]]);

        let normalized = transform.normalized();
        assert!((normalized.matrix[2][2] - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_insufficient_points() {
        let src = vec![(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)];
        let dst = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)];

        assert!(find_homography(&src, &dst).is_err());
    }
}