## Types of Morphological Operations

Morphology is a broad set of image processing operations that process images based on shapes. Morphological operations apply a structuring element to an input image, creating an output image of the same size. In a morphological operation, the value of each pixel in the output image is based on a comparison of the corresponding pixel in the input image with its neighbors.

### Morphological Dilation and Erosion

The most basic morphological operations are dilation and erosion. Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. The number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image. In the morphological dilation and erosion operations, the state of any given pixel in the output image is determined by applying a rule to the corresponding pixel and its neighbors in the input image. The rule used to process the pixels defines the operation as a dilation or an erosion. This table lists the rules for both dilation and erosion.

Rules for Dilation and Erosion

Operation

Rule

Example (Original and Processed Image)

Dilation

The value of the output pixel is the maximum value of all pixels in the neighborhood. In a binary image, a pixel is set to `1` if any of the neighboring pixels have the value `1`.

Morphological dilation makes objects more visible and fills in small holes in objects. Lines appear thicker, and filled shapes appear larger.

Erosion

The value of the output pixel is the minimum value of all pixels in the neighborhood. In a binary image, a pixel is set to `0` if any of the neighboring pixels have the value `0`.

Morphological erosion removes floating pixels and thin lines so that only substantive objects remain. Remaining lines appear thinner and shapes appear smaller.

The following figure illustrates the dilation of a binary image. The structuring element defines the neighborhood of the pixel of interest, which is circled. The dilation function applies the appropriate rule to the pixels in the neighborhood and assigns a value to the corresponding pixel in the output image. In the figure, the morphological dilation function sets the value of the output pixel to `1` because one of the elements in the neighborhood defined by the structuring element is on. For more information, see Structuring Elements.

Morphological Dilation of a Binary Image

The following figure illustrates this processing for a grayscale image. The dilation function applies the rule to the neighborhood of the circled pixel of interest. The value of the corresponding pixel in the output image is assigned as the highest value among all neighborhood pixels. In the figure, the value of the output pixel is `16` because it is the highest value in the neighborhood defined by the structuring element.

Morphological Dilation of a Grayscale Image

### Operations Based on Dilation and Erosion

Dilation and erosion are often used in combination to implement image processing operations. For example, the definition of a morphological opening of an image is an erosion followed by a dilation, using the same structuring element for both operations. You can combine dilation and erosion to remove small objects from an image and smooth the border of large objects.

This table lists functions in the toolbox that perform common morphological operations that are based on dilation and erosion.

Function

Morphological Definition

Example (Original and Processed Image)

`imopen`

Perform morphological opening. The opening operation erodes an image and then dilates the eroded image, using the same structuring element for both operations.

Morphological opening is useful for removing small objects and thin lines from an image while preserving the shape and size of larger objects in the image. For an example, see Use Morphological Opening to Extract Large Image Features.

`imclose`

Perform morphological closing. The closing operation dilates an image and then erodes the dilated image, using the same structuring element for both operations.

Morphological closing is useful for filling small holes in an image while preserving the shape and size of large holes and objects in the image.

`bwskel`

Skeletonize objects in a binary image. The process of skeletonization erodes all objects to centerlines without changing the essential structure of the objects, such as the existence of holes and branches.

`bwperim`

Find perimeter of objects in a binary image. A pixel is part of the perimeter if it is nonzero and it is connected to at least one zero-valued pixel. Therefore, edges of interior holes are considered part of the object perimeter.

`bwhitmiss`

Perform binary hit-miss transform. The hit-miss transform preserves pixels in a binary image whose neighborhoods match the shape of one structuring element and do not match the shape of a second disjoint structuring element.

The hit-miss transforms can be used to detect patterns in an image.

This example uses one structuring element with a neighborhood above and to the right of center, and a second structuring element with a neighborhood below and to the left of center. The transform preserves pixels with neighbors only to the top and right.

`imtophat`

Perform a morphological top-hat transform. The top-hat transform opens an image, then subtracts the opened image from the original image.

The top-hat transform can be used to enhance contrast in a grayscale image with nonuniform illumination. The transform can also isolate small bright objects in an image.

`imbothat`

Perform a morphological bottom-hat transform. The bottom-hat transform closes an image, then subtracts the original image from the closed image.

The bottom-hat transform isolates pixels that are darker than other pixels in their neighborhood. Therefore, the transform can be used to find intensity troughs in a grayscale image.