feat: Draw bounding boxes correctly

This commit is contained in:
uttarayan21
2025-08-05 12:58:17 +05:30
parent 74c603dc37
commit bcb7c94390
4 changed files with 34 additions and 12 deletions

View File

@@ -6,7 +6,7 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[patch."https://github.com/uttarayan21/mnn-rs"] [patch."https://github.com/uttarayan21/mnn-rs"]
mnn = { path = "/home/servius/Projects/mnn-rs" } mnn = { path = "/Users/fs0c131y/Projects/aftershoot/mnn-rs" }
[workspace.dependencies] [workspace.dependencies]
ndarray-image = { path = "ndarray-image" } ndarray-image = { path = "ndarray-image" }

View File

@@ -48,15 +48,13 @@ impl Drawable<Array3<u8>> for Aabb2<usize> {
let color = Array1::from_vec(vec![color.r, color.g, color.b, color.a]); let color = Array1::from_vec(vec![color.r, color.g, color.b, color.a]);
let pixel_size = canvas.dim().2; let pixel_size = canvas.dim().2;
let color = color.slice(ndarray::s![..pixel_size]); let color = color.slice(ndarray::s![..pixel_size]);
let [x1y1, x2y1, x1y2, x2y2] = self.corners(); let [x1y1, x2y1, x2y2, x1y2] = self.corners();
dbg!(self.corners());
let top = Aabb2::from_x1y1x2y2(x1y1.x, x1y1.y, x2y1.x, x2y1.y + thickness); 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 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 right = Aabb2::from_x1y1x2y2(x2y1.x, x2y1.y, x2y2.x + thickness, x2y2.y + thickness);
let lines = [top, bottom, left /* right */]; let lines = [top, bottom, left, right];
lines.into_iter().for_each(|line| { lines.into_iter().for_each(|line| {
dbg!(line);
canvas canvas
.roi_mut(line) .roi_mut(line)
.expect("Failed to get Roi") .expect("Failed to get Roi")

View File

@@ -2,6 +2,7 @@ pub mod draw;
pub mod nms; pub mod nms;
pub mod roi; pub mod roi;
use itertools::Itertools;
use nalgebra::{Point, Point2, Point3, SVector}; use nalgebra::{Point, Point2, Point3, SVector};
pub trait Num: num::Num + Copy + core::fmt::Debug + 'static {} pub trait Num: num::Num + Copy + core::fmt::Debug + 'static {}
impl<T: num::Num + Copy + core::fmt::Debug + 'static> Num for T {} impl<T: num::Num + Copy + core::fmt::Debug + 'static> Num for T {}
@@ -108,8 +109,7 @@ impl<T: Num, const D: usize> AxisAlignedBoundingBox<T, D> {
let min = self.min_vertex(); let min = self.min_vertex();
let max = self.max_vertex(); let max = self.max_vertex();
// *point > min && *point < max *point >= min && *point <= max
true
} }
pub fn scale(self, vector: SVector<T, D>) -> Self pub fn scale(self, vector: SVector<T, D>) -> Self
@@ -241,7 +241,6 @@ impl<T: Num> Aabb2<T> {
{ {
let point1 = Point2::new(x1, y1); let point1 = Point2::new(x1, y1);
let point2 = Point2::new(x2, y2); let point2 = Point2::new(x2, y2);
dbg!(point1, point2);
Self::from_min_max_vertices(point1, point2) Self::from_min_max_vertices(point1, point2)
} }
pub fn new_2d(point1: Point2<T>, point2: Point2<T>) -> Self pub fn new_2d(point1: Point2<T>, point2: Point2<T>) -> 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.min_vertex(), Point2::new(3.0, 5.0));
assert_eq!(intersection_bbox.size(), Vector2::new(1.0, 1.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
);
}
}
}
}

View File

@@ -43,8 +43,7 @@ pub fn main() -> Result<()> {
.cast() .cast()
.ok_or(errors::Error) .ok_or(errors::Error)
.attach_printable("Failed to cast f32 to usize")?; .attach_printable("Failed to cast f32 to usize")?;
array.draw(bbox, color::palette::css::GREEN_YELLOW.to_rgba8(), 20); array.draw(bbox, color::palette::css::GREEN_YELLOW.to_rgba8(), 10);
break;
} }
let v = array.view(); let v = array.view();
if let Some(output) = detect.output { if let Some(output) = detect.output {