# U2Net Segmentation

This function block performs salient object detection and background removal. Use it to extract a clear foreground mask and to produce a segmented image where the background is removed.

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

`Image Any` The input image to analyze (color or grayscale). Connect the image source you want to segment.

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

`Image Gray` Grayscale probability map / mask showing object likelihood.

`Image Any` Segmented image where background is removed using the mask.

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

`Model` Select between available U2Net variants (for example "U2Net (High Quality)" or "U2NetP (Fast)"). Choose higher quality for better masks or the fast model for speed.

`Input Size` Resize the model input. Smaller values increase speed; larger values improve detail at the cost of performance.

`Threshold` Controls binarization of the probability map to create the final mask. Increasing the threshold makes the mask stricter (fewer foreground pixels).

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

* Foreground extraction to produce a soft probability map and a binary mask.
* Segmented color image output that keeps only the detected foreground.
* Two model choices to trade off accuracy vs speed.
* Adjustable preprocessing size and threshold for fine control.

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

1. Connect an image source to the `Image Any` input.
2. Choose a `Model` depending on whether you prefer quality or speed.
3. Adjust `Input Size` to balance detail and processing time.
4. Tune `Threshold` to get the desired binary mask (preview using a `Show Image` block).
5. Use the `Image Gray` output when you need the mask for further processing, and the `Image Any` output for visualization or saving.

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

When run, this block produces a probability mask and a segmented image based on the currently selected model and slider settings. Use the mask output to feed subsequent processing blocks (e.g., filtering, contour detection, ROI operations).

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

* To speed up processing while keeping acceptable quality, reduce `Input Size` and use the `U2NetP (Fast)` model.
* For better-looking masks on noisy images, pass the source through `Denoising` or `Blur` before this block.
* If the subject is small relative to the image, use `Image Resize` to upsample the region of interest before segmentation.
* Preview results interactively by connecting the segmented output to `Show Image`.
* Save results automatically by linking the segmented image to `Image Logger` or `Image Write`.
* If you need alternative background removal approaches, compare results with `Background Removal (RMBG-1.4)` or `Background Removal (BiRefNet)` to see which fits your scene best.
* Use the binary mask output as input to ROI or shape-analysis blocks (for example, contour finders or measurement blocks) to extract object geometry.

(hint: useful companion blocks — `Show Image`, `Image Resize`, `Denoising`, `Image Logger`, `Image Write`, `Background Removal (RMBG-1.4)`, `Background Removal (BiRefNet)`)

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

* If masks look incomplete or too noisy: increase `Input Size` or apply `Denoising`/`Blur` to the input image.
* If the mask is too permissive (background included): raise `Threshold` to make the mask stricter.
* If processing is too slow: switch to the faster model option and/or lower the `Input Size`.
* If no output appears: confirm a valid image is connected to the `Image Any` input and preview using `Show Image`.


---

# 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/u2net-segmentation.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.
