---
title: "MorphologicalComponents"
language: "en"
type: "Symbol"
summary: "MorphologicalComponents[image] gives an array in which each pixel of image is replaced by an integer index representing the connected foreground image component in which the pixel lies. MorphologicalComponents[image, t] treats values above t as foreground. MorphologicalComponents[video, ...] computes connected components in frames of video."
keywords: 
- morphological components
- find features
- segmentation
- feature extraction
- locate features
- connected components
- labelling
- N4
- N8
- feature detection
- find clusters in image
- computer vision
- blob extraction
- blob analysis
- blob
- morphological clustering
- bwconncomp
- bwlabel
canonical_url: "https://reference.wolfram.com/language/ref/MorphologicalComponents.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Mathematical Morphology"
    link: "https://reference.wolfram.com/language/guide/MathematicalMorphology.en.md"
  - 
    title: "3D Images"
    link: "https://reference.wolfram.com/language/guide/3DImages.en.md"
  - 
    title: "Segmentation Analysis"
    link: "https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md"
  - 
    title: "Video Analysis"
    link: "https://reference.wolfram.com/language/guide/VideoAnalysis.en.md"
  - 
    title: "Image Processing & Analysis"
    link: "https://reference.wolfram.com/language/guide/ImageProcessing.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
  - 
    title: "Image Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "MorphologicalPerimeter"
    link: "https://reference.wolfram.com/language/ref/MorphologicalPerimeter.en.md"
  - 
    title: "ComponentMeasurements"
    link: "https://reference.wolfram.com/language/ref/ComponentMeasurements.en.md"
  - 
    title: "SelectComponents"
    link: "https://reference.wolfram.com/language/ref/SelectComponents.en.md"
  - 
    title: "WatershedComponents"
    link: "https://reference.wolfram.com/language/ref/WatershedComponents.en.md"
  - 
    title: "ClusteringComponents"
    link: "https://reference.wolfram.com/language/ref/ClusteringComponents.en.md"
  - 
    title: "Colorize"
    link: "https://reference.wolfram.com/language/ref/Colorize.en.md"
  - 
    title: "HighlightImage"
    link: "https://reference.wolfram.com/language/ref/HighlightImage.en.md"
  - 
    title: "HighlightVideo"
    link: "https://reference.wolfram.com/language/ref/HighlightVideo.en.md"
  - 
    title: "VideoObjectTracking"
    link: "https://reference.wolfram.com/language/ref/VideoObjectTracking.en.md"
related_tutorials: 
  - 
    title: "Image Processing"
    link: "https://reference.wolfram.com/language/tutorial/ImageProcessing.en.md"
---
# MorphologicalComponents

MorphologicalComponents[image] gives an array in which each pixel of image is replaced by an integer index representing the connected foreground image component in which the pixel lies.

MorphologicalComponents[image, t] treats values above t as foreground.

MorphologicalComponents[video, …] computes connected components in frames of video.

## Details and Options

* ``MorphologicalComponents``, also known as image connected components, is a binary image segmentation technique that segments image foreground based on pixel connectivity.

* ``MorphologicalComponents`` assigns sequential integers to different connected components and 0 to pixels that correspond to the background in the image.

* ``MorphologicalComponents[image]`` is equivalent to ``MorphologicalComponents[image, 0]``.

* ``MorphologicalComponents`` works with binary, grayscale, and other images.

* The following options can be specified:

|                  |             |                                     |
| ---------------- | ----------- | ----------------------------------- |
| CornerNeighbors  | True        | whether to include corner neighbors |
| Method           | "Connected" | connectivity method                 |
| Padding          | 0           | padding method to use               |

* The following ``Method`` settings can be specified:

|                |                                                      |
| -------------- | ---------------------------------------------------- |
| "Connected"    | labels connected components                          |
| "Nested"       | labels nested connected components                   |
| "Convex"       | labels objects within non-overlapping convex regions |
| "ConvexHull"   | finds non-overlapping convex hulls                   |
| "BoundingBox"  | finds non-overlapping bounding boxes                 |
| "BoundingDisk" | finds non-overlapping bounding disks                 |

* ``MorphologicalComponents[image, Method -> "Connected"]`` also works with ``Image3D`` objects.

---

## Examples (18)

### Basic Examples (1)

Find the connected components in a binary image:

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

Out[1]= [image]
```

### Scope (4)

Segment a binary image:

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

Out[1]= [image]
```

---

Segment a grayscale image, using all nonzero pixels as foreground:

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

Out[1]= [image]
```

Specify a background threshold:

```wl
In[2]:= MorphologicalComponents[[image], .2]//Colorize

Out[2]= [image]
```

---

Connected component labels:

```wl
In[1]:=
MorphologicalComponents[(⁠|   |   |   |   |
| - | - | - | - |
| 0 | 0 | 1 | 2 |
| 0 | 0 | 3 | 4 |
| 5 | 0 | 0 | 0 |
| 6 | 7 | 0 | 0 |⁠)] // MatrixForm

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

---

Connected components of a 3D image:

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

Out[1]= [image]
```

### Options (10)

#### CornerNeighbors (3)

8-connected objects:

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

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

---

4-connected objects:

```wl
In[1]:= MorphologicalComponents[[image], CornerNeighbors -> False] // MatrixForm

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

---

Connectedness in 3D:

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

In[2]:= Colorize@MorphologicalComponents[i, CornerNeighbors -> #]& /@ {True, False}

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

#### Method (6)

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

Out[1]= [image]
```

---

Find the bounding boxes of connected components:

```wl
In[1]:= MorphologicalComponents[[image], Method -> "BoundingBox"]// Colorize

Out[1]= [image]
```

---

Find the non-overlapping bounding disks:

```wl
In[1]:= MorphologicalComponents[[image], Method -> "BoundingDisk"]// Colorize

Out[1]= [image]
```

---

Find components whose convex hulls do not overlap:

```wl
In[1]:= MorphologicalComponents[[image], Method -> "Convex"]// Colorize

Out[1]= [image]
```

---

Non-overlapping convex components:

```wl
In[1]:= MorphologicalComponents[[image], Method -> "ConvexHull"]// Colorize

Out[1]= [image]
```

---

Label each component the same as its embedding component:

```wl
In[1]:= MorphologicalComponents[[image], Method -> "Nested"]//Colorize

Out[1]= [image]
```

#### Padding (1)

By default, zero padding is used:

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

In[2]:= MorphologicalComponents[i]//Colorize

Out[2]= [image]
```

Using ``Padding -> 1``, all components connected to the border are assumed to be connected:

```wl
In[3]:= MorphologicalComponents[i, Padding -> 1]//Colorize

Out[3]= [image]
```

### Applications (2)

Count the number of bright spots, specifying an explicit thresholding value:

```wl
In[1]:= MorphologicalComponents[[image], 0.8]//Max

Out[1]= 3
```

---

Count the number of pixels in each connected component of an image:

```wl
In[1]:= Tally[Flatten[MorphologicalComponents[[image]]]]

Out[1]= {{0, 11086}, {1, 327}, {2, 739}, {3, 348}, {4, 201}, {5, 171}, {6, 433}, {7, 1043}, {8, 502}}
```

### Neat Examples (1)

Find connected regions in a map:

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

Out[1]= [image]
```

## See Also

* [`MorphologicalPerimeter`](https://reference.wolfram.com/language/ref/MorphologicalPerimeter.en.md)
* [`ComponentMeasurements`](https://reference.wolfram.com/language/ref/ComponentMeasurements.en.md)
* [`SelectComponents`](https://reference.wolfram.com/language/ref/SelectComponents.en.md)
* [`WatershedComponents`](https://reference.wolfram.com/language/ref/WatershedComponents.en.md)
* [`ClusteringComponents`](https://reference.wolfram.com/language/ref/ClusteringComponents.en.md)
* [`Colorize`](https://reference.wolfram.com/language/ref/Colorize.en.md)
* [`HighlightImage`](https://reference.wolfram.com/language/ref/HighlightImage.en.md)
* [`HighlightVideo`](https://reference.wolfram.com/language/ref/HighlightVideo.en.md)
* [`VideoObjectTracking`](https://reference.wolfram.com/language/ref/VideoObjectTracking.en.md)

## Tech Notes

* [Image Processing](https://reference.wolfram.com/language/tutorial/ImageProcessing.en.md)

## Related Guides

* [Mathematical Morphology](https://reference.wolfram.com/language/guide/MathematicalMorphology.en.md)
* [3D Images](https://reference.wolfram.com/language/guide/3DImages.en.md)
* [Segmentation Analysis](https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md)
* [Video Analysis](https://reference.wolfram.com/language/guide/VideoAnalysis.en.md)
* [Image Processing & Analysis](https://reference.wolfram.com/language/guide/ImageProcessing.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)
* [Image Computation: Update History](https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md)

## History

* [Introduced in 2008 (7.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn70.en.md) \| [Updated in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) ▪ [2012 (9.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn90.en.md) ▪ [2025 (14.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn142.en.md)