Filtering and morphing of digital images using Python: An Image Processing Introduction

Cymon Marcaida
3 min readFeb 2, 2021

Digital images could not only be enhanced, but could also be filtered or morphed. In this article, I will discuss two ways: spatial filters and morphological operations

Spatial filters

Spatial filters are matrix filters that are applied on an image using convolution. There are different kinds of spatial filters that could be applied in an image, however I will only discuss two spatial filters: horizontal and vertical sobel filters. Let’s use this image again:

Guinan & Captain Jean-Luc Picard aboard USS Enterprise-D
import matplotlib.pyplot as plt
from scipy.signal import convolve2d
gp_bw = rgb2gray(imread('guinanpicard.png'))
## Applying Horizontal Sobel Filter
## Horizontal Sobel Filter
kernel1 = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
conv_im1 = convolve2d(gp_bw, kernel1, 'valid')
imshow(abs(conv_im1), cmap='gray')
## Applying Vertical Sobel Filter## Vertical Sobel Filter
kernel2 = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
conv_im2 = convolve2d(gp_bw, kernel2, 'valid')
imshow(abs(conv_im2), cmap='gray')
Image with the Horizontal Sobel filter (left) and Vertical Sobel (right)

We can see here the purpose of the two filters. The horizontal sobel filter was used to identify the horizontal outlines from our original image. The hat of Guinan and the head of Picard including their eyelids are outlined using that filter.

Meanwhile, the vertical sobel filter was used to identify vertical outlines of the image. Using that filter we could clearly see the clothes outline of both Guinan and Picard including his insignia.

Morphological operations

Besides from spatial filtering, we could also do morphological operations in our image. These operations are tend to be applied in order to “correct” or “complete” some objects within the image. Let’s use this simple image for this problem:

from skimage.draw import circlecirc_image = np.zeros((25, 25))
circ_image[circle(12, 12, 10)] = 1
imshow(circ_image);
A simple circle

Using this simple circle image, we will use morphological operations to change its appearance. First, let’s set up our structuring element. Our structuring element will define how the morphological operations will morph our image:

selem_circ = np.array([[0,1,0],
[1,1,1],
[0,1,0]])
imshow(selem_circ, cmap=’gray’);
Structuring element

Since we have defined our structural element, I will try to erode the image using it:

from skimage.morphology import erosionimshow(erosion(circ_image, selem_circ));
Original image (left) and eroded image (right)

The circle became small and octagon-like when we applied an erosion morphology into it. This is because erosion attempts to “smoothen” up the image by removing excess pixels using the given element. Let’s use another morphological operation:

from skimage.morphology import dilationimshow(dilation(circ_image, selem_circ));
Original image (left) and dilated image (right)

We used a morphological operation called dilation which is quite the opposite of erosion. What happened to our image? The circle grew based on the given structural element when we dilated our image.

We could see here that the structural element defines how the morphological operations will affect the image. This means that different structural element means different resulting image. There are different morphological operations available besides from dilation and erosion. Feel free to try them when you have time!

That’s it for this article!

--

--