From bcb7c94390dc25f0ca27ffd1877f046c7a55f289 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Tue, 5 Aug 2025 12:58:17 +0530 Subject: [PATCH] feat: Draw bounding boxes correctly --- Cargo.toml | 2 +- bounding-box/src/draw.rs | 10 ++++------ bounding-box/src/lib.rs | 31 ++++++++++++++++++++++++++++--- src/main.rs | 3 +-- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8000e7f..eb6a62e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ version = "0.1.0" edition = "2024" [patch."https://github.com/uttarayan21/mnn-rs"] -mnn = { path = "/home/servius/Projects/mnn-rs" } +mnn = { path = "/Users/fs0c131y/Projects/aftershoot/mnn-rs" } [workspace.dependencies] ndarray-image = { path = "ndarray-image" } diff --git a/bounding-box/src/draw.rs b/bounding-box/src/draw.rs index 3216599..6d8e044 100644 --- a/bounding-box/src/draw.rs +++ b/bounding-box/src/draw.rs @@ -48,15 +48,13 @@ impl Drawable> for Aabb2 { let color = Array1::from_vec(vec![color.r, color.g, color.b, color.a]); let pixel_size = canvas.dim().2; let color = color.slice(ndarray::s![..pixel_size]); - let [x1y1, x2y1, x1y2, x2y2] = self.corners(); - dbg!(self.corners()); + let [x1y1, x2y1, x2y2, x1y2] = self.corners(); let top = Aabb2::from_x1y1x2y2(x1y1.x, x1y1.y, x2y1.x, x2y1.y + thickness); - let bottom = Aabb2::from_x1y1x2y2(x2y2.x, x2y2.y, x1y2.x, x1y2.y + thickness); + let bottom = Aabb2::from_x1y1x2y2(x1y2.x, x1y2.y, x2y2.x, x2y2.y + thickness); let left = Aabb2::from_x1y1x2y2(x1y1.x, x1y1.y, x1y2.x + thickness, x1y2.y); - // let right = Aabb2::from_x1y1x2y2(x2y2.x, x2y2.y, x2y1.x + thickness, x2y1.y); - let lines = [top, bottom, left /* right */]; + let right = Aabb2::from_x1y1x2y2(x2y1.x, x2y1.y, x2y2.x + thickness, x2y2.y + thickness); + let lines = [top, bottom, left, right]; lines.into_iter().for_each(|line| { - dbg!(line); canvas .roi_mut(line) .expect("Failed to get Roi") diff --git a/bounding-box/src/lib.rs b/bounding-box/src/lib.rs index cc04732..cde1095 100644 --- a/bounding-box/src/lib.rs +++ b/bounding-box/src/lib.rs @@ -2,6 +2,7 @@ pub mod draw; pub mod nms; pub mod roi; +use itertools::Itertools; use nalgebra::{Point, Point2, Point3, SVector}; pub trait Num: num::Num + Copy + core::fmt::Debug + 'static {} impl Num for T {} @@ -108,8 +109,7 @@ impl AxisAlignedBoundingBox { let min = self.min_vertex(); let max = self.max_vertex(); - // *point > min && *point < max - true + *point >= min && *point <= max } pub fn scale(self, vector: SVector) -> Self @@ -241,7 +241,6 @@ impl Aabb2 { { let point1 = Point2::new(x1, y1); let point2 = Point2::new(x2, y2); - dbg!(point1, point2); Self::from_min_max_vertices(point1, point2) } pub fn new_2d(point1: Point2, point2: Point2) -> Self @@ -451,3 +450,29 @@ fn test_bounding_box_intersection_2d() { assert_eq!(intersection_bbox.min_vertex(), Point2::new(3.0, 5.0)); assert_eq!(intersection_bbox.size(), Vector2::new(1.0, 1.0)); } + +#[test] +fn test_bounding_box_contains_point() { + use nalgebra::Point2; + + let point1 = Point2::new(2, 3); + let point2 = Point2::new(5, 4); + let bbox = AxisAlignedBoundingBox::new_2d(point1, point2); + for (i, j) in (0..=10).cartesian_product(0..=10) { + if bbox.contains_point(&Point2::new(i, j)) { + if !(2..=5).contains(&i) && !(3..=4).contains(&j) { + panic!( + "Point ({}, {}) should not be contained in the bounding box", + i, j + ); + } + } else { + if (2..=5).contains(&i) && (3..=4).contains(&j) { + panic!( + "Point ({}, {}) should be contained in the bounding box", + i, j + ); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 0d165b1..616d1c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,8 +43,7 @@ pub fn main() -> Result<()> { .cast() .ok_or(errors::Error) .attach_printable("Failed to cast f32 to usize")?; - array.draw(bbox, color::palette::css::GREEN_YELLOW.to_rgba8(), 20); - break; + array.draw(bbox, color::palette::css::GREEN_YELLOW.to_rgba8(), 10); } let v = array.view(); if let Some(output) = detect.output {