Accelerating the pace of engineering and science

# bwmorph

Morphological operations on binary images

## Syntax

• BW2 = bwmorph(BW,operation) example
• BW2 = bwmorph(BW,operation,n)
• gpuarrayBW2 = bwmorph(gpuarrayBW,___) example

## Description

example

BW2 = bwmorph(BW,operation) applies a specific morphological operation to the binary image BW.

This function supports code generation (see Tips).

BW2 = bwmorph(BW,operation,n) applies the operation n times. n can be Inf, in which case the operation is repeated until the image no longer changes.

example

gpuarrayBW2 = bwmorph(gpuarrayBW,___) performs the morphological operation on a GPU. The input image and output image are gpuArrays. This syntax requires the Parallel Computing Toolbox™.

## Examples

### Perform Morphological Operations on Binary Image

Read binary image and display it.

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

Remove interior pixels to leave an outline of the shapes.

```BW2 = bwmorph(BW,'remove');
figure
imshow(BW2)
```

Get the image skeleton.

```BW3 = bwmorph(BW,'skel',Inf);
figure
imshow(BW3)
```

### Perform Morphological Operations on a GPU

This example performs the same operations as the previous example but performs them on a GPU. The example starts by reading the image into a gpuArray.

```BW1 = gpuArray(imread('circles.png'));
figure
imshow(BW1)

BW2 = bwmorph(BW1,'remove');
figure
imshow(BW2)

BW3 = bwmorph(BW1,'skel',Inf);
figure
imshow(BW3)```

## Input Arguments

expand all

### BW — Input imagebinary image

Input image, specified as a binary image. The input image can be numeric or logical, but must be 2-D, real and nonsparse.

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

### operation — Morphological operation to performcharacter string

Morphological operation to perform, specified as one of the following character strings.

Operation

Description

'bothat'

Performs the morphological "bottom hat" operation, returning the image minus the morphological closing of the image (dilation followed by erosion).

'branchpoints'

Find branch points of skeleton. For example:

```0  0  1  0  0           0  0  0  0  0
0  0  1  0  0  becomes  0  0  0  0  0
1  1  1  1  1           0  0  1  0  0
0  0  1  0  0           0  0  0  0  0
0  0  1  0  0           0  0  0  0  0```

Note: To find branch points, the image must be skeletonized. To create a skeletonized image, use bwmorph(BW,'skel').

'bridge'

Bridges unconnected pixels, that is, sets 0-valued pixels to 1 if they have two nonzero neighbors that are not connected. For example:

```1  0  0           1  1  0
1  0  1  becomes  1  1  1
0  0  1           0  1  1
```

'clean'

Removes isolated pixels (individual 1s that are surrounded by 0s), such as the center pixel in this pattern.

```0  0  0
0  1  0
0  0  0
```

'close'

Performs morphological closing (dilation followed by erosion).

'diag'

Uses diagonal fill to eliminate 8-connectivity of the background. For example:

```0  1  0           0  1  0
1  0  0  becomes  1  1  0
0  0  0           0  0  0
```
'dilate'To perform dilation using the structuring element ones(3), use imdilate.

'endpoints'

Finds end points of skeleton. For example:

```1  0  0  0           1  0  0  0
0  1  0  0  becomes  0  0  0  0
0  0  1  0           0  0  1  0
0  0  0  0           0  0  0  0```

Note: To find end points, the image must be skeletonized. To create a skeletonized image, use bwmorph(BW,'skel').

'erode'To perform erosion using the structuring element ones(3), use imerode.

'fill'

Fills isolated interior pixels (individual 0s that are surrounded by 1s), such as the center pixel in this pattern.

```1  1  1
1  0  1
1  1  1
```

'hbreak'

Removes H-connected pixels. For example:

```1  1  1           1  1  1
0  1  0  becomes  0  0  0
1  1  1           1  1  1
```

'majority'

Sets a pixel to 1 if five or more pixels in its 3-by-3 neighborhood are 1s; otherwise, it sets the pixel to 0.

'open'

Performs morphological opening (erosion followed by dilation).

'remove'

Removes interior pixels. This option sets a pixel to 0 if all its 4-connected neighbors are 1, thus leaving only the boundary pixels on.

'shrink'

With n = Inf, shrinks objects to points. It removes pixels so that objects without holes shrink to a point, and objects with holes shrink to a connected ring halfway between each hole and the outer boundary. This option preserves the Euler number.

'skel'

With n = Inf, removes pixels on the boundaries of objects but does not allow objects to break apart. The pixels remaining make up the image skeleton. This option preserves the Euler number.

'spur'

Removes spur pixels. For example:

```0  0  0  0           0  0  0  0
0  0  0  0           0  0  0  0
0  0  1  0  becomes  0  0  0  0
0  1  0  0           0  1  0  0
1  1  0  0           1  1  0  0
```

'thicken'

With n = Inf, thickens objects by adding pixels to the exterior of objects until doing so would result in previously unconnected objects being 8-connected. This option preserves the Euler number.

'thin'

With n = Inf, thins objects to lines. It removes pixels so that an object without holes shrinks to a minimally connected stroke, and an object with holes shrinks to a connected ring halfway between each hole and the outer boundary. This option preserves the Euler number. See Algorithms for more detail.

'tophat'

Performs morphological "top hat" operation, returning the image minus the morphological opening of the image (erosion followed by dilation).

Example: BW3 = bwmorph(BW,'skel');

Data Types: char

### n — Number of times to perform the operationnumeric value

Number of times to perform the operation, specified as a numeric value. n can be Inf, in which case bwmorph repeats the operation until the image no longer changes.

Example: BW3 = bwmorph(BW,'skel',100);

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

### gpuarrayBW — Input imagebinary image in a gpuArray

Input image, specified as a binary image of class logical in a gpuArray. The input image can be numeric or logical, but must be 2-D, real and nonsparse.

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

## Output Arguments

expand all

### BW2 — Output imagebinary image

Output image returned as a binary image of class logical.

### gpuarrayBW2 — Output imagebinary image in a gpuArray

Output image returned as a binary image of class logical in a gpuArray.

expand all

### Tips

• This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Code Generation Using a Shared Library.

When generating code, the text string specifying the operation must be a compile-time constant and, for best results, the input image must be of class logical.

• To perform erosion or dilation using the structuring element ones(3), use imerode or imdilate.

### Algorithms

When used with the 'thin' option, bwmorph uses the following algorithm (References [3]):

1. In the first subiteration, delete pixel p if and only if the conditions G1, G2, and G3 are all satisfied.

2. In the second subiteration, delete pixel p if and only if the conditions G1, G2, and ${G}_{3}\prime$ are all satisfied.

### Condition G1:

${X}_{H}\left(p\right)=1$

where

${X}_{H}\left(p\right)=\sum _{i=1}^{4}{b}_{i}$

x1, x2, ..., x8 are the values of the eight neighbors of p, starting with the east neighbor and numbered in counter-clockwise order.

### Condition G2:

$2\le \mathrm{min}\left\{{n}_{1}\left(p\right),{n}_{2}\left(p\right)\right\}\le 3$

where

${n}_{1}\left(p\right)=\sum _{k=1}^{4}{x}_{2k-1}\vee {x}_{2k}$

${n}_{2}\left(p\right)=\sum _{k=1}^{4}{x}_{2k}\vee {x}_{2k+1}$

### Condition G3:

$\left({x}_{2}\vee {x}_{3}\vee {\overline{x}}_{8}\right)\wedge {x}_{1}=0$

### Condition G3':

$\left({x}_{6}\vee {x}_{7}\vee {\overline{x}}_{4}\right)\wedge {x}_{5}=0$

The two subiterations together make up one iteration of the thinning algorithm. When the user specifies an infinite number of iterations (n=Inf), the iterations are repeated until the image stops changing. The conditions are all tested using applylut with precomputed lookup tables.

## References

[1] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Vol. 1, Addison-Wesley, 1992.

[2] Kong, T. Yung and Azriel Rosenfeld, Topological Algorithms for Digital Image Processing, Elsevier Science, Inc., 1996.

[3] Lam, L., Seong-Whan Lee, and Ching Y. Suen, "Thinning Methodologies-A Comprehensive Survey," IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol 14, No. 9, September 1992, page 879, bottom of first column through top of second column.

[4] Pratt, William K., Digital Image Processing, John Wiley & Sons, Inc., 1991.