---
title: "WatershedComponents"
language: "en"
type: "Symbol"
summary: "WatershedComponents[image] computes the watershed transform of image, returning the result as an array in which positive integers label the catchment basins. WatershedComponents[image, marker] uses a binary image marker to indicate regions where basins may be created. WatershedComponents[video, ...] computes watershed segmentation on frames of video."
keywords: 
- watershed transform
- segmentation
- watersheds
- morphologicalwatershed
- Vincent
- Soille
- Meyer
- Beucher
- watershed
canonical_url: "https://reference.wolfram.com/language/ref/WatershedComponents.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Segmentation Analysis"
    link: "https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md"
  - 
    title: "Image Processing & Analysis"
    link: "https://reference.wolfram.com/language/guide/ImageProcessing.en.md"
  - 
    title: "Cluster Analysis"
    link: "https://reference.wolfram.com/language/guide/ClusterAnalysis.en.md"
  - 
    title: "Video Analysis"
    link: "https://reference.wolfram.com/language/guide/VideoAnalysis.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "ClusteringComponents"
    link: "https://reference.wolfram.com/language/ref/ClusteringComponents.en.md"
  - 
    title: "ImageForestingComponents"
    link: "https://reference.wolfram.com/language/ref/ImageForestingComponents.en.md"
  - 
    title: "GrowCutComponents"
    link: "https://reference.wolfram.com/language/ref/GrowCutComponents.en.md"
  - 
    title: "ComponentMeasurements"
    link: "https://reference.wolfram.com/language/ref/ComponentMeasurements.en.md"
  - 
    title: "Colorize"
    link: "https://reference.wolfram.com/language/ref/Colorize.en.md"
  - 
    title: "MorphologicalComponents"
    link: "https://reference.wolfram.com/language/ref/MorphologicalComponents.en.md"
  - 
    title: "MorphologicalBinarize"
    link: "https://reference.wolfram.com/language/ref/MorphologicalBinarize.en.md"
---
# WatershedComponents

WatershedComponents[image] computes the watershed transform of image, returning the result as an array in which positive integers label the catchment basins.

WatershedComponents[image, marker] uses a binary image marker to indicate regions where basins may be created.

WatershedComponents[video, …] computes watershed segmentation on frames of video.

## Details and Options

* ``WatershedComponents``, also known as watershed transform, is an image segmentation algorithm that segments areas of lower intensity (basins) enclosed in between brighter ridges.

* ``WatershedComponents[image]`` finds basins at each regional minimum in ``image``.

* ``WatershedComponents`` works with 2D and 3D images as well as video inputs.

* ``WatershedComponents`` works with binary, grayscale, and multichannel images, operating on the intensity averaged over all channels.

* In the returned label array, zeros represent positions that do not belong to any foreground component.

* ``WatershedComponents[image, marker]`` finds basins only at the positions corresponding to foreground regions in a binary image ``marker``.

* The target region ``marker`` can be any of the following:

|                 |                     |
| --------------- | ------------------- |
| markerimage     | a marker image      |
| {pos1, pos2, …} | a list of positions |

* Positions ``posi`` are assumed to be in the standard image coordinate system.

[image]

* Typically, nonzero elements of ``marker`` are treated as seeds for the segmentation.

* The following options can be specified:

|                  |           |                                     |
| ---------------- | --------- | ----------------------------------- |
| CornerNeighbors  | Automatic | whether to include corner neighbors |
| Method           | Automatic | the method to use                   |

* Possible ``Method`` settings include:

|     |     |
| --- | --- |
| "Watershed" | morphological watershed method (Meyer) (default) |
| "Basins" | modified watershed algorithm (Beucher, Meyer)  |
| "Rainfall" | gradient descent or rainfall algorithm (Osma-Ruiz) |
| "Immersion" | watershed immersion algorithm (Vincent–Soille) |
| {"MinimumSaliency", t} | gradient descent algorithm that merges adjacent basins if their minimum boundary height is less than t |

* The ``"Watershed"`` and ``"Immersion"`` methods return the watershed lines, represented as 0s in the label array.

* With the ``"MinimumSaliency"`` method, ``CornerNeighbors -> False`` is always used. All other methods by default use ``CornerNeighbors -> True``.

---

## Examples (20)

### Basic Examples (2)

Watershed segmentation of an image with two regional minima:

```wl
In[1]:= WatershedComponents[[image]]//MatrixForm

Out[1]//MatrixForm=
(⁠|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
| - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | -  ... 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |⁠)
```

Visualize the segmentation:

```wl
In[2]:= Colorize[%]

Out[2]= [image]
```

---

Watershed segmentation of a 3D image:

```wl
In[1]:= WatershedComponents[[image]]//Colorize

Out[1]= [image]
```

### Scope (3)

Mark two regions with a marker image:

```wl
In[1]:= WatershedComponents[[image], [image]]//Colorize

Out[1]= [image]
```

---

Mark two regions with a list of positions:

```wl
In[1]:= WatershedComponents[[image], {{190, 180}, {190, 45}}]//Colorize

Out[1]= [image]
```

---

Mark 3D regions and background with a binary mask:

```wl
In[1]:= i = [image];marker = [image];

In[2]:=
ws = WatershedComponents[i, marker];
Image3D[Colorize[SelectComponents[ws, "Count", 2]], Options[i, BoxRatios]]

Out[2]= [image]
```

### Options (3)

#### CornerNeighbors (2)

By default, diagonally adjacent pixels are considered as neighbors:

```wl
In[1]:= WatershedComponents[[image]]//Colorize

Out[1]= [image]
```

Use ``CornerNeighbors -> False`` to only consider horizontally and vertically adjacent pixels as neighbors:

```wl
In[2]:= WatershedComponents[[image], CornerNeighbors -> False]//Colorize

Out[2]= [image]
```

---

``CornerNeighbors`` setting is ignored with ``Method -> "MinimumSaliency"`` :

```wl
In[1]:= img = [image];

In[2]:= WatershedComponents[img, Method -> {"MinimumSaliency", .2}, CornerNeighbors -> True] == WatershedComponents[img, Method -> {"MinimumSaliency", .2}, CornerNeighbors -> False]

Out[2]= True
```

#### Method (1)

Use a minimum saliency method to segment tiles in an image:

```wl
In[1]:= img = ColorNegate@[image];

In[2]:= WatershedComponents[img, Method -> {"MinimumSaliency", .3}]//Colorize

Out[2]= [image]
```

Lower saliency thresholds typically yield over-segmented results:

```wl
In[3]:= WatershedComponents[img, Method -> {"MinimumSaliency", 0}]//Colorize

Out[3]= [image]
```

Use higher saliency thresholds to merge similar neighboring components:

```wl
In[4]:= WatershedComponents[img, Method -> {"MinimumSaliency", .4}]//Colorize

Out[4]= [image]
```

### Applications (8)

Separate overlapping components using the distance transform image:

```wl
In[1]:= img = [image];

In[2]:= dt = ImageAdjust[ColorNegate[DistanceTransform[img]]]

Out[2]= [image]
```

Visualize the separation of the overlapping components:

```wl
In[3]:= ImageMultiply[img, Image[WatershedComponents[dt], "Bit"]]

Out[3]= [image]
```

---

Use watershed segmentation to separate overlapping 3D components:

```wl
In[1]:= image = [image];
```

Compute distance transform of the volume:

```wl
In[2]:= iw = ColorNegate@ImageAdjust@DistanceTransform@image;
```

Run watershed segmentation on the distance transform of a 3D volume and visualize the segmentation:

```wl
In[3]:=
cw = WatershedComponents[iw];
cw *= ImageData[image];
Colorize[cw]

Out[3]= [image]
```

Use 3D markers to enhance the segmentation:

```wl
In[4]:= markers = [image];

In[5]:=
cwm = WatershedComponents[iw, markers];
cwm *= ImageData[image];
Colorize[cwm]

Out[5]= [image]
```

---

Binary image created from watershed ridges after removing the background:

```wl
In[1]:= Image[WatershedComponents[Threshold[[image], 0.18]] , "Bit"]

Out[1]= [image]
```

---

Heart chamber segmentation:

```wl
In[1]:= img = [image];
```

Use local maxima of smoothed data as markers:

```wl
In[2]:=
markers = MaxDetect[GaussianFilter[img, 25], Padding -> 1];
HighlightImage[img, markers, Method -> {"DiskMarkers", 5}]

Out[2]= [image]
```

Watershed segmentation using detected marker point:

```wl
In[3]:= WatershedComponents[GradientFilter[img, 3], markers]//Colorize

Out[3]= [image]
```

---

Preprocessing the image by filling shallow regional maxima helps reduce over-segmentation:

```wl
In[1]:= img = [image];

In[2]:= WatershedComponents[FillingTransform[ColorNegate[img], 0.2, Padding -> 1]]//Colorize

Out[2]= [image]
```

---

Use a combination of ``GradientFilter`` and ``FillingTransform`` to segment an image:

```wl
In[1]:= WatershedComponents[FillingTransform[GradientFilter[[image], 1], 0.05, Padding -> 1]]//Colorize

Out[1]= [image]
```

---

Approximate the Voronoi diagram of a set of points:

```wl
In[1]:= i = [image];
```

Compute watershed transform of the distance transform of the image:

```wl
In[2]:=
segs = WatershedComponents[DistanceTransform[i]];
Colorize[segs]

Out[2]= [image]
```

Show the boundary of the components (Voronoi diagram) and the initial points:

```wl
In[3]:= HighlightImage[Image[segs, "Bit"], {Opacity[1], ColorNegate[i]}]

Out[3]= [image]
```

---

Over-segmentation can be used in a creative way to finely texture the background in an image:

```wl
In[1]:= Image[WatershedComponents[Threshold[[image], 0.6]] , "Bit"]

Out[1]= [image]
```

### Properties & Relations (2)

The methods ``"Basins"``, ``"Rainfall"``, and ``"MinimumSaliency"`` assign all pixels to a catchment basin:

```wl
In[1]:= img = [image];

In[2]:= Table[WatershedComponents[img, Method -> method]//Colorize, {method, {"Basins", "Rainfall", {"MinimumSaliency", 0}}}]

Out[2]= {[image], [image], [image]}
```

The methods ``"Watershed"`` and ``"Immersion"`` compute watershed lines separating the components:

```wl
In[3]:= Table[WatershedComponents[img, Method -> method]//Colorize, {method, {"Watershed", "Immersion"}}]

Out[3]= {[image], [image]}
```

Use the default method to compute watershed lines separating the components:

```wl
In[4]:= WatershedComponents[img]//Colorize

Out[4]= [image]
```

---

Typically, plateau pixels are separated based on their proximity to regional minima:

```wl
In[1]:=
img = [image];
Colorize[WatershedComponents[img], ImageSize -> 100]

Out[1]= [image]
```

The method ``"MinimumSaliency"`` assigns all plateau pixels to the same component:

```wl
In[2]:= Colorize[WatershedComponents[img, Method -> {"MinimumSaliency", 0}], ImageSize -> 100]

Out[2]= [image]
```

### Possible Issues (1)

Typically, images do not have one regional minimum per component:

```wl
In[1]:= MinDetect[[image]]

Out[1]= [image]
```

Watershed segmentation is conceptually using regional minima as markers:

```wl
In[2]:= WatershedComponents[[image]]//Colorize

Out[2]= [image]
```

Computing watershed on the gradient image typically gives a more desired segmentation:

```wl
In[3]:= g = GradientFilter[[image], 1]

Out[3]= [image]

In[4]:= WatershedComponents[g]//Colorize

Out[4]= [image]
```

### Neat Examples (1)

Solve a maze puzzle using watershed transform:

```wl
In[1]:=
i = [image];
w = ColorNegate[Image[WatershedComponents[i], "Bit"]];
HighlightImage[i, Dilation[w, 1]]

Out[1]= [image]
```

## See Also

* [`ClusteringComponents`](https://reference.wolfram.com/language/ref/ClusteringComponents.en.md)
* [`ImageForestingComponents`](https://reference.wolfram.com/language/ref/ImageForestingComponents.en.md)
* [`GrowCutComponents`](https://reference.wolfram.com/language/ref/GrowCutComponents.en.md)
* [`ComponentMeasurements`](https://reference.wolfram.com/language/ref/ComponentMeasurements.en.md)
* [`Colorize`](https://reference.wolfram.com/language/ref/Colorize.en.md)
* [`MorphologicalComponents`](https://reference.wolfram.com/language/ref/MorphologicalComponents.en.md)
* [`MorphologicalBinarize`](https://reference.wolfram.com/language/ref/MorphologicalBinarize.en.md)

## Related Guides

* [Segmentation Analysis](https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md)
* [Image Processing & Analysis](https://reference.wolfram.com/language/guide/ImageProcessing.en.md)
* [Cluster Analysis](https://reference.wolfram.com/language/guide/ClusterAnalysis.en.md)
* [Video Analysis](https://reference.wolfram.com/language/guide/VideoAnalysis.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2025 (14.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn142.en.md)