# Background Removal (RMBG-1.4)

This function block removes background from an input image using a pretrained image-segmentation model. It generates a clean foreground cutout and a binary mask (foreground = 255, background = 0). The model is automatically prepared when you first run the block.

## 📥 Inputs <a href="#inputs" id="inputs"></a>

`Image` A RGB or BGR image to process.

## 📤 Outputs <a href="#outputs" id="outputs"></a>

`Cutout` Foreground cutout image (background removed). This can be either RGB on black background or RGBA with alpha depending on the chosen option.

`Mask` Binary mask image where foreground pixels are 255 and background pixels are 0.

## 🕹️ Controls <a href="#controls" id="controls"></a>

`Keep RGB on black` When unchecked (default) the block returns an RGB cutout on a black background plus the mask. When checked the block returns an RGBA image (alpha channel) plus the mask.

## 🎨 Features <a href="#features" id="features"></a>

* Generates a binary segmentation mask separating foreground from background.
* Option to return RGBA (alpha) or RGB cutout on black background for flexible downstream use.
* Model files are automatically fetched the first time the block runs (internet access required).
* Works with single images from cameras, files, or streams.

## 📊 How it runs <a href="#evaluation" id="evaluation"></a>

When the block runs it processes the incoming image with the segmentation model to produce a binary mask. The mask is used to create a foreground cutout which is sent together with the mask through the outputs. Model initialization happens automatically (first run may take longer due to model download).

## 📝 Usage Recommendations <a href="#usage" id="usage"></a>

* Feed this block with a clear RGB/BGR image from any image source such as `Load Image`, `Camera USB`, `Stream Reader` or a saved frame.
* Use the `Keep RGB on black` option depending on whether you need an alpha channel or a simple RGB cutout.
* Combine the outputs with other blocks to refine results or save outputs.

## 💡 Tips and Tricks <a href="#tips-and-tricks" id="tips-and-tricks"></a>

* Improve speed with smaller inputs: use `Image Resizer` before this block if your input is very large.
* Crop to the area of interest first using `Image ROI` or `Image ROI Select` to reduce processing time and improve mask quality.
* For additional visual checks, send both `Cutout` and `Mask` into `Show Image` or place them side-by-side with `Collage Images`.
* Save results automatically with `Image Logger`, `Image Write` or `Multi Image Write` when you want to archive cutouts and masks.
* If you need even higher-quality segmentation for difficult boundaries, try the alternative block `Background Removal (BiRefNet)` (available in the AI Applications list).
* Use `Apply Mask` if you want to combine the original image and the mask in a custom way (for example to keep some blended background or visualize masked areas).

## 🛠️ Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

* If the block reports missing packages, install the required libraries (transformers, pillow) on your system and re-run.
* First run may be slower due to automatic model download — subsequent runs will be faster.
* If results include stray background fragments, try cropping the image with `Image ROI` or increase the input quality (better lighting / contrast).
* For very noisy images, apply a mild `Blur` or a color filtering step (e.g., `HSV Filter`) before background removal to improve segmentation stability.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.augelab.com/function-blocks/ai-blocks/background-removal-rmbg-1.4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
