Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Merge pull request #1804 from tylerjenningsw/fix/roto-stroke-order

BFlat7175a20f63aa
diff --cc crates/track/src/roto.rs
index 0948d3fe0,998f5100d..103d695dd
--- a/crates/track/src/roto.rs
+++ b/crates/track/src/roto.rs
@@@ -651,37 -699,7 +689,25 @@@ fn initial_models(rgb: &[[f32; 3]], har
}
(Gmm::fit(&fgc, COMPONENTS, MAX_SAMPLES), Gmm::fit(&bgc, COMPONENTS, MAX_SAMPLES))
}
- };
+ }
+}
+
+/// Segment a frame from its strokes (and a propagated prior): GrabCut iterations on a reduced
+/// copy, then a full-resolution cut in a band around the boundary.
+pub fn segment(img: &Image, strokes: &[&Stroke], prior: Option<&Prior>, opts: &SegOpts, refine_radius: f64) -> FrameSeg {
+ let (w, h) = (img.width as usize, img.height as usize);
+ if w == 0 || h == 0 {
+ return FrameSeg::empty(w, h);
+ }
+ let rgb = rgb_of(img);
- let fgs = rasterize(strokes, &[StrokeKind::Fg], opts.scale, w, h);
- let bgs = rasterize(strokes, &[StrokeKind::Bg], opts.scale, w, h);
- let hard: Vec<u8> = fgs
- .iter()
- .zip(&bgs)
- .map(|(f, b)| {
- if *b {
- HARD_BG
- } else if *f {
- HARD_FG
- } else {
- 0
- }
- })
- .collect();
++ let hard = resolve(strokes, StrokeKind::Fg, StrokeKind::Bg, opts.scale, w, h);
++ let fgs: Vec<bool> = hard.iter().map(|v| *v == HARD_FG).collect();
++ let bgs: Vec<bool> = hard.iter().map(|v| *v == HARD_BG).collect();
+ // A warped matte is uncertain by about the search radius: blur it. A model's matte is not.
+ let soft_prior: Option<Vec<f32>> =
+ prior.map(|p| if p.weight > PRIOR { p.soft.clone() } else { matting::gauss(&p.soft, w, h, (opts.search_radius * opts.scale / 4.0).max(1.0)) });
+ let pw = prior.map_or(PRIOR, |p| p.weight);
+ let (fg0, bg0) = initial_models(&rgb, &hard, &fgs, strokes, prior, opts, (w, h));
let iters = if opts.best { 4 } else { 3 };
let work = if opts.best { 960 } else { 480 };
let f = w.max(h).div_ceil(work).max(1);