A spatial omics cell segmentation algorithm that failed spectacularly - somehow became a good anime background remover instead.
A lightweight, non-AI background removal tool for anime-style images with known solid-color backgrounds, specially suited for AIGC assets, outperforming deep-learning solutions like RMBG-2.0 on expected context.
Despite using zero AI models, our method consistently produces cleaner alpha mattes on anime images — especially at semi-transparent edges, fine line art, and anti-aliased boundaries where RMBG-2.0 tends to over-smooth or introduce artifacts.
| Scenario | Input | Ours | RMBG-2.0 | Original Size | Threshold | Border width |
|---|---|---|---|---|---|---|
| White BG | ![]() |
![]() |
![]() |
160 x 160 | 0.0375 | 2 |
| Red BG | ![]() |
![]() |
![]() |
320 x 320 | 0.2 | 2 |
Testing images are all 2752x1536 images generated with Nano Banana 2, we are showing the character's heads in these results for example.
pip install -r requirements.txtDependencies: numpy, opencv-python, scipy, scikit-image
python ProcessBG.py -i <input> -o <output> --bg_color <R,G,B> [options]| Argument | Required | Default | Description |
|---|---|---|---|
-i, --input |
Yes | — | Path to input image |
-o, --output |
Yes | — | Path to output RGBA image (PNG recommended) |
--bg_color |
Yes | — | Background color, e.g. 255,255,255 |
--transparent_thres |
No | 0.0625 |
Minimum alpha for semi-transparent regions |
--border_width |
No | 2 |
Width of anime-style border strokes (px) |
# White background removal
python ProcessBG.py -i character.png -o character_rgba.png --bg_color 255,255,255
# Red background with thicker border lines
python ProcessBG.py -i character.png -o character_rgba.png --bg_color 255,0,0 --border_width 3The pipeline consists of four stages:
-
Alpha Initialization — For each pixel, compute the minimal alpha needed to explain the observed color given the known background. The estimated foreground color is distributed across a 4-neighborhood to produce an alpha matte robust to JPEG artifacts.
-
Cellular Automata Refinement — Iterative disk-neighborhood voting marches the alpha matte toward object boundaries. A pixel becomes opaque only if ≥80% of its neighbors are opaque/semi-transparent and ≤10% are transparent (preventing over-expansion into mixed regions). Symmetrically, a pixel becomes transparent only if ≥80% of its neighbors are transparent and ≤10% are opaque. Remaining semi-transparent pixels are gamma-corrected to suppress residual background bleed. Converges within 256 iterations.
-
Regional Content Analysis — Connected components of candidate background regions are classified by boundary smoothness (via Sobel gradients). Morphological criteria (skeleton analysis for tiny regions, erosion-based checks for rough/textured regions) recover fine foreground details like thin lines and anime-style contour strokes.
-
Foreground Color Reconstruction — With the final alpha matte, the foreground RGB is recovered by inverting the alpha compositing. This removes background color bleed from semi-transparent pixels, producing clean RGBA output.
- Requires a known, uniform background color (not suitable for natural photos with complex backgrounds).
- Optimized for AIGC assets with clear contour lines and sharp background contrast.
- Does not handle semi-transparent foreground elements.
MIT License — see LICENSE for details.





