rten_imageproc/
lib.rs

1//! Provides 2D geometry and image processing functions.
2//!
3//! This includes:
4//!
5//! - 2D vectors and related math
6//! - 2D shapes and associated algorithms: [Point], [Line], [Rect],
7//!   [RotatedRect], [Polygon]
8//! - Rudimentary drawing functions
9//! - Algorithms for finding the contours of connected components in an image
10//!   ([find_contours])
11//! - Algorithms for simplifying polygons and finding various kinds of shape
12//!   that contain a polygon: [simplify_polygon], [min_area_rect], [convex_hull]
13
14mod contours;
15mod drawing;
16mod math;
17mod normalize;
18mod poly_algos;
19mod shapes;
20
21pub use contours::{RetrievalMode, find_contours};
22pub use drawing::{FillIter, Painter, Rgb, draw_line, draw_polygon, fill_rect, stroke_rect};
23pub use math::Vec2;
24pub use normalize::{IMAGENET_MEAN, IMAGENET_STD_DEV, normalize_image};
25pub use poly_algos::{convex_hull, min_area_rect, simplify_polygon, simplify_polyline};
26pub use shapes::{
27    BoundingRect, Coord, Line, LineF, Point, PointF, Polygon, PolygonF, Polygons, Rect, RectF,
28    RotatedRect, bounding_rect,
29};
30
31#[cfg(test)]
32mod tests {
33    use std::fmt::Display;
34
35    use rten_tensor::{MatrixLayout, NdTensorView, NdTensorViewMut};
36
37    use super::{Coord, Point, Rect};
38
39    /// Return a list of the points on the border of `rect`, in counter-clockwise
40    /// order starting from the top-left corner.
41    ///
42    /// If `omit_corners` is true, the corner points of the rect are not
43    /// included.
44    pub fn border_points(rect: Rect, omit_corners: bool) -> Vec<Point> {
45        let mut points = Vec::new();
46
47        let left_range = if omit_corners {
48            rect.top() + 1..rect.bottom() - 1
49        } else {
50            rect.top()..rect.bottom()
51        };
52
53        // Left edge
54        for y in left_range.clone() {
55            points.push(Point::from_yx(y, rect.left()));
56        }
57
58        // Bottom edge
59        for x in rect.left() + 1..rect.right() - 1 {
60            points.push(Point::from_yx(rect.bottom() - 1, x));
61        }
62
63        // Right edge
64        for y in left_range.rev() {
65            points.push(Point::from_yx(y, rect.right() - 1));
66        }
67
68        // Top edge
69        for x in (rect.left() + 1..rect.right() - 1).rev() {
70            points.push(Point::from_yx(rect.top(), x));
71        }
72
73        points
74    }
75
76    /// Set the elements of a grid listed in `points` to `value`.
77    #[allow(dead_code)]
78    pub fn plot_points<T: Copy>(mut grid: NdTensorViewMut<T, 2>, points: &[Point], value: T) {
79        for point in points {
80            grid[point.coord()] = value;
81        }
82    }
83
84    /// Plot the 1-based indices of points in `points` on a grid. `step` is the
85    /// increment value for each plotted point.
86    #[allow(dead_code)]
87    fn plot_point_indices<T: std::ops::AddAssign + Copy + Default>(
88        mut grid: NdTensorViewMut<T, 2>,
89        points: &[Point],
90        step: T,
91    ) {
92        let mut value = T::default();
93        value += step;
94        for point in points {
95            grid[point.coord()] = value;
96            value += step;
97        }
98    }
99
100    /// Convert a slice of `[y, x]` coordinates to `Point`s
101    pub fn points_from_coords<T: Coord>(coords: &[[T; 2]]) -> Vec<Point<T>> {
102        coords.iter().map(|[y, x]| Point::from_yx(*y, *x)).collect()
103    }
104
105    /// Convery an array of `[y, x]` coordinates to `Point`s
106    pub fn points_from_n_coords<T: Coord, const N: usize>(coords: [[T; 2]; N]) -> [Point<T>; N] {
107        coords.map(|[y, x]| Point::from_yx(y, x))
108    }
109
110    /// Print out elements of a 2D grid for debugging.
111    #[allow(dead_code)]
112    pub fn print_grid<T: Display>(grid: NdTensorView<T, 2>) {
113        for y in 0..grid.rows() {
114            for x in 0..grid.cols() {
115                print!("{:2} ", grid[[y, x]]);
116            }
117            println!();
118        }
119        println!();
120    }
121}