panoramic

Rembg – background segmentation tool using machine learning

Last Updated on March 6, 2023

In Operation

We can remove a background from a local PNG image with the command (other image formats are supported):

$ rembg i input.png output.png

Replace the i with p for a folder as the input, and replace the i with s to run as a web server.

There are various command-line options:

-a --alpha matting – apply alpha matting;
-af --alpha-matting-foreground-threshold – trimap foreground threshold; defaults to 240;
-ab --alpha-matting-background-threshold – trimap background threshold; defaults to 10;
-ae --alpha-matting-erode-size – erode size; defaults to 10;
-om --only-mask – output only the mask;
-m --model – define the model to use.

Let’s say we want to remove the background from this image of an adorable labrador retriever.

labrador retriever

Issue the command:

$ rembg i labrador-retriever.jpg labrador-retriever-output.jpg

labrador retriever

The output image unfortunately suffers from a dark outline. Let’s apply alpha matting with an erode size of 15 to improve the appearance.

$ rembg i -a -ae 15 labrador-retriever.jpg labrador-retriever-output-with-alpha-matting.jpg

labrador retriever

Rembg has done an admirable job here.

Next page: Page 3 – Human segmentation model

Pages in this article:
Page 1 – Introduction / Getting Started
Page 2 – In Operation
Page 3 – Human segmentation model
Page 4 – Summary

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Please read our FAQ before making a comment.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments