| Title: | Remove Image Backgrounds with Pre-Trained Segmentation Models |
|---|---|
| Description: | Remove the background from an image using pre-trained deep learning segmentation models ('U-2-Net', 'ISNet', 'BiRefNet' and others) run through the 'ONNX' Runtime via the 'onnxr' package. Given an image, a model predicts a foreground alpha matte which is composited into a cutout with a transparent (or solid-colour) background; optional closed-form alpha matting (ported from 'pymatting') refines soft edges. An R port of the Python 'rembg' package (<https://github.com/danielgatis/rembg>). Models are downloaded on first use and cached in a per-user cache directory. |
| Authors: | Troy Hernandez [aut, cre] (ORCID: <https://orcid.org/0009-0005-4248-604X>), cornball.ai [cph], Daniel Gatis [cph] (Author of the Python 'rembg' package this is ported from) |
| Maintainer: | Troy Hernandez <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-07-22 10:53:05 UTC |
| Source: | https://github.com/cran/rembg |
Models are cached in tools::R_user_dir("rembg", "cache"), the standard
per-package cache location. Set the U2NET_HOME environment variable to
override it, for example to ~/.u2net to share the cache with the Python
rembg package.
model_home()model_home()
A file path (character scalar). The directory is not created.
model_home()model_home()
Loads a segmentation model (downloading it on first use) and returns a session object that [rembg()] can reuse across many images. Building the session once and passing it to [rembg()] avoids re-loading the model each call.
new_session(model = "u2net", backend = c("cpu", "cuda", "coreml"), model_path = NULL, size = NULL, mean = NULL, std = NULL, ...)new_session(model = "u2net", backend = c("cpu", "cuda", "coreml"), model_path = NULL, size = NULL, mean = NULL, std = NULL, ...)
model |
Model name; see [rembg_models()]. Defaults to |
backend |
ONNX Runtime execution backend passed to
[onnxr::onnx_model()]: |
model_path |
Optional path to a local |
size |
Input size (pixels) for a custom |
mean |
Length-3 per-channel normalisation mean for a custom
|
std |
Length-3 per-channel normalisation standard deviation for a custom
|
... |
Reserved for future use. |
An object of class "rembg_session".
[rembg()], [rembg_models()]
# interactive() guard: new_session() downloads the model on first use if (interactive() && onnxr::onnx_is_installed()) { sess <- new_session("u2netp") sess # bring your own model: # new_session("u2net_custom", model_path = "~/.u2net/my_model.onnx") }# interactive() guard: new_session() downloads the model on first use if (interactive() && onnxr::onnx_is_installed()) { sess <- new_session("u2netp") sess # bring your own model: # new_session("u2net_custom", model_path = "~/.u2net/my_model.onnx") }
Runs a segmentation model to predict a foreground alpha matte and composites the result into a cutout with a transparent (or solid-colour) background.
rembg(input, model = "u2net", session = NULL, only_mask = FALSE, post_process_mask = FALSE, alpha_matting = FALSE, alpha_matting_foreground_threshold = 240, alpha_matting_background_threshold = 10, alpha_matting_erode_size = 10, cloth_category = NULL, points = NULL, labels = NULL, bgcolor = NULL, out = NULL, output = c("array", "raw"), ...)rembg(input, model = "u2net", session = NULL, only_mask = FALSE, post_process_mask = FALSE, alpha_matting = FALSE, alpha_matting_foreground_threshold = 240, alpha_matting_background_threshold = 10, alpha_matting_erode_size = 10, cloth_category = NULL, points = NULL, labels = NULL, bgcolor = NULL, out = NULL, output = c("array", "raw"), ...)
input |
The image to process: a file path (PNG or JPEG), a raw vector of
encoded PNG/JPEG bytes, or a numeric |
model |
Model name to use when |
session |
An [rembg_session] from [new_session()]. If |
only_mask |
If |
post_process_mask |
If |
alpha_matting |
If |
alpha_matting_foreground_threshold |
Mask values (0-255 domain) above this are treated as definite foreground in the matting trimap. Default 240. |
alpha_matting_background_threshold |
Mask values (0-255 domain) below this are treated as definite background in the matting trimap. Default 10. |
alpha_matting_erode_size |
Pixels by which the trimap foreground and background regions are eroded, leaving an unknown band for matting to solve. Default 10. |
cloth_category |
For the |
points |
For the |
labels |
For the |
bgcolor |
Optional background colour to composite the cutout onto, as a
length-3 (RGB) or length-4 (RGBA) numeric vector in |
out |
Optional output file path. If given, the result is written there as a PNG (in addition to being returned). |
output |
In-memory return type: |
... |
Passed to [new_session()] when |
Depending on output: an [h,w,4] RGBA array (or
[h,w] mask if only_mask) in [0,1], or a raw vector of
PNG bytes. If out is set, the PNG is also written to that path.
[new_session()], [rembg_models()]
# interactive() guard: rembg() downloads the model on first use if (interactive() && onnxr::onnx_is_installed()) { cutout <- rembg(system.file("extdata", "example.jpg", package = "rembg")) dim(cutout) }# interactive() guard: rembg() downloads the model on first use if (interactive() && onnxr::onnx_is_installed()) { cutout <- rembg(system.file("extdata", "example.jpg", package = "rembg")) dim(cutout) }
Returns the names of the segmentation models that [new_session()] and [rembg()] can use. Models are downloaded on first use.
rembg_models()rembg_models()
A character vector of model names.
rembg_models()rembg_models()