# Grab Cut Algorithm

This function block separates foreground objects from the background using an interactive segmentation approach. It offers three operation modes so you can provide different types of hints (automatic detection, a rectangular ROI, or small foreground/background masks) to guide the extraction.

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

`Image Any` Provide the source color image to segment.

`ROI` (optional, as input socket in ROI mode) Provide a rectangle to indicate the rough object location (format: \[(x1,y1),(x2,y2)]).

`Foreground` (optional, as input socket in Mask mode) Provide a grayscale mask hinting probable foreground pixels.

`Background` (optional, as input socket in Mask mode) Provide a grayscale mask hinting probable background pixels.

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

`Image Any` Segmented image where the background is removed or masked out — the foreground object remains.

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

`Grabcut Method` Select between available extraction modes:

* `Automatic` The block attempts to locate the main object automatically and extract it.
* `ROI` Use when you can provide a rectangular region around the object.
* `Mask` Use when you can supply small foreground and background mask hints.

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

* Multiple operation modes lets you pick the best approach for your image and workflow.
* Accepts both rough rectangles and fine foreground/background hints for flexible segmentation.
* Produces a clean cutout suitable for further processing or export.
* Works on color images (RGB/BGR) and returns a masked color result.

## ⚙️ Running mechanism (high level) <a href="#how-it-works" id="how-it-works"></a>

* The block reads the selected `Grabcut Method` and expects the corresponding sockets to be connected when required.
* In `Automatic` mode it finds an initial foreground region automatically and refines it.
* In `ROI` mode it uses the provided rectangle as the initial guess for extraction.
* In `Mask` mode it uses the provided small foreground/background masks to guide the extraction.
* The result is a segmented image with background removed or suppressed and is provided through the `Image Any` output socket.

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

1. Connect an image-producing block to the `Image Any` input.
2. Choose `Grabcut Method`:
   * For quick automated results pick `Automatic`.
   * For controlled extraction supply a rectangle to `ROI` and choose `ROI`.
   * For the best accuracy in challenging scenes provide small foreground and background masks and choose `Mask`.
3. When using `ROI`, supply a rectangle in the format `[(x1,y1),(x2,y2)]` from an ROI selection block.
4. When using `Mask`, supply small grayscale masks marking some foreground and background pixels.
5. Read the segmented output from the `Image Any` output socket and continue processing or save/export.

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

* If the subject is easy to isolate by color, add a color prefilter before this block. Useful blocks for that: `HSV Filter`, `RGB Mask`, or `Image Threshold`. These can help create helpful `Foreground`/`Background` masks for `Mask` mode.
* Use `Image ROI Select` to draw and provide a precise rectangle when using `ROI` mode.
* For noisy images try smoothing first with `Blur` or `Denoising` to improve contour detection in `Automatic` mode.
* Combine with `Morphological Transformations` after segmentation to clean small holes or speckles in the mask.
* If you need to visualize or save results, attach `Show Image` to preview and `Image Write` or `Image Logger` to persist outputs.
* For complex backgrounds try running `Background Subtractor` or `Background Removal (RMBG-1.4)` / `Background Removal (BiRefNet)` earlier in the flow to create a better initial mask for the Grab Cut block.

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

* If the result misses parts of the object:
  * Try `ROI` mode with a slightly larger rectangle.
  * Or provide a small `Foreground` mask marking a few object pixels.
* If too much background remains:
  * Provide `Background` mask hints marking background regions.
  * Try `Morphological Transformations` to remove residual background after segmentation.
* If no object is detected in `Automatic` mode:
  * Preprocess with `Contrast Optimization` or `Auto Contrast` to increase separation between object and background.
  * Use `ROI` mode instead if the automatic guess fails.
* Ensure any mask inputs are provided as grayscale images and that ROI coordinates are within image bounds.

This block is ideal when you need a reliable foreground extraction step before detection, measurement, or visualization.


---

# 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/image-transformations/transformation-filters/grab-cut-algorithm.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.
