Performing Morphological Operations

Morphological operations alter the intensities of specific regions of an image. Unlike the convolution or geometric operations which affect an entire image, morphological operations isolate certain sections of an image (acknowledging them as either foreground or background) and then expands or contracts just those regions to achieve a desired effect.

Developers who are looking for efficient ways to enhance or isolate qualities of an image that are found in either the foreground or the background of an image will find the morphological operations in vImage useful. For example, you can apply morphological operations to scientific images of the terrain of Mars to perform topological analyses that isolate the craters and valleys along the planet’s surface. In general, morphological operations are well-suited for:

This chapter describes the basics of image processing using morphology. By reading this chapter, you’ll:


The target of vImage morphological operations differs from that of the other types of operations found in vImage. Instead of applying an operation to an entire image, vImage applies morphological operations to an object. An object is composed of either the brightest pixels in an image or the darkest pixels in the image, where brightness is defined relative to the particular image.

Figure 4-1 shows objects outlined in an image.

Figure 4-1  Example of objects in an image

Morphological functions change the shape of an object by performing dilation, erosion, maximum, and minimum operations. Dilation expands objects. Erosion contracts them. Maximum is a special case of dilation, while minimum is a special case of erosion. As with convolution, the precise nature of the expanding or shrinking is determined by a kernel the calling function provides. The number of rows and number of columns of the image does not change after applying a morphological operation.

When you define bright pixels as the object, dark pixels become the background. In this case dilation expands objects with erosion contracts them. When you define dark pixels as the object, bright pixels become the background. In this case, dilation contracts objects and erosion expands them.

Figure 4-2  Examples of object dilation and erosion

You can use morphological functions on grayscale images, where the source image is planar (single-channel) or on full-color images. The kernel itself (explained in more detail in the following section) is always planar.


Each morphological function requires that you pass it a convolution kernel that determines how the values of neighboring pixels are used to compute the value of a destination pixel. A kernel is a packed array, without padding at the ends of the rows. See also Convolution Kernels. The elements of the array must be of type uint8_t (for the Planar8 and ARGB8888 formats) or of type float (for the PlanarF and ARGBFFFF formats). The height and the width of the array must both be odd numbers.

For example, a 3 x 3 convolution kernel for a Planar8 image consists of an array of nine 8-bit (1-byte) values, arranged consecutively. The first three values represent the first row of the kernel, the next three values the second row, and the last three values the third row.

Morphology functions perform clipping to prevent overflow for the Planar8 and ARGB8888 formats. Saturated clipping maps all intensity levels above 255, to 255, all intensity levels below 0, to 0, and leaves intensity levels between 0 and 255, inclusive, unchanged.

When the pixel to be transformed is near the edge of the image—not merely the region of interest, but the entire image of which it is a part—the kernel may extend beyond the edge of the image, so that there are no existing pixels beneath some of the kernel elements. This scenario is known as an edge case, as illustrated in Figure 4-3.

Figure 4-3  Example of an edge case

In this case the morphology functions make use of only that part of the kernel which overlaps the source buffer. The other kernel elements are ignored.

vImage performs morphological operations on full-color images in the following way:

Operation Types

There are three main types of morphological operations: dilation, erosion, and maximizing/minimizing.