PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Object Selection: enclosed background gaps stay unselected, only object-like holes fill (#6556)
MarekSpodBiedry156b27d65b8c
diff --git a/crates/algo/src/segment/grabcut.rs b/crates/algo/src/segment/grabcut.rsindex 573c09079..e1c1d89de 100644--- a/crates/algo/src/segment/grabcut.rs+++ b/crates/algo/src/segment/grabcut.rs@@ -126,7 +126,9 @@ pub fn object_select(sampler: &dyn Sampler, canvas: Rect, rect: Rect, work_px: u}let (fg, bg) = grabcut(&img, &mut trimap, 8)?;let low: Vec<bool> = trimap.iter().map(|t| is_fg(*t)).collect();- let low = super::clean_mask(&low, lw, lh, 0.1, 0.002);+ let low = super::clean_mask(&low, lw, lh, 0.1, 0.0);+ // Every enclosed hole, however small, fills only when it looks like the object, so background seen through it stays open.+ let low = super::fill_holes_by_model(&low, &img, &fg, &bg, 0.0, 0.05);super::finish_region(sampler, window, step, &low, lw, lh, Some((&fg, &bg)))}@@ -209,7 +211,9 @@ pub fn object_at(sampler: &dyn Sampler, canvas: Rect, x: f32, y: f32, work_px: ulet mut seeds = vec![false; w * h];seeds[sy * w + sx] = true;let low = super::keep_seeded(&low, &seeds, w, h);- let low = super::clean_mask(&low, w, h, 0.1, 0.01);+ let low = super::clean_mask(&low, w, h, 0.1, 0.0);+ // As in the rectangle mode: enclosed holes fill only when they look like the object.+ let low = super::fill_holes_by_model(&low, &img, &fg, &bg, 0.0, 0.05);if !low.iter().any(|v| *v) {return None;}diff --git a/crates/algo/src/segment/tests.rs b/crates/algo/src/segment/tests.rsindex 743c3dc46..e060913c9 100644--- a/crates/algo/src/segment/tests.rs+++ b/crates/algo/src/segment/tests.rs@@ -912,3 +912,61 @@ fn select_subject_dump_pictures() {write("ring", &img, None);write("ring-selected", &img, reg.as_ref());}++/// A dark square on a light background with a square hole that shows the background colour.+fn square_with_hole(hole: usize) -> RgbImage {+ let (w, h) = (400usize, 300usize);+ let mut rng = Rng::new(11);+ let (x0, y0) = (100usize, 50usize);+ RgbImage::from_fn(w, h, |x, y| {+ let n = 0.02 * rng.normal();+ let in_square = x >= x0 && x < x0 + 200 && y >= y0 && y < y0 + 200;+ let in_hole = x >= x0 + 96 && x < x0 + 96 + hole && y >= y0 + 96 && y < y0 + 96 + hole;+ let c = if in_square && !in_hole { [0.1, 0.12, 0.15] } else { [0.9, 0.9, 0.85] };+ c.map(|v: f32| (v + n).clamp(0.0, 1.0))+ })+}++#[test]+fn object_select_keeps_small_background_hole_open() {+ let img = square_with_hole(8);+ let s = ImageSampler { img: &img, origin: (0, 0) };+ let canvas = Rect::new(0, 0, 400, 300);+ let reg = grabcut::object_select(&s, canvas, Rect::new(80, 30, 320, 270), 1_000_000).unwrap();+ assert!(reg.at(150, 100) >= 0.5, "the square itself is selected");+ assert!(reg.at(200, 150) < 0.5, "background seen through the object stays unselected");+}++#[test]+fn object_at_keeps_small_background_hole_open() {+ let img = square_with_hole(16);+ let s = ImageSampler { img: &img, origin: (0, 0) };+ let canvas = Rect::new(0, 0, 400, 300);+ let reg = grabcut::object_at(&s, canvas, 130.0, 80.0, 1_000_000).unwrap();+ assert!(reg.at(150, 100) >= 0.5, "the square itself is selected");+ assert!(reg.at(203, 153) < 0.5, "background seen through the object stays unselected");+}++#[test]+fn hole_fill_without_specks_fills_object_coloured_holes_only() {+ // The hole check Object Selection uses: no hole fills just for being small, a speck of the object's colour still does.+ let (w, h) = (40usize, 40usize);+ let mut rng = Rng::new(5);+ let in_square = |x: usize, y: usize| (5..35).contains(&x) && (5..35).contains(&y);+ let bg_hole = |x: usize, y: usize| (10..12).contains(&x) && (10..12).contains(&y);+ let fg_hole = |x: usize, y: usize| (25..27).contains(&x) && (25..27).contains(&y);+ let img = RgbImage::from_fn(w, h, |x, y| {+ let n = 0.02 * rng.normal();+ let c = if in_square(x, y) && !bg_hole(x, y) { [0.1, 0.12, 0.15] } else { [0.9, 0.9, 0.85] };+ c.map(|v: f32| (v + n).clamp(0.0, 1.0))+ });+ let mask: Vec<bool> = (0..w * h).map(|i| in_square(i % w, i / w) && !bg_hole(i % w, i / w) && !fg_hole(i % w, i / w)).collect();+ // The colour models, as GrabCut would fit them: the object colour vs. the backdrop colour.+ let dark: Vec<bool> = (0..w * h).map(|i| in_square(i % w, i / w) && !bg_hole(i % w, i / w)).collect();+ let (fs, bs): (Vec<_>, Vec<_>) = img.px.iter().zip(&dark).partition(|(_, m)| **m);+ let fit = |v: Vec<(&[f32; 3], &bool)>| Gmm::fit(&v.iter().map(|(p, _)| **p).collect::<Vec<_>>(), 3, gmm::DEFAULT_REG).unwrap();+ let (fg, bg) = (fit(fs), fit(bs));+ let out = fill_holes_by_model(&mask, &img, &fg, &bg, 0.0, 0.05);+ assert!(out[25 * w + 25], "a speck of the object's colour fills");+ assert!(!out[10 * w + 10], "a speck of the background's colour stays open");+}