9b7a192599
After the full-foreground despill (5ddf0cd) the residual green was confined to
semi-transparent hair (alpha < 0.5): 85% of the worst pixels had alpha < 0.3,
where a pixel-wise unmix (divide by small alpha) is too noisy to trust.
Replace the hand-rolled unmix/local-blur estimator with pymatting's
estimate_foreground_ml (Germer et al. multi-level closed form), which propagates
reliable foreground colour from high-alpha neighbours into the fringe and
estimates the background, so green spill is unmixed rather than clamped. The
despill pass stays as a light cleanup on top.
- foreground.method selects "ml" (default) or "unmix" (legacy heuristic kept as
a fallback when pymatting is unavailable, matching the chroma fallback idiom).
- ForegroundEstimate now exposes rgb (F), background (B) and a correction map;
debug outputs become foreground_rgb / foreground_background / foreground_correction.
Controlled comparison (same ViTMatte alpha, only foreground method changed),
edge_green_excess:
TestImage p95 0.176->0.031, mean 0.061->0.005
TestImage2 p95 0.165->0.004, mean 0.052->0.002
Adds pymatting (pulls in numba) to requirements.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
83 lines
2.3 KiB
Python
83 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ChromaSettings:
|
|
green_margin: float = 0.08
|
|
min_green_value: float = 0.22
|
|
min_saturation: float = 0.20
|
|
hue_low: float = 35.0
|
|
hue_high: float = 95.0
|
|
border_ratio: float = 0.04
|
|
min_samples: int = 2048
|
|
hue_sigma_min: float = 7.0
|
|
lab_sigma_min: float = 10.0
|
|
rgb_sigma_min: float = 0.08
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TrimapSettings:
|
|
sure_bg_threshold: float = 0.92
|
|
sure_fg_threshold: float = 0.12
|
|
unknown_radius_ratio: float = 0.012
|
|
fg_safe_radius_ratio: float = 0.006
|
|
min_unknown_radius: int = 4
|
|
min_fg_safe_radius: int = 2
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AlphaPostSettings:
|
|
min_component_area_ratio: float = 0.00001
|
|
fill_hole_area_ratio: float = 0.00002
|
|
alpha_floor: float = 0.002
|
|
alpha_ceil: float = 0.998
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ForegroundSettings:
|
|
enabled: bool = True
|
|
method: str = "ml" # "ml" (pymatting) or "unmix" (legacy heuristic)
|
|
fallback_to_unmix: bool = True
|
|
ml_regularization: float = 1e-5
|
|
# Legacy "unmix" method parameters (used only when method == "unmix").
|
|
edge_low: float = 0.005
|
|
edge_high: float = 0.995
|
|
min_unmix_alpha: float = 0.08
|
|
unmix_strength: float = 0.70
|
|
local_strength: float = 0.45
|
|
local_blur_radius: int = 11
|
|
local_alpha_threshold: float = 0.92
|
|
local_bg_confidence_max: float = 0.20
|
|
green_excess_margin: float = 0.015
|
|
bg_confidence_weight: float = 0.70
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DespillSettings:
|
|
enabled: bool = True
|
|
edge_low: float = 0.005
|
|
strength: float = 0.92
|
|
green_excess_margin: float = 0.015
|
|
edge_expand_radius: int = 2
|
|
alpha_weight_floor: float = 0.35
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ModelSettings:
|
|
model_name: str = "hustvl/vitmatte-small-composition-1k"
|
|
device: str = "cuda"
|
|
matting_method: str = "vitmatte"
|
|
fallback_to_chroma_alpha: bool = False
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PipelineSettings:
|
|
chroma: ChromaSettings = ChromaSettings()
|
|
trimap: TrimapSettings = TrimapSettings()
|
|
alpha_post: AlphaPostSettings = AlphaPostSettings()
|
|
foreground: ForegroundSettings = ForegroundSettings()
|
|
despill: DespillSettings = DespillSettings()
|
|
model: ModelSettings = ModelSettings()
|