Skip to content

Improve Remove BG with classical edge-aware segmentation - #29

Merged
Vxiey merged 12 commits into
mainfrom
fix/classical-background-removal
Sep 13, 2026
Merged

Vxiey merged 12 commits into
mainfrom
fix/classical-background-removal

Conversation

@Vxiey

@Vxiey Vxiey commented Sep 13, 2026

Copy link
Copy Markdown
Owner

Summary

Rebuilds Remove BG as a fully local classical computer-vision pipeline. It uses no AI/neural model, no cloud API, no model download, no OpenCV and no new runtime dependency.

What changed

  • Replaces the old single-reference-colour flood fill with adaptive border colour modelling.
  • Uses deterministic 1/2/4-colour clustering for plain, uneven and tiled/room backgrounds.
  • Filters high-gradient border samples so a subject touching the frame is less likely to become a background prototype.
  • Uses chromatic RGB edge barriers, not only brightness edges, to protect skin, clothing and other equal-luma colour boundaries.
  • Region growing combines global border-colour confidence with conservative local-gradient continuation for shadows and mild lighting gradients.
  • Adds a classical trimap/confidence layer: uncertain pixels remain foreground; only verified border-connected background is removed.
  • Adds connected-component safety checks and fails closed if the remaining subject is implausibly small or fragmented.
  • Feathers only the immediate cut contour; deep foreground stays opaque and deep background stays transparent.
  • Runs segmentation on a bounded proxy (max ~900k px / 1200 px dimension) and projects the final mask back to source resolution.
  • Preserves existing alpha and cancellation behavior.
  • Emits diagnostic metadata for background clusters/weights/spreads, edge threshold, trimap percentages and largest foreground component.

Regression coverage

  • Internal same-colour details that are not border-connected stay opaque.
  • Multi-colour/tiled background is removed while a skin-like central subject remains intact.
  • Similar-luma but different-colour subject edges are protected.
  • Existing transparency remains untouched.
  • Uniform images fail closed instead of deleting the whole image.
  • Cancellation is honored.
  • Tests assert that ONNX Runtime, rembg, OpenCV, TensorFlow and Torch are not required.

The implementation is intentionally conservative: preserving some difficult background is preferred over cutting holes through a face or other foreground detail.

@Vxiey
Vxiey merged commit 02576c2 into main Sep 13, 2026
6 checks passed
@Vxiey
Vxiey deleted the fix/classical-background-removal branch September 13, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant