Select › Sky: a white or grey backdrop that reaches the bottom is not sky (#4638)

* 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 <[email protected]>

---------

Co-authored-by: Brandon Thomas <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-13 02:45:31 -04:00
1 parent 2d190e7c57
commit 015ad7083a
1 file changed
+34
+34
View File
@@ -132,9 +132,31 @@ pub fn sky_mask_low(img: &RgbImage, threshold: f32) -> Option<Vec<bool>> {
}
}
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<Region> {
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);