Skip to content
Search

Mathematical morphology

lastmod 2023-10-26created 2021-10-2414 edits

Mathematical morphology (MM) is a theory and technique for analyzing and processing geometric structures, based on set theory, lattice theory, topology, and random functions. MM is mostly applied to digital images, but it can also be applied to graphs, polygon meshes, solids, and many other spatial structures.

Topological and geometric continuous-space concepts such as size, shape, convexity, connectivity, and geodesic distance were introduced by MM on both continuous and discrete spaces. MM is also the foundation of morphological digital image processing, which consists of a set of operators that transform images according to the above characterizations.

The basic morphological operators are erosion, dilation, opening, and closing.

MM was originally developed for binary images and was later extended to grayscale functions and images. The subsequent generalization to complete lattices is widely accepted today as the theoretical foundation of MM.

in Image Processing

In image processing, the morphology technique is an approach that views images from a morphological perspective. An image processing technique used to transform the shape of specific objects within an image.

  • Mainly used in binary image processing
  • Uses set inclusion, translation, symmetry, complement, difference, etc.
  • Used to remove noise from an image or to describe the shape of objects in an image (e.g. representative examples of morphology are the erosion and dilation operations)
  • The erosion operation has the effect of shaving off object regions in the image
  • The dilation operation results in expanding object regions

Filters

Closing filter
You can see that small holes in the white foreground objects have been filled, and several adjacent objects have been connected together. Removes any holes or gaps too small to contain the structuring element.
Opening filter
Removes several small objects in the scene. Removes everything too small to contain the structuring element.

Both filters are often used in object detection.

  • The opening filter removes small blobs caused by image noise, and
  • the closing filter connects objects that were wrongly fragmented into small parts.

That is, it is advantageous to use the two filters in sequence.

Erosion

Sets the boundary pixels of white objects in a binary image to 0 (black).

  • Can be used to remove noise (small white objects), or
  • to separate objects that are stuck together.

In OpenCV, use cv2.erode.

kernel = np.ones((5, 5), np.uint8)
result = cv2.erode(img, kernel, iterations=1)

Morphology-erosion-kernel-samples.png

You can also control how many pixels at the object boundary become 0 by fixing the kernel size and increasing the number of iterations (iterations).

Morphology-erosion-iteration-samples.png

Dilation

Works the opposite way from Erosion. Adds 1 (white) around the boundary pixels of white objects in a binary image.

  • Emphasizes objects.
  • If there is a nonzero value x, fills the surroundings with x.
  • Can be used to restore objects shrunk by Erosion (used to remove noise - small white objects -) to their original size, or
  • to merge adjacent objects into one.

In OpenCV, use cv2.dilate.

kernel = np.ones((5, 5), np.uint8)
result = cv2.dilate(img, kernel, iterations = 1)

Morphology-dilation-kernel-samples.png

Likewise, the number of iterations (iterations) can be adjusted.

Morphology-dilation-iteration-samples.png

Opening

Applies the Erosion operation followed by the Dilation operation. Used to remove noise (small white objects) in an image.

Applying Dilation to objects shrunk by Erosion (used to remove noise (small white objects)) brings the objects back to their original size.

In OpenCV, use cv2.morphologyEx with cv2.MORPH_OPEN as the argument.

kernel = np.ones((5, 5), np.uint8)
result = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

Morphology-opening-samples.png

Closing

The opposite of Opening: applies the Dilation operation first, then the Erosion operation. Used to fill holes (small black spots in white objects). -> removes black noise

In OpenCV, use cv2.morphologyEx with cv2.MORPH_CLOSE as the argument.

kernel = np.ones((11, 11), np.uint8)
result = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

Morphology-closing-samples.png

Gradient

Used when you want to strongly emphasize edges.

Gradient = 팽창(dilation) - 침식(erosion)

Mathematical_morphology_-_Gradient.gif

Tophat

Emphasizes regions where brightness changes sharply.

Tophat = 원본 - Opening

Mathematical_morphology_-_Tophat.gif

Blackhat

Emphasizes dark regions.

Blackhat = Closing - 원본

Mathematical_morphology_-_Blackhat.gif

See also

Favorite site

OpenCV

author Zer0 sitemap