Spatial Filtering and Morphological Operations

Abel Joshua Cruzada
3 min readJan 13, 2021

Have you ever tried taking a picture without capturing unnecessary objects? For instance, you have an image of a map with many information such as the road, buildings, and other landmarks’ name, and you only want to extract the roads. Capturing such a picture can be very difficult, near impossible. To solve this kind of problem, I will discuss the high-level definitions of spatial filtering and morphological operations to eliminate unnecessary objects in our image in this blog.

Spatial Filtering

Spatial Filtering is a technique to transform our image depending on the issue we want to solve. It assigns new values of a pixel-based on their neighbors using defined matrices known as kernels. The kernels are applied in our image through the process of convolution. We can use different kinds of kernels to achieve different effects like the one we see in Photoshop, such as sharpening, edge detection, and blurring. Figure 1 shows the effect of each kernel on a single image.

Fig. 1. Effects of different kernels Note: From Kernel (Image Processing) Wikipedia (https://en.wikipedia.org/wiki/Kernel_(image_processing))

Morphological Operations

Depending on your goal, you can redefine your kernel. For example, if you want to identify or segment objects in your image, you can use the edge detection kernel. If you’re going to clean noises in your image, you can use the blur kernel variations. Spatial filtering and kernels are widely used in machine learning, specifically Convolutional Neural Network (CNN), to extract relevant features in images.

Once we have transformed our images using Spatial Filtering or a thresholding method, there are cases where parts of some objects are removed as well. Morphological Operations is a collection of image processing operations based on a structured element. It transforms each pixel of an image based on its neighbors’ values selected by the structuring element. A structuring element can take on any form given and can be tailored based on the objective.

Fig. 2. Sample structuring element (cross, horizontal line, vertical line)

Pixels to be transformed by the morphological operations are identified if at least one of the structuring element’s pixels covers the field image.

Fig. 3. Structuring Element Coverage (top image covered, bottom image not covered)

Two Basic Morphological Operations:

1. Erosion — Set the pixel value to minimum over all the neighboring pixels based on the structuring element.

Fig. 4. Erosion (left image original, right image result)

2. Dilation — Set the pixel value to maximum over all the neighboring pixels based on the structuring element.

Fig. 5. Dilation(left image original, right image result)

Going back to the map example, we can use spatial filtering and morphological operations to extract only the roads in our map. Roads that are separated by the text can be closed together using morphological operations or vice versa.

Fig. 6. Sample Spatial Filtering and Morphological Operations(left image original, right image result)

Learning spatial filtering and morphological operations can be a significant asset. These techniques are quite useful in extracting relevant features and removing unnecessary components in your image which could also improve your machine learning models.

Originally published at https://abeljoshuacruzada.wixsite.com on January 13, 2021.

--

--

Abel Joshua Cruzada

An aspiring Data Science leader who aims to empower society through data.