Introduction to Image Processing — Part 3: Spatial Filtering and Morphological Operations
Ever wonder how your favorite photo editing tool or native photo manipulation in your smartphones do simple effects such as blurring, sharpening, embossing, and outlining? We will have an idea of how they do in this part, but more importantly, we will discuss the methods we can use to detect and clean our objects of interest from an image. Specifically, we will go through Spatial Filters and Morphological Operations.
Spatial Filters
Spatial filtering is the process of assigning the value of a pixel based on its neighbors. The filters or masks, which are also known as kernels, used in the process are small matrices run in the entire image through a convolution process. [1]
In machine learning (ML), particularly in artificial neural networks (ANN), spatial filtering is used to extract features by applying different kernels to the image [2]. A list of possible spatial filters we can use are shown in Table 1.
Now, let’s try using it on our favorite photo, Lenna, in a grayscale image.
We can see from the result the output image significantly varies, depending on the filter we use. In the Edge filter, for example, the image only highlighted the edges of the image, while the Sharpen and Blur filters, from the name itself, sharpens and blurs the original image to a certain degree.
Morphological Operations
Morphological operations transform the image’s pixel value based on its neighbors. Similar to Spatial filtering, it also uses smaller matrices, also known as structuring elements. [1] These operations can be used to further enhance and clean our image by removing, enlarging, or connecting segmented parts in our image.
Now, let’s try to do morphological operations in the set of images below.
Our goal in each image would be to:
- Image 1 — remove the straight lines
- Image 2 — remove the horizontal lines
- Image 3 — connect the gaps
The first thing we need to do is to build our structuring elements that would match our objects of interest.
The scikit-image library contains a lot of morphological operations we can choose from. For this task, we will only use four operations: erosion, dilation, opening, and closing. Because we may need to do several iterations on a specific morphological operation, it would be convenient to put them in a function that would handle the iterations.
By playing around with different structuring elements and morphological operations, we now have the resulting images, as shown in Figure 3.
Removing and connecting several elements are not that perfect. In the resulting Image 1, you will notice that the circles are a bit smaller than the original image. Image 3, on the other hand, still contains disconnected elements, particularly at the intersection. These outputs can even be further improved by exploring other combinations of structuring elements and morphological operations.
The stuff that we have discussed here could help prepare our images for the next steps of image processing tasks, such as blob detection and image segmentation, which will be the next topic of this series.
References
[1] B. Borja, “Lecture 3 — Filtering and Morphological Operations”, IIP 2020
[2] V. Powell, “Image Kernels,” Setosa, [Online]. Available: https://setosa.io/ev/image-kernels/.
[3] “Kernel (image processing),” Wikipedia, [Online]. Available: https://en.wikipedia.org/wiki/Kernel_(image_processing).