Morphological Operations

In image processing, the term morphology refers to a set of operations which analyzes shapes to fill in small holes, remove noises, extract contours, etc.

A morphological operation is a lot like the filtering process in spatial convolution. There are two parts in play: a structuring element and a predefined calculation rule.

  • A structuring element(SE), which is analogous to the kernel in convolution, moves point-to-point across each pixel of the input image. The structuring element overlaps a neighborhood of pixels at a time. Each pixel in the structuring element is compared against its right-under pixel in the input image.
  • Different from the process of convolution, the morphological operation is logical (e.g. AND, OR, NOT) rather than arithmetic.

probe image with structuring element

Structuring element moves from point to point ↑

As we know, logical operations only apply to 0/1 pixels. This is why most morphological techniques process binary images. However, some of the techniques can be extended to grayscale images as well.

Shape And Size Of Structuring Element

A structuring element is essentially a matrix of pixels, with each pixel value being zero or one. The size of an SE is the dimensions of the matrix, while the shape is specified by the pattern of ones and zeros. The shape and size of a structuring element is flexible and depends on the imaging geometry of the objects of interest. For example, a linear SE can detect linear lines.

Linear, square and isotropic disks are commonly used shapes for SEs. The center pixel is called the origin.

example structuring element

Primitive Morphological Operations: Erosion And Dilation

Erosion

Erosion probes the input image with the SE and examines if the SE “fits” the corresponding neighborhood of pixels. A structuring element is said to fit a neighborhood of pixels if, for each of its “1” pixels, the corresponding pixel is also “1”. Erosion reduces the size of regions and leads to thinning.

With A denoting the input image, and B denoting the SE, an erosion is given as:

structuring image dilation

Visual effects of erosion include:

  • Strip away the small-scale details along the edges
  • Enlarge the holes enclosed in a region
  • Set apart loosely joined objects, and expand the gap between different regions
Original image structuring element After erosion
kernel = np.ones((10, 1), np.uint8)
kernel = np.ones((3, 3), np.uint8)
kernel = np.ones((10, 1), np.uint8)

Dilation

Dilation probes the input image with the SE and examines if the SE “hits” or intersects the corresponding neighborhood of pixels. A structuring element is said to hit a neighborhood of pixels if, for any of its “1” pixels, the corresponding pixel is “1”. Dilation expands the size of regions and leads to thickening.

With ‘A’ denoting the input image, and ‘B’ denoting the SE, a dilation is given as:

Visual effects of dilation include:

  • Bridge gaps of character due to poor resolution
  • Connect adjacent regions or shrink the gap between regions
  • Repair small-scale defects and noises
Original image Structuring element After closing[1]
kernel = np.ones((10, 1), np.uint8)

[1] Closing is to first perform dilation operation and then perform erosion operation.

Morphological Operations in Dynamsoft Barcode Reader

Dynamsoft Barcode Reader SDK is an enterprise-grade SDK known for its decoding accuracy and speed. It’s capable of scanning 1D, 2D, QR codes and more from static images and live video feed on Android, iOS, Windows, macOS, and Linux. It utilizes proprietary algorithms and morphological operations to clean up the images and improve both the decoding rate and accuracy.

Try Dynamsoft Barcode Reader online demo »

Reference:

https://www.cs.auckland.ac.nz/courses/compsci773s1c/lectures/ImageProcessing-html/topic4.htm
http://www.cyto.purdue.edu/cdroms/micro2/content/education/wirth07.pdf