# Background Removal (BiRefNet)

This function block performs high-quality foreground / background segmentation using the BiRefNet model. It produces a binary mask and (optionally) a green overlay visualization of the detected foreground to help you inspect results quickly.

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

`Image` RGB / BGR image to be segmented.

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

`Overlay` Image with green overlay showing segmented foreground regions.

`Mask` Binary segmentation mask (0 = background, 255 = foreground).

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

`Overlay` Toggle to enable or disable the overlay visualization output (shows green overlay over input image).

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

* High-quality segmentation suitable for objects with complex boundaries and partial transparency.
* Produces both a binary mask and a visualization overlay for quick verification.
* Automatically handles model loading and device selection (GPU if available).
* Designed to work with images of any size (model processing may internally use a fixed-size transform for inference).
* Requires additional packages to be available: transformers, torch, torchvision, pillow.

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

1. Connect an image source to the `Image` input (for example: `Camera USB`, `Camera IP (ONVIF)`, `Load Image`).
2. Toggle `Overlay` if you want a green visual overlay to be produced alongside the binary mask.
3. Inspect the outputs: use `Overlay` to preview results visually and use `Mask` for downstream processing (measurements, cropping, counting, etc.).
4. Save or log the outputs with `Image Write` or `Image Logger` if you need to store results.

## 📊 Evaluation <a href="#evaluation" id="evaluation"></a>

When run, this block returns a binary mask indicating foreground pixels and an optional overlay image where foreground regions are tinted green. Use the mask for further image operations or measurements.

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

* Preprocess with `Image Resizer` or `Image Resize` to limit input size if your source images are extremely large. This can reduce memory usage and speed up processing.
* Denoise or smooth the input using `Denoising` or `Blur` before segmentation to reduce spurious mask artifacts.
* For stronger contrast between subject and background, try `Auto Contrast` or `Adjust Colors` prior to feeding the image.
* To remove small mask holes or speckles, run the `Morphological Transformations` block on the `Mask` output, then use `Apply Mask` to produce a cleaned cutout.
* If you want a different segmentation approach for comparison or lower-dependency operation, try `Background Removal (RMBG-1.4)` and compare results.
* Visualize results interactively with `Show Image`. To record results automatically, use `Image Logger` or `Image Write`.
* Use `Image ROI` / `Image ROI Select` before segmentation when you need to segment a specific region only (reduces processing and often improves results).

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

* If the block reports missing dependencies, install the required packages (transformers, torch, torchvision, pillow) and restart the environment.
* If results look noisy or incomplete, try preprocessing with `Image Resizer`, `Denoising`, or `Auto Contrast`, and postprocess the `Mask` with `Morphological Transformations`.
* If processing is slow on large images, reduce input size with `Image Resizer` or use a smaller source image prior to this block.
* To inspect intermediate results, connect the `Overlay` or `Mask` outputs to `Show Image` or save them with `Image Write`.


---

# 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-birefnet.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.
