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
use super::{SlidingWindow, WindowGenerator};
use crate::types::ImageWindow;
use image::{DynamicImage, SubImage};

/// Struct for generating sliding windows over an image
#[derive(Debug, PartialEq)]
pub struct PyramidWindow {
    /// Width of the layer 1 window
    pub width: u32,
    /// Height of the layer 1 window
    pub height: u32,
    /// Step size for moving the window
    pub step_size: u32,
    /// the pyramid layers
    pub layer_scales: Vec<f32>,
}

impl WindowGenerator<DynamicImage> for PyramidWindow {
    fn windows<'a, 'b>(
        &'a self,
        image: &'b DynamicImage,
    ) -> Vec<ImageWindow<SubImage<&'b DynamicImage>>> {
        let mut windows = Vec::new();
        for scale in self.layer_scales.iter() {
            let sliding_window = SlidingWindow {
                width: (self.width as f32 * scale) as u32,
                height: (self.height as f32 * scale) as u32,
                step_size: (self.step_size as f32 * scale) as u32,
            };
            windows.append(&mut sliding_window.windows(image));
        }
        windows
    }
}

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

    #[test]
    fn test_sliding_window() {
        let image = DynamicImage::new_rgb8(10, 10);
        let window_size = 5;
        let step_size = 5;

        let generator = PyramidWindow {
            width: window_size,
            height: window_size,
            step_size: step_size,
            layer_scales: vec![1.0, 2.0],
        };

        let windows = generator.windows(&image);

        // Check that the correct number of windows was returned
        assert_eq!(windows.len(), 5);

        // Check that the positions and subimages of the returned windows are correct
        assert_eq!(windows[0].x, 0);
        assert_eq!(windows[0].x, 0);
        assert_eq!(windows[1].x, 5);
        assert_eq!(windows[2].y, 5);
        assert_eq!(windows[3].x, 5);
        assert_eq!(windows[3].y, 5);
    }

    #[test]
    fn test_sliding_window_on_1_5_layer_scale() {
        let image = DynamicImage::new_rgb8(10, 10);
        let window_size = 5;
        let step_size = 5;
        let generator = PyramidWindow {
            width: window_size,
            height: window_size,
            step_size: step_size,
            layer_scales: vec![1.5],
        };

        let windows = generator.windows(&image);
        assert_eq!(windows.len(), 1);
    }
}