Skip to main content

gstreamer_analytics/
image_util.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub struct Rect<T> {
7    pub x: T,
8    pub y: T,
9    pub w: T,
10    pub h: T,
11}
12
13#[cfg(feature = "v1_28")]
14#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
15#[doc(alias = "gst_analytics_image_util_iou_float")]
16pub fn iou_f32(bb1: Rect<f32>, bb2: Rect<f32>) -> f32 {
17    skip_assert_initialized!();
18    unsafe {
19        ffi::gst_analytics_image_util_iou_float(
20            bb1.x, bb1.y, bb1.w, bb1.h, bb2.x, bb2.y, bb2.w, bb2.h,
21        )
22    }
23}
24
25#[cfg(feature = "v1_28")]
26#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
27#[doc(alias = "gst_analytics_image_util_iou_int")]
28pub fn iou_i32(bb1: Rect<i32>, bb2: Rect<i32>) -> f32 {
29    skip_assert_initialized!();
30    unsafe {
31        ffi::gst_analytics_image_util_iou_int(
32            bb1.x, bb1.y, bb1.w, bb1.h, bb2.x, bb2.y, bb2.w, bb2.h,
33        )
34    }
35}
36
37#[cfg(test)]
38#[cfg(feature = "v1_28")]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn iou_float_no_overlap() {
44        gst::init().unwrap();
45        // Two non-overlapping boxes
46        let iou = iou_f32(
47            Rect::<f32> {
48                x: 0.0,
49                y: 0.0,
50                w: 10.0,
51                h: 10.0,
52            },
53            Rect::<f32> {
54                x: 20.0,
55                y: 20.0,
56                w: 10.0,
57                h: 10.0,
58            },
59        );
60        assert_eq!(iou, 0.0);
61    }
62
63    #[test]
64    fn iou_float_identical_boxes() {
65        gst::init().unwrap();
66        let iou = iou_f32(
67            Rect::<f32> {
68                x: 0.0,
69                y: 0.0,
70                w: 10.0,
71                h: 10.0,
72            },
73            Rect::<f32> {
74                x: 0.0,
75                y: 0.0,
76                w: 10.0,
77                h: 10.0,
78            },
79        );
80        assert!((iou - 1.0).abs() < f32::EPSILON);
81    }
82
83    #[test]
84    fn iou_int_no_overlap() {
85        gst::init().unwrap();
86        let iou = iou_i32(
87            Rect::<i32> {
88                x: 0,
89                y: 0,
90                w: 10,
91                h: 10,
92            },
93            Rect::<i32> {
94                x: 20,
95                y: 20,
96                w: 10,
97                h: 10,
98            },
99        );
100        assert_eq!(iou, 0.0);
101    }
102
103    #[test]
104    fn iou_int_identical_boxes() {
105        gst::init().unwrap();
106        let iou = iou_i32(
107            Rect::<i32> {
108                x: 0,
109                y: 0,
110                w: 10,
111                h: 10,
112            },
113            Rect::<i32> {
114                x: 0,
115                y: 0,
116                w: 10,
117                h: 10,
118            },
119        );
120        assert!((iou - 1.0).abs() < f32::EPSILON);
121    }
122}