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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! Object detection module.
//!
//! This module provides generic object detection utilities including:
//! - Bounding box operations
//! - Non-Maximum Suppression (NMS)
//! - Intersection over Union (`IoU`)
//!
//! # Example
//!
//! ```
//! use oximedia_cv::detect::{BoundingBox, Detection};
//!
//! let bbox = BoundingBox::new(10.0, 20.0, 100.0, 150.0);
//! let detection = Detection::new(bbox, 0, 0.95);
//! ```

use crate::error::CvResult;

/// Trait for object detection algorithms.
pub trait ObjectDetector {
    /// Detect objects in an image.
    ///
    /// # Arguments
    ///
    /// * `image` - Image data (format depends on implementation)
    /// * `width` - Image width
    /// * `height` - Image height
    ///
    /// # Returns
    ///
    /// Vector of detections.
    ///
    /// # Errors
    ///
    /// Returns an error if detection fails.
    fn detect(&mut self, image: &[u8], width: u32, height: u32) -> CvResult<Vec<Detection>>;

    /// Get class names supported by this detector.
    fn class_names(&self) -> &[String];
}

/// Bounding box representation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BoundingBox {
    /// X coordinate of top-left corner.
    pub x: f32,
    /// Y coordinate of top-left corner.
    pub y: f32,
    /// Width of the box.
    pub width: f32,
    /// Height of the box.
    pub height: f32,
}

impl BoundingBox {
    /// Create a new bounding box.
    ///
    /// # Arguments
    ///
    /// * `x` - X coordinate of top-left corner
    /// * `y` - Y coordinate of top-left corner
    /// * `width` - Box width
    /// * `height` - Box height
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::new(10.0, 20.0, 100.0, 150.0);
    /// assert_eq!(bbox.x, 10.0);
    /// ```
    #[must_use]
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Create a bounding box from center coordinates.
    ///
    /// # Arguments
    ///
    /// * `cx` - Center X coordinate
    /// * `cy` - Center Y coordinate
    /// * `width` - Box width
    /// * `height` - Box height
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::from_center(50.0, 50.0, 100.0, 100.0);
    /// assert_eq!(bbox.x, 0.0);
    /// ```
    #[must_use]
    pub fn from_center(cx: f32, cy: f32, width: f32, height: f32) -> Self {
        Self {
            x: cx - width / 2.0,
            y: cy - height / 2.0,
            width,
            height,
        }
    }

    /// Create a bounding box from two corner points.
    ///
    /// # Arguments
    ///
    /// * `x1` - X coordinate of first corner
    /// * `y1` - Y coordinate of first corner
    /// * `x2` - X coordinate of opposite corner
    /// * `y2` - Y coordinate of opposite corner
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::from_corners(10.0, 20.0, 110.0, 170.0);
    /// assert_eq!(bbox.width, 100.0);
    /// ```
    #[must_use]
    pub fn from_corners(x1: f32, y1: f32, x2: f32, y2: f32) -> Self {
        let min_x = x1.min(x2);
        let min_y = y1.min(y2);
        let max_x = x1.max(x2);
        let max_y = y1.max(y2);

        Self {
            x: min_x,
            y: min_y,
            width: max_x - min_x,
            height: max_y - min_y,
        }
    }

    /// Get the area of the bounding box.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::new(0.0, 0.0, 10.0, 20.0);
    /// assert_eq!(bbox.area(), 200.0);
    /// ```
    #[must_use]
    pub fn area(&self) -> f32 {
        self.width * self.height
    }

    /// Get the center point of the bounding box.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
    /// let (cx, cy) = bbox.center();
    /// assert_eq!(cx, 50.0);
    /// ```
    #[must_use]
    pub fn center(&self) -> (f32, f32) {
        (self.x + self.width / 2.0, self.y + self.height / 2.0)
    }

    /// Get the right edge coordinate.
    #[must_use]
    pub fn right(&self) -> f32 {
        self.x + self.width
    }

    /// Get the bottom edge coordinate.
    #[must_use]
    pub fn bottom(&self) -> f32 {
        self.y + self.height
    }

    /// Check if this box contains a point.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
    /// assert!(bbox.contains(50.0, 50.0));
    /// assert!(!bbox.contains(150.0, 50.0));
    /// ```
    #[must_use]
    pub fn contains(&self, px: f32, py: f32) -> bool {
        px >= self.x && px <= self.right() && py >= self.y && py <= self.bottom()
    }

    /// Check if this box intersects with another.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
    /// let b = BoundingBox::new(50.0, 50.0, 100.0, 100.0);
    /// assert!(a.intersects(&b));
    /// ```
    #[must_use]
    pub fn intersects(&self, other: &Self) -> bool {
        self.x < other.right()
            && self.right() > other.x
            && self.y < other.bottom()
            && self.bottom() > other.y
    }

    /// Get the intersection box with another bounding box.
    ///
    /// Returns None if the boxes don't intersect.
    #[must_use]
    pub fn intersection(&self, other: &Self) -> Option<Self> {
        let x1 = self.x.max(other.x);
        let y1 = self.y.max(other.y);
        let x2 = self.right().min(other.right());
        let y2 = self.bottom().min(other.bottom());

        if x2 > x1 && y2 > y1 {
            Some(Self::new(x1, y1, x2 - x1, y2 - y1))
        } else {
            None
        }
    }

    /// Get the union box with another bounding box.
    #[must_use]
    pub fn union(&self, other: &Self) -> Self {
        let x1 = self.x.min(other.x);
        let y1 = self.y.min(other.y);
        let x2 = self.right().max(other.right());
        let y2 = self.bottom().max(other.bottom());

        Self::new(x1, y1, x2 - x1, y2 - y1)
    }

    /// Scale the bounding box by a factor.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
    /// let scaled = bbox.scale(0.5);
    /// assert_eq!(scaled.width, 50.0);
    /// ```
    #[must_use]
    pub fn scale(&self, factor: f32) -> Self {
        Self {
            x: self.x * factor,
            y: self.y * factor,
            width: self.width * factor,
            height: self.height * factor,
        }
    }

    /// Expand the bounding box by a margin.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::BoundingBox;
    ///
    /// let bbox = BoundingBox::new(10.0, 10.0, 80.0, 80.0);
    /// let expanded = bbox.expand(5.0);
    /// assert_eq!(expanded.x, 5.0);
    /// assert_eq!(expanded.width, 90.0);
    /// ```
    #[must_use]
    pub fn expand(&self, margin: f32) -> Self {
        Self {
            x: self.x - margin,
            y: self.y - margin,
            width: self.width + 2.0 * margin,
            height: self.height + 2.0 * margin,
        }
    }

    /// Clamp the bounding box to image bounds.
    #[must_use]
    pub fn clamp(&self, img_width: f32, img_height: f32) -> Self {
        let x = self.x.max(0.0);
        let y = self.y.max(0.0);
        let right = self.right().min(img_width);
        let bottom = self.bottom().min(img_height);

        Self {
            x,
            y,
            width: (right - x).max(0.0),
            height: (bottom - y).max(0.0),
        }
    }
}

impl Default for BoundingBox {
    fn default() -> Self {
        Self::new(0.0, 0.0, 0.0, 0.0)
    }
}

/// Object detection result.
#[derive(Debug, Clone)]
pub struct Detection {
    /// Bounding box of the detected object.
    pub bbox: BoundingBox,
    /// Class ID.
    pub class_id: u32,
    /// Detection confidence (0.0 - 1.0).
    pub confidence: f32,
    /// Optional class name.
    pub class_name: Option<String>,
}

impl Detection {
    /// Create a new detection.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::detect::{BoundingBox, Detection};
    ///
    /// let bbox = BoundingBox::new(10.0, 20.0, 100.0, 100.0);
    /// let detection = Detection::new(bbox, 0, 0.95);
    /// ```
    #[must_use]
    pub fn new(bbox: BoundingBox, class_id: u32, confidence: f32) -> Self {
        Self {
            bbox,
            class_id,
            confidence,
            class_name: None,
        }
    }

    /// Create a detection with a class name.
    #[must_use]
    pub fn with_class_name(mut self, name: impl Into<String>) -> Self {
        self.class_name = Some(name.into());
        self
    }
}

/// Calculate Intersection over Union (`IoU`) between two bounding boxes.
///
/// # Arguments
///
/// * `a` - First bounding box
/// * `b` - Second bounding box
///
/// # Returns
///
/// `IoU` value between 0.0 and 1.0.
///
/// # Examples
///
/// ```
/// use oximedia_cv::detect::{BoundingBox, object::iou};
///
/// let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
/// let b = BoundingBox::new(50.0, 50.0, 100.0, 100.0);
/// let overlap = iou(&a, &b);
/// assert!(overlap > 0.0 && overlap < 1.0);
/// ```
#[must_use]
pub fn iou(a: &BoundingBox, b: &BoundingBox) -> f32 {
    if let Some(intersection) = a.intersection(b) {
        let intersection_area = intersection.area();
        let union_area = a.area() + b.area() - intersection_area;

        if union_area > 0.0 {
            intersection_area / union_area
        } else {
            0.0
        }
    } else {
        0.0
    }
}

/// Calculate Generalized `IoU` (`GIoU`) between two bounding boxes.
///
/// `GIoU` is a more robust metric that handles non-overlapping boxes better.
///
/// # Returns
///
/// `GIoU` value between -1.0 and 1.0.
#[must_use]
pub fn giou(a: &BoundingBox, b: &BoundingBox) -> f32 {
    let intersection_area = a.intersection(b).map_or(0.0, |i| i.area());
    let union_area = a.area() + b.area() - intersection_area;

    if union_area <= 0.0 {
        return -1.0;
    }

    let iou_val = intersection_area / union_area;

    // Enclosing box
    let enclosing = a.union(b);
    let enclosing_area = enclosing.area();

    if enclosing_area <= 0.0 {
        return iou_val;
    }

    // GIoU = IoU - (C - U) / C
    // where C is enclosing area and U is union area
    iou_val - (enclosing_area - union_area) / enclosing_area
}

/// Perform Non-Maximum Suppression on detections.
///
/// # Arguments
///
/// * `detections` - Input detections
/// * `iou_threshold` - `IoU` threshold for suppression (typically 0.45-0.5)
///
/// # Returns
///
/// Filtered detections after NMS.
///
/// # Examples
///
/// ```
/// use oximedia_cv::detect::{BoundingBox, Detection, object::nms};
///
/// let detections = vec![
///     Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.9),
///     Detection::new(BoundingBox::new(10.0, 10.0, 100.0, 100.0), 0, 0.8),
/// ];
/// let filtered = nms(&detections, 0.5);
/// // The second detection should be suppressed due to high overlap
/// ```
#[must_use]
pub fn nms(detections: &[Detection], iou_threshold: f32) -> Vec<Detection> {
    if detections.is_empty() {
        return Vec::new();
    }

    // Sort by confidence (descending)
    let mut sorted: Vec<_> = detections.iter().enumerate().collect();
    sorted.sort_by(|a, b| {
        b.1.confidence
            .partial_cmp(&a.1.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let mut keep = vec![true; detections.len()];
    let mut result = Vec::new();

    for i in 0..sorted.len() {
        let (orig_idx, detection) = sorted[i];
        if !keep[orig_idx] {
            continue;
        }

        result.push(detection.clone());

        // Suppress overlapping detections with lower confidence
        for j in (i + 1)..sorted.len() {
            let (other_idx, other) = sorted[j];
            if !keep[other_idx] {
                continue;
            }

            // Only suppress same class
            if detection.class_id == other.class_id
                && iou(&detection.bbox, &other.bbox) > iou_threshold
            {
                keep[other_idx] = false;
            }
        }
    }

    result
}

/// Perform soft NMS on detections.
///
/// Instead of completely suppressing overlapping detections,
/// soft NMS reduces their confidence scores.
///
/// # Arguments
///
/// * `detections` - Input detections
/// * `iou_threshold` - `IoU` threshold for soft suppression
/// * `sigma` - Gaussian sigma for score decay
/// * `score_threshold` - Minimum score to keep a detection
///
/// # Returns
///
/// Detections with adjusted confidence scores.
#[must_use]
pub fn soft_nms(
    detections: &[Detection],
    iou_threshold: f32,
    sigma: f32,
    score_threshold: f32,
) -> Vec<Detection> {
    if detections.is_empty() {
        return Vec::new();
    }

    let mut detections = detections.to_vec();
    let mut result = Vec::new();

    while !detections.is_empty() {
        // Find detection with highest confidence
        // detections is non-empty (loop condition), so max_by always yields Some
        let max_idx = detections
            .iter()
            .enumerate()
            .max_by(|a, b| {
                a.1.confidence
                    .partial_cmp(&b.1.confidence)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(i, _)| i)
            .unwrap_or(0);

        let best = detections.remove(max_idx);
        result.push(best.clone());

        // Update scores of remaining detections
        for det in &mut detections {
            if det.class_id == best.class_id {
                let overlap = iou(&best.bbox, &det.bbox);
                if overlap > iou_threshold {
                    // Gaussian decay
                    det.confidence *= (-overlap * overlap / sigma).exp();
                }
            }
        }

        // Remove low-confidence detections
        detections.retain(|d| d.confidence >= score_threshold);
    }

    result
}

/// Filter detections by confidence threshold.
#[must_use]
pub fn filter_by_confidence(detections: &[Detection], threshold: f32) -> Vec<Detection> {
    detections
        .iter()
        .filter(|d| d.confidence >= threshold)
        .cloned()
        .collect()
}

/// Filter detections by class ID.
#[must_use]
pub fn filter_by_class(detections: &[Detection], class_id: u32) -> Vec<Detection> {
    detections
        .iter()
        .filter(|d| d.class_id == class_id)
        .cloned()
        .collect()
}

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

    #[test]
    fn test_bounding_box_new() {
        let bbox = BoundingBox::new(10.0, 20.0, 100.0, 150.0);
        assert_eq!(bbox.x, 10.0);
        assert_eq!(bbox.y, 20.0);
        assert_eq!(bbox.width, 100.0);
        assert_eq!(bbox.height, 150.0);
    }

    #[test]
    fn test_bounding_box_from_center() {
        let bbox = BoundingBox::from_center(50.0, 50.0, 100.0, 100.0);
        assert_eq!(bbox.x, 0.0);
        assert_eq!(bbox.y, 0.0);
    }

    #[test]
    fn test_bounding_box_from_corners() {
        let bbox = BoundingBox::from_corners(10.0, 20.0, 110.0, 170.0);
        assert_eq!(bbox.x, 10.0);
        assert_eq!(bbox.y, 20.0);
        assert_eq!(bbox.width, 100.0);
        assert_eq!(bbox.height, 150.0);
    }

    #[test]
    fn test_bounding_box_area() {
        let bbox = BoundingBox::new(0.0, 0.0, 10.0, 20.0);
        assert_eq!(bbox.area(), 200.0);
    }

    #[test]
    fn test_bounding_box_center() {
        let bbox = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let (cx, cy) = bbox.center();
        assert_eq!(cx, 50.0);
        assert_eq!(cy, 50.0);
    }

    #[test]
    fn test_bounding_box_contains() {
        let bbox = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        assert!(bbox.contains(50.0, 50.0));
        assert!(!bbox.contains(150.0, 50.0));
    }

    #[test]
    fn test_bounding_box_intersects() {
        let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let b = BoundingBox::new(50.0, 50.0, 100.0, 100.0);
        let c = BoundingBox::new(200.0, 200.0, 50.0, 50.0);

        assert!(a.intersects(&b));
        assert!(!a.intersects(&c));
    }

    #[test]
    fn test_bounding_box_intersection() {
        let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let b = BoundingBox::new(50.0, 50.0, 100.0, 100.0);

        let inter = a.intersection(&b).expect("intersection should succeed");
        assert_eq!(inter.x, 50.0);
        assert_eq!(inter.y, 50.0);
        assert_eq!(inter.width, 50.0);
        assert_eq!(inter.height, 50.0);
    }

    #[test]
    fn test_bounding_box_union() {
        let a = BoundingBox::new(0.0, 0.0, 50.0, 50.0);
        let b = BoundingBox::new(50.0, 50.0, 50.0, 50.0);

        let union = a.union(&b);
        assert_eq!(union.x, 0.0);
        assert_eq!(union.y, 0.0);
        assert_eq!(union.width, 100.0);
        assert_eq!(union.height, 100.0);
    }

    #[test]
    fn test_bounding_box_scale() {
        let bbox = BoundingBox::new(10.0, 20.0, 100.0, 100.0);
        let scaled = bbox.scale(0.5);
        assert_eq!(scaled.x, 5.0);
        assert_eq!(scaled.width, 50.0);
    }

    #[test]
    fn test_bounding_box_expand() {
        let bbox = BoundingBox::new(10.0, 10.0, 80.0, 80.0);
        let expanded = bbox.expand(5.0);
        assert_eq!(expanded.x, 5.0);
        assert_eq!(expanded.y, 5.0);
        assert_eq!(expanded.width, 90.0);
        assert_eq!(expanded.height, 90.0);
    }

    #[test]
    fn test_detection_new() {
        let bbox = BoundingBox::new(10.0, 20.0, 100.0, 100.0);
        let detection = Detection::new(bbox, 0, 0.95);
        assert_eq!(detection.class_id, 0);
        assert!(detection.confidence > 0.9);
    }

    #[test]
    fn test_iou() {
        let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let b = BoundingBox::new(50.0, 50.0, 100.0, 100.0);

        let overlap = iou(&a, &b);
        // Intersection: 50x50 = 2500
        // Union: 10000 + 10000 - 2500 = 17500
        // IoU: 2500 / 17500 = 0.142857...
        assert!(overlap > 0.14 && overlap < 0.15);
    }

    #[test]
    fn test_iou_no_overlap() {
        let a = BoundingBox::new(0.0, 0.0, 50.0, 50.0);
        let b = BoundingBox::new(100.0, 100.0, 50.0, 50.0);

        assert_eq!(iou(&a, &b), 0.0);
    }

    #[test]
    fn test_iou_same_box() {
        let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let b = BoundingBox::new(0.0, 0.0, 100.0, 100.0);

        assert!((iou(&a, &b) - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_giou() {
        let a = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let b = BoundingBox::new(50.0, 50.0, 100.0, 100.0);

        let g = giou(&a, &b);
        // GIoU can range from -1 to 1
        assert!((-1.0..=1.0).contains(&g));

        // Same boxes should have GIoU of 1
        let c = BoundingBox::new(0.0, 0.0, 100.0, 100.0);
        let g2 = giou(&a, &c);
        assert!((g2 - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_nms() {
        let detections = vec![
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.9),
            Detection::new(BoundingBox::new(10.0, 10.0, 100.0, 100.0), 0, 0.8),
            Detection::new(BoundingBox::new(200.0, 200.0, 100.0, 100.0), 0, 0.85),
        ];

        let filtered = nms(&detections, 0.5);

        // First two overlap significantly, keep only the best
        // Third one doesn't overlap, should be kept
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn test_nms_different_classes() {
        let detections = vec![
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.9),
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 1, 0.8),
        ];

        let filtered = nms(&detections, 0.5);

        // Different classes, both should be kept
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn test_soft_nms() {
        let detections = vec![
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.9),
            Detection::new(BoundingBox::new(10.0, 10.0, 100.0, 100.0), 0, 0.8),
        ];

        let filtered = soft_nms(&detections, 0.3, 0.5, 0.1);

        // Both should be kept but with adjusted scores
        assert!(!filtered.is_empty());
    }

    #[test]
    fn test_filter_by_confidence() {
        let detections = vec![
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.9),
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.3),
        ];

        let filtered = filter_by_confidence(&detections, 0.5);
        assert_eq!(filtered.len(), 1);
    }

    #[test]
    fn test_filter_by_class() {
        let detections = vec![
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 0, 0.9),
            Detection::new(BoundingBox::new(0.0, 0.0, 100.0, 100.0), 1, 0.8),
        ];

        let filtered = filter_by_class(&detections, 0);
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].class_id, 0);
    }
}