lhk229 5ddf0cd392 Despill across full foreground to cut edge green spill
Green spill bleeds into pixels ViTMatte marks as solid foreground (alpha~1),
but despill was gated to the soft edge band (0.005<alpha<0.995) and weakened
exactly there: the alpha tent peaked at 0.5 and bg_confidence (low on the
character edge) further suppressed the correction.

- (1) Apply despill to the whole foreground (alpha > edge_low) instead of the
  soft band only; neutral pixels stay untouched via the green-excess term.
- (2) Weight by alpha at full strength across the opaque range (alpha>=0.5),
  easing off only where mostly transparent, instead of a tent peaking at 0.5.
- (3) Drop bg_confidence as a despill gate; localise purely by green excess.

Controlled comparison (same alpha, only despill changed), edge_green_excess:
  ViTMatte TestImage  p95 0.239->0.176, mean 0.131->0.061
  ViTMatte TestImage2 p95 0.231->0.165, mean 0.115->0.052

Removes now-unused DespillSettings fields edge_high and bg_confidence_weight.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 16:58:32 +08:00
2026-06-30 16:25:56 +08:00
2026-06-30 16:25:56 +08:00

BgFilter

Offline green screen character matting for AI-generated character images.

The first implementation follows the workflow in docs/green_screen_matting_workflow.md:

RGB input -> chroma confidence -> trimap -> ViTMatte -> alpha cleanup -> despill -> RGBA PNG -> QA previews

Environment

Use the conda environment lightML.

conda activate lightML
pip install -r requirements.txt

If the shell is not activated, call the environment Python directly:

D:\MiniConda\envs\lightML\python.exe -m bgfilter.cli --help

Single Image

D:\MiniConda\envs\lightML\python.exe -m bgfilter.cli `
  --input Samples\TestImage.png `
  --output Outputs\TestImage_rgba.png `
  --debug-dir Outputs\TestImage_debug `
  --config configs\default.yaml `
  --device cuda

The CLI reads configs/default.yaml when --config is provided. Command-line options override config values, so tuning can usually happen in YAML while runtime choices such as --device cpu stay on the command line.

Use CPU for validation when CUDA is unavailable:

D:\MiniConda\envs\lightML\python.exe -m bgfilter.cli `
  --input Samples\TestImage.png `
  --output Outputs\TestImage_rgba.png `
  --debug-dir Outputs\TestImage_debug `
  --config configs\default.yaml `
  --device cpu

Batch

D:\MiniConda\envs\lightML\python.exe -m bgfilter.cli `
  --input-dir Samples `
  --output-dir Outputs `
  --debug-dir Outputs\debug `
  --config configs\default.yaml `
  --device cuda

Chroma-Only Debug Mode

This mode skips ViTMatte and uses chroma confidence as an alpha seed. It is useful for fast debugging of chroma confidence, trimap, despill, and QA outputs.

D:\MiniConda\envs\lightML\python.exe -m bgfilter.cli `
  --input-dir Samples `
  --output-dir Outputs\chroma `
  --debug-dir Outputs\chroma_debug `
  --config configs\default.yaml `
  --matting-method chroma `
  --device cpu

Outputs

For each processed image, the CLI writes an RGBA PNG and optional debug files:

bg_confidence.png
trimap.png
alpha.png
foreground_mask.png
foreground_unmix_mask.png
foreground_local_mask.png
foreground_rgb.png
despill_mask.png
preview_black.png
preview_white.png
preview_gray.png
preview_red.png
preview_blue.png
qa_grid.png
metadata.json

Quality Check

The quality checker measures alpha validity and green spill on semi-transparent edge pixels.

D:\MiniConda\envs\lightML\python.exe -m bgfilter.quality_cli `
  Outputs\TestImage_rgba.png `
  --max-edge-green-excess-p95 0.30

Run the bundled sample smoke check:

D:\MiniConda\envs\lightML\python.exe scripts\smoke_samples.py `
  --samples-dir Samples `
  --output-dir Outputs\smoke_samples `
  --config configs\default.yaml `
  --device cpu `
  --fallback-to-chroma-alpha `
  --max-edge-green-excess-p95 0.30

Current Notes

  • Samples/ and Outputs/ are ignored by Git.
  • ViTMatte model weights are loaded from Hugging Face on first use.
  • Foreground color estimation runs before final de-spill and writes debug masks for unmixing and local foreground propagation.
S
Description
No description provided
Readme 1 MiB
Languages
Python 100%