How local AI background removal works

Background removal is a pipeline. The model predicts a matte at its working resolution, then the application maps that result back to the source image and encodes a new file.

Published

1. Decode the source image

The application first decodes JPEG, PNG, WebP, or AVIF bytes into pixels. Orientation matters here because the model and final compositor need to agree about which pixel is where.

In the browser, decoding uses browser image facilities. The native Node and CLI path uses Sharp and libvips for decode and orientation.

No segmentation has happened yet. This stage only turns the input file into pixels the rest of the pipeline can use.

2. Prepare the model input

bgcut's public model input is 512 by 512. The application resizes the source for the model and applies the normalization expected by the model.

The browser WebGPU path uses TypeGPU for resize and ImageNet normalization. The browser WebAssembly path uses canvas resize with the same normalization. The native path uses linear resize and ImageNet normalization.

The original source dimensions are kept because the 512 by 512 model input is not the final output size.

3. Run segmentation inference

The ONNX model predicts a matte that describes foreground membership across the image. bgcut uses ONNX Runtime to execute the model locally.

Browser automatic mode tries WebGPU and can fall back to WebAssembly. The native CLI and Node.js API use native ONNX Runtime with WebGPU or CPU according to engine selection.

The model file is large because it contains the learned parameters. The npm package does not bundle the model artifacts. bgcut validates cached model size and SHA-256 before reuse.

4. Restore the matte and export

The predicted matte is restored to the source image dimensions. bgcut then combines that matte with the original pixels to produce transparency at the original size.

PNG and WebP can carry alpha transparency. JPEG cannot, so a JPEG result needs an opaque background.

This last step is why the model's 512 by 512 input does not mean the downloaded cutout is limited to 512 by 512.