> For the complete documentation index, see [llms.txt](https://docs.augelab.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.augelab.com/function-blocks/blocks-reference/image-transformations/transformation-filters/watershed-algorithm.md).

# Watershed Algorithm

This function block performs region-based segmentation using supplied foreground and background masks to separate touching or overlapping objects. It returns a visual overlay with boundaries and a labeled segmentation map for downstream processing.

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

`Image Any` The original color or grayscale image to segment.

`Fore Ground` A binary/grayscale image indicating sure-foreground regions (objects).

`Back Ground` A binary/grayscale image indicating sure-background regions.

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

`Image Any` The original image with detected region boundaries overlaid (visual result).

`Shed Image` A labeled image (grayscale) representing segmented regions / markers.

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

This function block has no adjustable widgets. Provide accurate `Fore Ground` and `Back Ground` inputs to control the segmentation result.

## ⚙️ Running mechanism <a href="#running-mechanism" id="running-mechanism"></a>

* The block uses the provided `Fore Ground` and `Back Ground` masks to determine which pixels are definitely object and which are definitely background.
* It identifies the unknown region between these masks and assigns intermediate markers.
* A segmentation pass separates regions, draws the detected boundaries on a copy of the input image, and produces a labeled `Shed Image` where each segmented region has a distinct marker value.
* The overlaid image highlights boundaries for quick visual inspection while the labeled image is suitable for measurements or counting.

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

* Works well for separating touching objects when clear foreground/background masks are provided.
* Produces both a visual overlay for inspection and a labeled output useful for further processing (measurements, counting, extraction).
* Accepts color or grayscale input images.

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

1. Prepare a clear `Fore Ground` mask covering the interior of objects and a complementary `Back Ground` mask for the background.
2. Connect the original image to `Image Any` and the two masks to their respective inputs.
3. Run the block to obtain the segmented overlay and the labeled `Shed Image`.
4. Use the labeled image for downstream tasks such as object counting or extracting individual regions.

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

* Use `Image Threshold`, `Image Adaptive Threshold`, or `HSV Filter` to create initial binary masks from the input image.
* Clean up small holes and noise in masks with `Morphological Transformations` or `Denoising` before feeding them into this block.
* For difficult backgrounds, try `Grab Cut Algorithm` or `Background Subtractor` to generate better foreground/background candidates.
* Restrict processing to a region of interest with `Image ROI` or `Image ROI Select` to speed up segmentation and avoid irrelevant areas.
* Preview masks and results with `Show Image` and save useful frames using `Image Logger` or `Image Write`.
* After segmentation, use `Find Contour`, `Minimum Rectangle`, or `Measure Object Distance` to extract region properties and measurements from the labeled `Shed Image`.

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

* Poor segmentation or merged objects: check that `Fore Ground` covers only object interiors and `Back Ground` covers only background; add morphological opening/closing to refine masks.
* No boundaries visible: verify that the input images and masks are valid image types (not empty) and that masks are binary or clearly contrasted.
* Over-segmentation (too many small regions): smooth or simplify masks using `Blur`, `Denoising`, or `Morphological Transformations` before segmentation.
* Under-segmentation (objects not separated): increase the accuracy of foreground markers (use stronger object markers or manual ROI selection) to help separate touching objects.
