From 015ad7083a13ea9ad49df6c6f70a2e4146b4e669 Mon Sep 17 00:00:00 2001 From: Gert Pellin - GPE Date: Tue, 13 Oct 2026 08:45:31 +0200 Subject: [PATCH] =?UTF-8?q?Select=20=E2=80=BA=20Sky:=20a=20white=20or=20gr?= =?UTF-8?q?ey=20backdrop=20that=20reaches=20the=20bottom=20is=20not=20sky?= =?UTF-8?q?=20(#4638)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Select > Sky: a white or grey backdrop that reaches the bottom is not sky The colour prior counts bright, nearly neutral colour as overcast sky, so a seamless studio backdrop starting at the top edge flooded the whole background (79 % of a product shot). A sky never reaches the bottom of a photo; a backdrop does. A region that also covers most of the bottom rows and is nearly neutral (mean saturation under 0.1) is now taken for a backdrop and nothing is selected; blue sky reaching the bottom, as in a photo taken upward, still is. Fixes #4634 * Sky: read the bottom rows of the mask with get() Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/algo/src/segment/sky.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/algo/src/segment/sky.rs b/crates/algo/src/segment/sky.rs index 74fc62790d1..a54bcf6772b 100644 --- a/crates/algo/src/segment/sky.rs +++ b/crates/algo/src/segment/sky.rs @@ -132,9 +132,31 @@ pub fn sky_mask_low(img: &RgbImage, threshold: f32) -> Option> { } } let mask = clean_mask(&mask, w, h, 0.002, 0.01); + if is_backdrop(img, &mask, top) { + return None; + } mask.iter().any(|v| *v).then_some(mask) } +/// A neutral region that also covers most of the bottom rows is a studio or product backdrop, +/// not an overcast sky (which never reaches the bottom of a photo); blue sky is never one. +fn is_backdrop(img: &RgbImage, mask: &[bool], rows: usize) -> bool { + let (w, h) = (img.w, img.h); + let bottom = (h - rows.min(h)) * w..h * w; + let Some(bottom_rows) = mask.get(bottom) else { return false }; + let covered = bottom_rows.iter().filter(|m| **m).count(); + if covered * 10 < bottom_rows.len() * 6 { + return false; + } + let (mut sat, mut n) = (0.0f32, 0usize); + for (p, _) in img.px.iter().zip(mask).filter(|(_, m)| **m) { + let (mx, mn) = (p[0].max(p[1]).max(p[2]), p[0].min(p[1]).min(p[2])); + sat += if mx > 1e-4 { (mx - mn) / mx } else { 0.0 }; + n += 1; + } + n > 0 && sat / (n as f32) < 0.1 +} + /// Selects the sky over `canvas`. `None` if no sky was found. pub fn select_sky(sampler: &dyn Sampler, canvas: Rect, p: SkyParams) -> Option { if canvas.width() < 4 || canvas.height() < 4 { @@ -269,6 +291,18 @@ mod tests { assert!(r.at(30, 10) > 0.9 && r.at(30, 50) < 0.1); } + /// #4634: a near-white studio backdrop around a product is not an overcast sky, but blue sky + /// reaching the bottom (a photo taken upward) still is. + #[test] + fn a_white_backdrop_is_not_sky_but_a_blue_sky_down_to_the_bottom_is() { + let disc = |x: usize, y: usize| ((x as f32 - 75.0).powi(2) + (y as f32 - 50.0).powi(2)).sqrt() < 30.0; + let img = RgbImage::from_fn(150, 100, |x, y| if disc(x, y) { [0.12, 0.12, 0.12] } else { [0.95, 0.95, 0.94] }); + assert!(select_sky(&ImageSampler { img: &img, origin: (0, 0) }, Rect::new(0, 0, 150, 100), SkyParams::default()).is_none()); + let img = RgbImage::from_fn(150, 100, |x, y| if disc(x, y) { [0.12, 0.3, 0.1] } else { [0.35, 0.6, 0.95] }); + let r = select_sky(&ImageSampler { img: &img, origin: (0, 0) }, Rect::new(0, 0, 150, 100), SkyParams::default()).expect("blue sky"); + assert!(r.at(5, 95) > 0.9 && r.at(75, 50) < 0.1); + } + #[test] fn colour_prior_ranks_sky_over_foliage() { assert!(colour_prior([0.4, 0.6, 0.95]) > 0.8);