test(segment): pin a BK-sized GrabCut mask and use a genuine min-cut tie

Address CodeRabbit on #9961: the 32x24 pinned mask stayed under the 4096-node
BK threshold, and the tie fixture had a unique minimum cut.
This commit is contained in:
Khawar Zamman Wani committed 2026-10-29 09:30:05 +05:30
1 parent 5177daef70
commit 6d449fc8cb
1 file changed
+34 -13
+34 -13
View File
@@ -330,6 +330,23 @@ fn grabcut_small_disc_mask_is_pinned() {
assert_eq!(got, want);
}
#[test]
fn grabcut_large_disc_mask_is_pinned() {
// 5168 free nodes with terminal links both ways, so the pre-BK solver was push/relabel here.
// The count and FNV-1a hash are of the mask that push/relabel produced at the merge base.
let (img, _) = disc_scene(96, 80, 48.0, 40.0, 26.0, 3);
let mut tri = vec![grabcut::BG; img.w * img.h];
for y in 6..74 {
for x in 10..86 {
tri[y * img.w + x] = grabcut::PR_FG;
}
}
grabcut::grabcut(&img, &mut tri, 8).unwrap();
let fg: Vec<bool> = tri.iter().map(|t| *t == grabcut::PR_FG || *t == grabcut::FG).collect();
let hash = fg.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(*b)).wrapping_mul(0x0100_0000_01b3));
assert_eq!((fg.iter().filter(|b| **b).count(), hash), (2128, 0x62fd_c64a_0ee4_4e75));
}
#[test]
fn object_select_disc_full_resolution() {
let (img, truth) = disc_scene(200, 160, 100.0, 80.0, 50.0, 2);
@@ -1134,29 +1151,33 @@ fn grid_energy(img: &RgbImage, cf: &[f32], cb: &[f32], cut: &[bool], weight: imp
}
#[test]
fn bk_and_default_grid_cut_label_a_tie_alike() {
// Uniform pixels, equal data costs and a hard border: every free node is a coin flip between
// the two sides, so only the solvers' tie-breaking decides the labelling.
fn bk_and_default_grid_cut_tie_reaches_the_same_energy() {
// Hard background down the left edge, hard foreground down the right, uniform pixels and equal
// data costs: a vertical cut at any column separates the borders at the same energy, so only
// the solvers' tie-breaking decides the labelling.
let (w, h) = (120usize, 100usize);
let img = RgbImage::from_fn(w, h, |_, _| [0.5, 0.5, 0.5]);
let (cf, cb) = (vec![1.0f32; w * h], vec![1.0f32; w * h]);
let fixed: Vec<u8> = (0..w * h)
.map(|i| {
if i % w == 0 {
HARD_BG
} else if i == 50 * w + 60 {
HARD_FG
} else {
FREE
}
.map(|i| match i % w {
0 => HARD_BG,
x if x == w - 1 => HARD_FG,
_ => FREE,
})
.collect();
let weight = |_: f32| 2.0;
// Two distinct candidate labellings (vertical cuts at different columns) tie at the minimum.
let split = |col: usize| -> Vec<bool> { (0..w * h).map(|i| i % w >= col).collect() };
let (e30, e90) = (grid_energy(&img, &cf, &cb, &split(30), weight), grid_energy(&img, &cf, &cb, &split(90), weight));
assert!((e30 - e90).abs() <= 1e-6 * e30.abs().max(1.0), "candidates must tie: {e30} vs {e90}");
assert_ne!(split(30), split(90));
let a = grid_cut_cancellable(&img, &cf, &cb, &fixed, weight, &|| false).expect("default cut");
let b = grid_cut_bk_cancellable(&img, &cf, &cb, &fixed, weight, &|| false).expect("bk cut");
let (ea, eb) = (grid_energy(&img, &cf, &cb, &a, weight), grid_energy(&img, &cf, &cb, &b, weight));
assert!((ea - eb).abs() <= 1e-3 * ea.abs().max(1.0), "default {ea} vs bk {eb}");
assert_eq!(a, b, "tie broken differently");
assert!((ea - e30).abs() <= 1e-3 * e30.abs().max(1.0), "default {ea} is not the tied minimum {e30}");
assert!((eb - e30).abs() <= 1e-3 * e30.abs().max(1.0), "bk {eb} is not the tied minimum {e30}");
// The solvers may pick different minimum cuts here; only the energy is guaranteed.
assert!(a.iter().any(|&l| l) && b.iter().any(|&l| l), "both label the foreground border");
}
#[test]