PhotoCraft: An open-source, photo editor written by scratch in pure Rust
wip: fix Object Finder foreground classification (#6548)
euisuhd7fabb52404c
diff --git a/crates/algo/src/segment/grabcut.rs b/crates/algo/src/segment/grabcut.rsindex 573c09079..41ee22fcd 100644--- a/crates/algo/src/segment/grabcut.rs+++ b/crates/algo/src/segment/grabcut.rs@@ -139,8 +139,8 @@ pub const OBJECT_AT_WORK_PX: usize = 60_000;/// A heuristic, not a semantic segmenter (that is #2788): on a working-resolution copy of the/// canvas the area around the point is grown over pixels of similar colour (CIELAB, bounded both/// by the distance to the seed's running mean and by the local edge step), so it stops at the-/// object's outline. A region that grows over most of the canvas is the background: nothing is-/// found. Otherwise GrabCut refines it inside its padded bounding box (the grown area probable+/// object's outline. A region covering most of the canvas boundary is treated as background.+/// Otherwise GrabCut refines it inside its padded bounding box (the grown area probable/// foreground, the cursor definite foreground, outside the box definite background), the part/// connected to the cursor is kept and the boundary is re-cut at full resolution, as the/// rectangle mode does.@@ -164,8 +164,9 @@ pub fn object_at(sampler: &dyn Sampler, canvas: Rect, x: f32, y: f32, work_px: ulet sy = (((py - canvas.y0) as usize) / step).min(h - 1);let grown = grow_similar(&lab, w, h, sx, sy);let count = grown.iter().filter(|g| **g).count();- // Background (or a flat image): no single object under the cursor.- if count * 10 > w * h * 6 || count < 4 {+ let boundary = grown.iter().enumerate().filter(|(i, on)| **on && (i % w == 0 || i % w + 1 == w || i / w == 0 || i / w + 1 == h)).count();+ // shortcut: cropped objects covering most of the frame need a semantic segmenter (#2788).+ if boundary * 2 >= 2 * w + 2 * h - 4 || count < 4 {return None;}let (mut x0, mut y0, mut x1, mut y1) = (w, h, 0usize, 0usize);diff --git a/crates/algo/src/segment/tests.rs b/crates/algo/src/segment/tests.rsindex 743c3dc46..19726e118 100644--- a/crates/algo/src/segment/tests.rs+++ b/crates/algo/src/segment/tests.rs@@ -751,6 +751,38 @@ fn object_at_finds_the_disc_under_the_cursor() {assert!(v > 0.9, "IoU {v}");}+#[test]+fn object_at_keeps_large_foregrounds_but_not_the_surrounding_background() {+ let (w, h) = (400, 300);+ for margin in [30, 20, 8] {+ let inside = |x: usize, y: usize| x >= margin && x < w - margin && y >= margin && y < h - margin;+ let img = RgbImage::from_fn(w, h, |x, y| {+ let noise = ((x * 13 + y * 7) % 17) as f32 / 850.0;+ if inside(x, y) { [0.8 + noise, 0.2, 0.1] } else { [0.1, 0.2, 0.8 + noise] }+ });+ let truth: Vec<_> = (0..w * h).map(|i| inside(i % w, i / w)).collect();+ let sampler = ImageSampler { img: &img, origin: (-40, 25) };+ let canvas = Rect::new(-40, 25, w as i32 - 40, h as i32 + 25);+ let region = grabcut::object_at(&sampler, canvas, 160.0, 175.0, grabcut::OBJECT_AT_WORK_PX)+ .unwrap_or_else(|| panic!("missing large foreground with margin {margin}"));+ let local = Region { bbox: region.bbox.translate(40, -25), mask: region.mask };+ assert!(iou(&local, &truth, w, h) > 0.95, "margin {margin}");+ assert!(grabcut::object_at(&sampler, canvas, -39.0, 26.0, grabcut::OBJECT_AT_WORK_PX).is_none(), "background margin {margin}");+ }+}++#[test]+fn object_at_allows_a_foreground_touching_one_edge_but_rejects_a_flat_image() {+ let img = RgbImage::from_fn(400, 300, |x, y| if x < 360 && (20..280).contains(&y) { [0.8, 0.2, 0.1] } else { [0.1, 0.2, 0.8] });+ let sampler = ImageSampler { img: &img, origin: (0, 0) };+ let canvas = Rect::new(0, 0, 400, 300);+ let region = grabcut::object_at(&sampler, canvas, 200.0, 150.0, grabcut::OBJECT_AT_WORK_PX).unwrap();+ assert!(region.at(0, 150) > 0.9 && region.at(350, 150) > 0.9);+ assert!(region.at(380, 150) < 0.1 && region.at(200, 5) < 0.1);+ let flat = RgbImage::from_fn(400, 300, |_, _| [0.5; 3]);+ assert!(grabcut::object_at(&ImageSampler { img: &flat, origin: (0, 0) }, canvas, 200.0, 150.0, grabcut::OBJECT_AT_WORK_PX).is_none());+}+#[test]fn object_at_on_the_background_or_off_canvas_finds_nothing() {let (img, _) = disc_scene(320, 240, 160.0, 120.0, 70.0, 7);diff --git a/crates/algo/src/segment/workflow_tests.rs b/crates/algo/src/segment/workflow_tests.rsindex ca3be5b40..96c6caeb8 100644--- a/crates/algo/src/segment/workflow_tests.rs+++ b/crates/algo/src/segment/workflow_tests.rs@@ -1,4 +1,28 @@use super::*;++#[test]+#[ignore = "release Object Finder timing on a 24 MP document"]+#[allow(clippy::assertions_on_constants)]+fn object_finder_large_foreground_release_timing() {+ assert!(!cfg!(debug_assertions), "run with --release");+ let (w, h) = (6000, 4000);+ let img = RgbImage::from_fn(w, h, |x, y| if (300..5700).contains(&x) && (200..3800).contains(&y) { [0.8, 0.2, 0.1] } else { [0.1, 0.2, 0.8] });+ let sampler = ImageSampler { img: &img, origin: (0, 0) };+ let canvas = Rect::new(0, 0, w as i32, h as i32);+ for (name, x, y) in [("foreground", 3000.0, 2000.0), ("background", 10.0, 10.0)] {+ let start = std::time::Instant::now();+ let region = grabcut::object_at(&sampler, canvas, x, y, grabcut::OBJECT_AT_WORK_PX);+ println!("OBJECT_FINDER {w}x{h} {name}: {:.1} ms", start.elapsed().as_secs_f64() * 1000.0);+ if name == "foreground" {+ let region = region.unwrap();+ assert!(region.at(3000, 2000) > 0.9 && region.at(310, 210) > 0.9);+ assert!(region.at(10, 10) < 0.1);+ } else {+ assert!(region.is_none());+ }+ }+}+#[test]#[ignore = "release comparison of actual Quick Selection strokes on 24-36 MP documents"]#[allow(clippy::assertions_on_constants)]diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rsindex 380b6a720..a7b55131e 100644--- a/crates/engine/src/smartselect_cmds.rs+++ b/crates/engine/src/smartselect_cmds.rs@@ -696,6 +696,22 @@ mod tests {assert!(s.execute("select.object", json!({"point": ["a", 1]})).is_err());}+ #[test]+ fn object_selection_finds_a_large_foreground_at_each_depth_and_undoes() {+ for depth in [8, 16, 32] {+ let inside = |x: i32, y: i32| (10..190).contains(&x) && (8..152).contains(&y);+ let mut s = session_with(200, 160, depth, |x, y| if inside(x as i32, y as i32) { [0.8, 0.2, 0.1] } else { [0.1, 0.2, 0.8] });+ let r = s.execute("select.object", json!({"point": [100, 80]})).unwrap();+ assert_eq!(r["changed"], true, "depth {depth}");+ assert!(sel_iou(&s, 200, 160, inside) > 0.95, "depth {depth}");+ s.execute("edit.undo", json!({})).unwrap();+ assert!(s.active().unwrap().doc.selection.is_none());+ let r = s.execute("select.object", json!({"point": [1, 1]})).unwrap();+ assert_eq!(r["changed"], false, "background at depth {depth}");+ assert!(s.active().unwrap().doc.selection.is_none());+ }+ }+#[test]fn object_selection_point_honours_the_modes() {let (mut s, _) = disc(8);