Split trimap and dropout-fill segmentation backends

Add SegmentationSettings.fill_backend / fill_model_name so the dropout-fill can
use a different (higher-recall) segmenter than the trimap. Default is now the
combination the investigation converged on: BiRefNet builds the trimap (reliable
topology -- finger gap removed, strands kept as sure_fg), anime-seg drives the
fill (more, more-coherent anime wisps). Because the fill only adds (max), a
higher-recall-but-less-reliable mask there is harmless.

On TestImage2 this eliminates the strand breakage a full anime-seg pipeline
caused (lost 699 -> 0) while keeping ~91% of its extra wisp recall (gained
1133 -> 1031). Strands stay continuous; the recovered wisps are anime-seg's
coherent curves rather than BiRefNet's grainy fill.

Also adds an optional unsharp-mask knob (segmentation.sharpen, default off).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 11:17:46 +08:00
parent 08378e57f8
commit 9805b51b99
4 changed files with 45 additions and 2 deletions
+13
View File
@@ -111,6 +111,19 @@ class AnimeSegSegmenter:
return np.asarray(mask_img, dtype=np.float32) / 255.0
def unsharp_mask(mask: np.ndarray, amount: float, radius: int) -> np.ndarray:
"""Sharpen a soft mask: raise thin-strand peaks and crisp the blur so strands
reach the sure-foreground threshold instead of dissolving into the unknown band."""
if amount <= 0:
return mask
from .deps import require_cv2
cv2 = require_cv2()
k = max(1, radius) * 2 + 1
blur = cv2.GaussianBlur(mask, (k, k), 0)
return np.clip(mask + amount * (mask - blur), 0.0, 1.0).astype(np.float32)
def make_segmenter(settings: SegmentationSettings):
if settings.backend == "birefnet":
return BiRefNetSegmenter(settings)