# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# bwconncomp

Find connected components in binary image

## Syntax

• ``CC = bwconncomp(BW)``
example
• ``CC = bwconncomp(BW,conn)``
example

## Description

example

````CC = bwconncomp(BW)` returns the connected components `CC` found in the binary image `BW`. `bwconncomp` uses a default connectivity of 8 for two dimensions, 26 for three dimensions, and `conndef(ndims(BW),'maximal')` for higher dimensions.```

example

````CC = bwconncomp(BW,conn)` returns the connected components where `conn` specifies the desired connectivity for the connected components.Code Generation support: Yes.MATLAB® Function Block support: No.```

## Examples

collapse all

Create a small sample 3-D array.

```BW = cat(3, [1 1 0; 0 0 0; 1 0 0],... [0 1 0; 0 0 0; 0 1 0],... [0 1 1; 0 0 0; 0 0 1]); ```

Find the connected components in the array.

```CC = bwconncomp(BW) ```
```CC = struct with fields: Connectivity: 26 ImageSize: [3 3 3] NumObjects: 2 PixelIdxList: {[5×1 double] [3×1 double]} ```

Calculate centroids of the objects in the array.

```S = regionprops(CC,'Centroid') ```
```S = 2×1 struct array with fields: Centroid ```

Read image into the workspace and display it.

```BW = imread('text.png'); imshow(BW) ```

Find the number of connected components in the image.

```CC = bwconncomp(BW) ```
```CC = struct with fields: Connectivity: 8 ImageSize: [256 256] NumObjects: 88 PixelIdxList: {1×88 cell} ```

Determine which is the largest component in the image and erase it (set all the pixels to 0).

```numPixels = cellfun(@numel,CC.PixelIdxList); [biggest,idx] = max(numPixels); BW(CC.PixelIdxList{idx}) = 0; ```

Display the image, noting that the largest component happens to be the two consecutive f's in the word different.

```figure imshow(BW) ```

## Input Arguments

collapse all

Input binary image, specified as a real, nonsparse, numeric or logical array of any dimension.

Example: `BW = imread('text.png'); CC = bwconncomp(BW);`

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical`

Connectivity for the connected components, specified as one of the following scalar values.

Value

Meaning

Two-dimensional connectivities

4

4-connected neighborhood

8

8-connected neighborhood

Three-dimensional connectivities

6

6-connected neighborhood

18

18-connected neighborhood

26

26-connected neighborhood

To calculate the default connectivity for higher dimensions, `bwconncomp` uses `conndef(ndims(BW),'maximal')`.

Connectivity can be defined in a more general way for any dimension using a 3-by-3-by- ... -by-3 matrix of 0s and 1s. `conn` must be symmetric about its center element. The 1-valued elements define neighborhood locations relative to `conn`.

Example: `BW = imread('text.png'); CC = bwconncomp(BW,4);`

Data Types: `double` | `logical`

## Output Arguments

collapse all

Connected components, returned as a structure with four fields.

FieldDescription
`Connectivity`Connectivity of the connected components (objects)
`ImageSize`Size of `BW`
`NumObjects`Number of connected components (objects) in `BW`
`PixelIdxList`1-by-`NumObjects` cell array where the k-th element in the cell array is a vector containing the linear indices of the pixels in the k-th object.

collapse all

### Code Generation

This function supports the generation of C code using MATLAB Coder™. For more information, see Code Generation for Image Processing.

When generating code, note the following:

• `bwconncomp` only supports 2-D inputs.

• The `conn` arguments must be a compile-time constant and the only connectivities supported are 4 or 8. You can also specify connectivity as a 3-by-3 matrix, but it can only be `[0 1 0;1 1 1;0 1 0]` or `ones(3)`

• The `PixelIdxList` field in the `CC` struct return value is not supported.

### Tips

• The functions `bwlabel`, `bwlabeln`, and `bwconncomp` all compute connected components for binary images. `bwconncomp` replaces the use of `bwlabel` and `bwlabeln`. It uses significantly less memory and is sometimes faster than the other functions.

FunctionInput DimensionOutput FormMemory UseConnectivity
`bwlabel`2-DLabel matrix with double-precisionHigh4 or 8
`bwlabeln`N-DDouble-precision label matrixHighAny
`bwconncomp`N-D`CC` structLowAny

• To extract features from a binary image using `regionprops` with default connectivity, just pass `BW` directly into `regionprops` (i.e., `regionprops(BW)`).

• To compute a label matrix having more memory-efficient data type (e.g., `uint8` versus `double`), use the `labelmatrix` function on the output of `bwconncomp`. See the documentation for each function for more information.

### Algorithms

The basic steps in finding the connected components are:

1. Search for the next unlabeled pixel, `p`.

2. Use a flood-fill algorithm to label all the pixels in the connected component containing `p`.

3. Repeat steps 1 and 2 until all the pixels are labelled.