# Documentation

### This is machine translation

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

## Understanding Morphological Reconstruction

Morphological reconstruction can be thought of conceptually as repeated dilations of an image, called the marker image, until the contour of the marker image fits under a second image, called the mask image. In morphological reconstruction, the peaks in the marker image “spread out,” or dilate.

This figure illustrates this processing in 1-D. Each successive dilation is constrained to lie underneath the mask. When further dilation ceases to change the image, processing stops. The final dilation is the reconstructed image. (Note: the actual implementation of this operation in the toolbox is done much more efficiently. See the `imreconstruct` reference page for more details.) The figure shows the successive dilations of the marker.

Repeated Dilations of Marker Image, Constrained by Mask

Morphological reconstruction is based on morphological dilation, but note the following unique properties:

• Processing is based on two images, a marker and a mask, rather than one image and a structuring element.

• Processing is based on the concept of connectivity, rather than a structuring element.

• Processing repeats until stability; i.e., the image no longer changes.

### Understanding the Marker and Mask

Morphological reconstruction processes one image, called the marker, based on the characteristics of another image, called the mask. The high points, or peaks, in the marker image specify where processing begins. The processing continues until the image values stop changing.

To illustrate morphological reconstruction, consider this simple image. It contains two primary regions, the blocks of pixels containing the values `14` and `18`. The background is primarily all set to `10`, with some pixels set to `11`.

To morphologically reconstruct this image, perform these steps:

1. Create a marker image. As with the structuring element in dilation and erosion, the characteristics of the marker image determine the processing performed in morphological reconstruction. The peaks in the marker image should identify the location of objects in the mask image that you want to emphasize.

One way to create a marker image is to subtract a constant from the mask image, using `imsubtract`.

```marker = imsubtract(A,2) marker = 8 8 8 8 8 8 8 8 8 8 8 12 12 12 8 8 9 8 9 8 8 12 12 12 8 8 8 9 8 8 8 12 12 12 8 8 9 8 9 8 8 8 8 8 8 8 8 8 8 8 8 9 8 8 8 16 16 16 8 8 8 8 8 9 8 16 16 16 8 8 8 8 9 8 8 16 16 16 8 8 8 9 8 9 8 8 8 8 8 8 8 8 8 8 8 8 9 8 8 8 ```
2. Call the `imreconstruct` function to morphologically reconstruct the image. In the output image, note how all the intensity fluctuations except the intensity peak have been removed.

`recon = imreconstruct(marker, mask)`

### Pixel Connectivity

Morphological processing starts at the peaks in the marker image and spreads throughout the rest of the image based on the connectivity of the pixels. Connectivity defines which pixels are connected to other pixels. A set of pixels in a binary image that form a connected group is called an object or a connected component.

Determining which pixels create a connected component depends on how pixel connectivity is defined. For example, this binary image contains one foreground object or two, depending on the connectivity. If the foreground is 4-connected, the image is all one object — there is no distinction between a foreground object and the background. However, if the foreground is 8-connected, the pixels set to 1 connect to form a closed loop and the image has two separate objects: the pixels in the loop and the pixels outside the loop.

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

#### Defining Connectivity in an Image

The following table lists all the standard two- and three-dimensional connectivities supported by the toolbox. See these sections for more information:

Supported Connectivities

 Two-Dimensional Connectivities 4-connected Pixels are connected if their edges touch. This means that a pair of adjoining pixels are part of the same object only if they are both on and are connected along the horizontal or vertical direction. 8-connected Pixels are connected if their edges or corners touch. This means that if two adjoining pixels are on, they are part of the same object, regardless of whether they are connected along the horizontal, vertical, or diagonal direction. Three-Dimensional Connectivities 6-connected Pixels are connected if their faces touch. 18-connected Pixels are connected if their faces or edges touch. 26-connected Pixels are connected if their faces, edges, or corners touch.

#### Choosing a Connectivity

The type of neighborhood you choose affects the number of objects found in an image and the boundaries of those objects. For this reason, the results of many morphology operations often differ depending upon the type of connectivity you specify.

For example, if you specify a 4-connected neighborhood, this binary image contains two objects; if you specify an 8-connected neighborhood, the image has one object.

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

#### Specifying Custom Connectivities

You can also define custom neighborhoods by specifying a 3-by-3-by-...-by-3 array of 0's and 1's. The `1`-valued elements define the connectivity of the neighborhood relative to the center element.

For example, this array defines a “North/South” connectivity which can be used to break up an image into independent columns.

```CONN = [ 0 1 0; 0 1 0; 0 1 0 ] CONN = 0 1 0 0 1 0 0 1 0```

### Note

Connectivity arrays must be symmetric about their center element. Also, you can use a 2-D connectivity array with a 3-D image; the connectivity affects each "page" in the 3-D image.

### Finding Peaks and Valleys

Grayscale images can be thought of in three dimensions: the x- and y-axes represent pixel positions and the z-axis represents the intensity of each pixel. In this interpretation, the intensity values represent elevations, as in a topographical map. The areas of high intensity and low intensity in an image, peaks and valleys in topographical terms, can be important morphological features because they often mark relevant image objects.

For example, in an image of several spherical objects, points of high intensity could represent the tops of the objects. Using morphological processing, these maxima can be used to identify objects in an image.

This section covers these topics:

#### Terminology

This section uses the following terms.

Term

Definition

global maxima

Highest regional maxima in the image. See the entry for regional maxima in this table for more information.

global minima

Lowest regional minima in the image. See the entry for regional minima in this table for more information.

regional maxima

Connected set of pixels of constant intensity from which it is impossible to reach a point with higher intensity without first descending; that is, a connected component of pixels with the same intensity value, t, surrounded by pixels that all have a value less than t.

regional minima

Connected set of pixels of constant intensity from which it is impossible to reach a point with lower intensity without first ascending; that is, a connected component of pixels with the same intensity value, t, surrounded by pixels that all have a value greater than t.

#### Understanding the Maxima and Minima Functions

An image can have multiple regional maxima or minima but only a single global maximum or minimum. Determining image peaks or valleys can be used to create marker images that are used in morphological reconstruction.

This figure illustrates the concept in 1-D.

#### Finding Areas of High or Low Intensity

The toolbox includes functions that you can use to find areas of high or low intensity in an image:

• The `imregionalmax` and `imregionalmin` functions identify all regional minima or maxima.

• The `imextendedmax` and `imextendedmin` functions identify regional minima or maxima that are greater than or less than a specified threshold.

The functions accept a grayscale image as input and return a binary image as output. In the output binary image, the regional minima or maxima are set to `1`; all other pixels are set to `0`.

For example, this simple image contains two primary regional maxima, the blocks of pixels containing the value `13` and `18`, and several smaller maxima, set to `11`.

The binary image returned by `imregionalmax` pinpoints all these regional maxima.

`B = imregionalmax(A)`

You might want only to identify areas of the image where the change in intensity is extreme; that is, the difference between the pixel and neighboring pixels is greater than (or less than) a certain threshold. For example, to find only those regional maxima in the sample image, `A`, that are at least two units higher than their neighbors, use `imextendedmax`.

`B = imextendedmax(A,2)`

#### Suppressing Minima and Maxima

In an image, every small fluctuation in intensity represents a regional minimum or maximum. You might only be interested in significant minima or maxima and not in these smaller minima and maxima caused by background texture.

To remove the less significant minima and maxima but retain the significant minima and maxima, use the `imhmax` or `imhmin` function. With these functions, you can specify a contrast criteria or threshold level, h, that suppresses all maxima whose height is less than h or whose minima are greater than h.

### Note

The `imregionalmin`, `imregionalmax`, `imextendedmin`, and `imextendedmax` functions return a binary image that marks the locations of the regional minima and maxima in an image. The `imhmax` and `imhmin` functions produce an altered image.

For example, this simple image contains two primary regional maxima, the blocks of pixels containing the value `14` and `18`, and several smaller maxima, set to `11`.

To eliminate all regional maxima except the two significant maxima, use `imhmax`, specifying a threshold value of `2`. Note that `imhmax` only affects the maxima; none of the other pixel values are changed. The two significant maxima remain, although their heights are reduced.

`B = imhmax(A,2)`

This figure takes the second row from the sample image to illustrate in 1-D how `imhmax` changes the profile of the image.

#### Imposing a Minimum

You can emphasize specific minima (dark objects) in an image using the `imimposemin` function. The `imimposemin` function uses morphological reconstruction to eliminate all minima from the image except the minima you specify.

To illustrate the process of imposing a minimum, this code creates a simple image containing two primary regional minima and several other regional minima.

```mask = uint8(10*ones(10,10)); mask(6:8,6:8) = 2; mask(2:4,2:4) = 7; mask(3,3) = 5; mask(2,9) = 9; mask(3,8) = 9; mask(9,2) = 9; mask(8,3) = 9```

#### Creating a Marker Image

To obtain an image that emphasizes the two deepest minima and removes all others, create a marker image that pinpoints the two minima of interest. You can create the marker image by explicitly setting certain pixels to specific values or by using other morphological functions to extract the features you want to emphasize in the mask image.

This example uses `imextendedmin` to get a binary image that shows the locations of the two deepest minima.

`marker = imextendedmin(mask,1)`

#### Applying the Marker Image to the Mask

Now use `imimposemin` to create new minima in the mask image at the points specified by the marker image. Note how `imimposemin` sets the values of pixels specified by the marker image to the lowest value supported by the datatype (`0` for `uint8` values). `imimposemin` also changes the values of all the other pixels in the image to eliminate the other minima.

```I = imimposemin(mask,marker) I = 11 11 11 11 11 11 11 11 11 11 11 8 8 8 11 11 11 11 11 11 11 8 0 8 11 11 11 11 11 11 11 8 8 8 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 0 0 0 11 11 11 11 11 11 11 0 0 0 11 11 11 11 11 11 11 0 0 0 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11```

This figure illustrates in 1-D how `imimposemin` changes the profile of row 2 of the image.

Imposing a Minimum

### Flood-Fill Operations

The `imfill` function performs a flood-fill operation on binary and grayscale images. For binary images, `imfill` changes connected background pixels (0's) to foreground pixels (1's), stopping when it reaches object boundaries. For grayscale images, `imfill` brings the intensity values of dark areas that are surrounded by lighter areas up to the same intensity level as surrounding pixels. (In effect, `imfill` removes regional minima that are not connected to the image border. See Finding Areas of High or Low Intensity for more information.) This operation can be useful in removing irrelevant artifacts from images. See these additional topics:

#### Specifying Connectivity

For both binary and grayscale images, the boundary of the fill operation is determined by the connectivity you specify.

### Note

`imfill` differs from the other object-based operations in that it operates on background pixels. When you specify connectivity with `imfill`, you are specifying the connectivity of the background, not the foreground.

The implications of connectivity can be illustrated with this matrix.

```BW = logical([0 0 0 0 0 0 0 0; 0 1 1 1 1 1 0 0; 0 1 0 0 0 1 0 0; 0 1 0 0 0 1 0 0; 0 1 0 0 0 1 0 0; 0 1 1 1 1 0 0 0; 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0]);```

If the background is 4-connected, this binary image contains two separate background elements (the part inside the loop and the part outside). If the background is 8-connected, the pixels connect diagonally, and there is only one background element.

#### Specifying the Starting Point

For binary images, you can specify the starting point of the fill operation by passing in the location subscript or by using `imfill` in interactive mode, selecting starting pixels with a mouse. See the reference page for `imfill` for more information about using `imfill` interactively.

For example, if you call `imfill`, specifying the pixel `BW(4,3)` as the starting point, `imfill` only fills the inside of the loop because, by default, the background is 4-connected.

```imfill(BW,[4 3]) ans = 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0```

If you specify the same starting point, but use an 8-connected background connectivity, `imfill` fills the entire image.

```imfill(BW,[4 3],8) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1```

#### Filling Holes

A common use of the flood-fill operation is to fill holes in images. For example, suppose you have an image, binary or grayscale, in which the foreground objects represent spheres. In the image, these objects should appear as disks, but instead are donut shaped because of reflections in the original photograph. Before doing any further processing of the image, you might want to first fill in the “donut holes” using `imfill`.

Because the use of flood-fill to fill holes is so common, `imfill` includes special syntax to support it for both binary and grayscale images. In this syntax, you just specify the argument `'holes'`; you do not have to specify starting locations in each hole.

To illustrate, this example fills holes in a grayscale image of a spinal column.

```[X,map] = imread('spine.tif'); I = ind2gray(X,map); Ifill = imfill(I,'holes'); imshow(I);figure, imshow(Ifill)```